diff --git a/packages/babylon/src/tokenizer/context.js b/packages/babylon/src/tokenizer/context.js index 7a345726cf..49393869a0 100644 --- a/packages/babylon/src/tokenizer/context.js +++ b/packages/babylon/src/tokenizer/context.js @@ -103,8 +103,8 @@ tt.incDec.updateContext = function() { // tokExprAllowed stays unchanged }; -tt._function.updateContext = function() { - if (this.curContext() !== types.braceStatement) { +tt._function.updateContext = function(prevType) { + if (this.state.exprAllowed && !this.braceIsBlock(prevType)) { this.state.context.push(types.functionExpression); } diff --git a/packages/babylon/test/fixtures/core/categorized/01-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/01-regex/actual.js new file mode 100644 index 0000000000..7cf0857eda --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/01-regex/actual.js @@ -0,0 +1 @@ +const x = function foo() {} /42/i diff --git a/packages/babylon/test/fixtures/core/categorized/01-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/01-regex/expected.json new file mode 100644 index 0000000000..10cbc78c12 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/01-regex/expected.json @@ -0,0 +1,205 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "sourceType": "script", + "body": [ + { + "type": "VariableDeclaration", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 6, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "id": { + "type": "Identifier", + "start": 6, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 7 + }, + "identifierName": "x" + }, + "name": "x" + }, + "init": { + "type": "BinaryExpression", + "start": 10, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 33 + } + }, + "left": { + "type": "BinaryExpression", + "start": 10, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "left": { + "type": "FunctionExpression", + "start": 10, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "id": { + "type": "Identifier", + "start": 19, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 25, + "end": 27, + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 27 + } + }, + "body": [], + "directives": [] + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start": 29, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 29 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 32, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 32 + }, + "end": { + "line": 1, + "column": 33 + }, + "identifierName": "i" + }, + "name": "i" + } + } + } + ], + "kind": "const" + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/02-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/02-regex/actual.js new file mode 100644 index 0000000000..4ece5a5688 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/02-regex/actual.js @@ -0,0 +1 @@ +(function foo() {} /42/i) diff --git a/packages/babylon/test/fixtures/core/categorized/02-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/02-regex/expected.json new file mode 100644 index 0000000000..0e56014fea --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/02-regex/expected.json @@ -0,0 +1,174 @@ +{ + "type": "File", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 1, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "left": { + "type": "BinaryExpression", + "start": 1, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "left": { + "type": "FunctionExpression", + "start": 1, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 13, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 13 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 16, + "end": 18, + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "body": [], + "directives": [] + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start": 20, + "end": 22, + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 23, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + }, + "identifierName": "i" + }, + "name": "i" + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/03-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/03-regex/actual.js new file mode 100644 index 0000000000..f03c2d35a4 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/03-regex/actual.js @@ -0,0 +1,3 @@ +(function () { + return function fn() {} /42/i +}) diff --git a/packages/babylon/test/fixtures/core/categorized/03-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/03-regex/expected.json new file mode 100644 index 0000000000..c44a030fe1 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/03-regex/expected.json @@ -0,0 +1,226 @@ +{ + "type": "File", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 49, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 3, + "column": 2 + } + }, + "expression": { + "type": "FunctionExpression", + "start": 1, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "id": null, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 13, + "end": 48, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 3, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 17, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "argument": { + "type": "BinaryExpression", + "start": 24, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 31 + } + }, + "left": { + "type": "BinaryExpression", + "start": 24, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "left": { + "type": "FunctionExpression", + "start": 24, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 9 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 33, + "end": 35, + "loc": { + "start": { + "line": 2, + "column": 18 + }, + "end": { + "line": 2, + "column": 20 + }, + "identifierName": "fn" + }, + "name": "fn" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 38, + "end": 40, + "loc": { + "start": { + "line": 2, + "column": 23 + }, + "end": { + "line": 2, + "column": 25 + } + }, + "body": [], + "directives": [] + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 2, + "column": 27 + }, + "end": { + "line": 2, + "column": 29 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 45, + "end": 46, + "loc": { + "start": { + "line": 2, + "column": 30 + }, + "end": { + "line": 2, + "column": 31 + }, + "identifierName": "i" + }, + "name": "i" + } + } + } + ], + "directives": [] + }, + "extra": { + "parenthesized": true, + "parenStart": 0 + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/04-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/04-regex/actual.js new file mode 100644 index 0000000000..e9013ccd03 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/04-regex/actual.js @@ -0,0 +1,5 @@ +function fn() { + return + function foo() {} + /42/i +} diff --git a/packages/babylon/test/fixtures/core/categorized/04-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/04-regex/expected.json new file mode 100644 index 0000000000..a57a133e2d --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/04-regex/expected.json @@ -0,0 +1,190 @@ +{ + "type": "File", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 11, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 11 + }, + "identifierName": "fn" + }, + "name": "fn" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 14, + "end": 54, + "loc": { + "start": { + "line": 1, + "column": 14 + }, + "end": { + "line": 5, + "column": 1 + } + }, + "body": [ + { + "type": "ReturnStatement", + "start": 18, + "end": 24, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 8 + } + }, + "argument": null + }, + { + "type": "FunctionDeclaration", + "start": 27, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "id": { + "type": "Identifier", + "start": 36, + "end": 39, + "loc": { + "start": { + "line": 3, + "column": 11 + }, + "end": { + "line": 3, + "column": 14 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 42, + "end": 44, + "loc": { + "start": { + "line": 3, + "column": 17 + }, + "end": { + "line": 3, + "column": 19 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExpressionStatement", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "expression": { + "type": "RegExpLiteral", + "start": 47, + "end": 52, + "loc": { + "start": { + "line": 4, + "column": 2 + }, + "end": { + "line": 4, + "column": 7 + } + }, + "extra": { + "raw": "/42/i" + }, + "pattern": "42", + "flags": "i" + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/05-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/05-regex/actual.js new file mode 100644 index 0000000000..80f6e6ab31 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/05-regex/actual.js @@ -0,0 +1 @@ +test(); function foo() {} /42/i diff --git a/packages/babylon/test/fixtures/core/categorized/05-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/05-regex/expected.json new file mode 100644 index 0000000000..7d3effcb5a --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/05-regex/expected.json @@ -0,0 +1,169 @@ +{ + "type": "File", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 7, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "expression": { + "type": "CallExpression", + "start": 0, + "end": 6, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 6 + } + }, + "callee": { + "type": "Identifier", + "start": 0, + "end": 4, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + }, + "identifierName": "test" + }, + "name": "test" + }, + "arguments": [] + } + }, + { + "type": "FunctionDeclaration", + "start": 8, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 8 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "id": { + "type": "Identifier", + "start": 17, + "end": 20, + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 20 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 23, + "end": 25, + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 25 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExpressionStatement", + "start": 26, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "expression": { + "type": "RegExpLiteral", + "start": 26, + "end": 31, + "loc": { + "start": { + "line": 1, + "column": 26 + }, + "end": { + "line": 1, + "column": 31 + } + }, + "extra": { + "raw": "/42/i" + }, + "pattern": "42", + "flags": "i" + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/06-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/06-regex/actual.js new file mode 100644 index 0000000000..64c6e58163 --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/06-regex/actual.js @@ -0,0 +1 @@ +!function fn() {} /42/i; diff --git a/packages/babylon/test/fixtures/core/categorized/06-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/06-regex/expected.json new file mode 100644 index 0000000000..69b9be072f --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/06-regex/expected.json @@ -0,0 +1,190 @@ +{ + "type": "File", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "sourceType": "script", + "body": [ + { + "type": "ExpressionStatement", + "start": 0, + "end": 24, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "expression": { + "type": "BinaryExpression", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "left": { + "type": "BinaryExpression", + "start": 0, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "left": { + "type": "UnaryExpression", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "operator": "!", + "prefix": true, + "argument": { + "type": "FunctionExpression", + "start": 1, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 10, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "fn" + }, + "name": "fn" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 15, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "body": [], + "directives": [] + } + }, + "extra": { + "parenthesizedArgument": false + } + }, + "operator": "/", + "right": { + "type": "NumericLiteral", + "start": 19, + "end": 21, + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 21 + } + }, + "extra": { + "rawValue": 42, + "raw": "42" + }, + "value": 42 + } + }, + "operator": "/", + "right": { + "type": "Identifier", + "start": 22, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + }, + "identifierName": "i" + }, + "name": "i" + } + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/core/categorized/07-regex/actual.js b/packages/babylon/test/fixtures/core/categorized/07-regex/actual.js new file mode 100644 index 0000000000..ec285bfe1a --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/07-regex/actual.js @@ -0,0 +1 @@ +function foo() {} /42/i diff --git a/packages/babylon/test/fixtures/core/categorized/07-regex/expected.json b/packages/babylon/test/fixtures/core/categorized/07-regex/expected.json new file mode 100644 index 0000000000..f8f814604b --- /dev/null +++ b/packages/babylon/test/fixtures/core/categorized/07-regex/expected.json @@ -0,0 +1,121 @@ +{ + "type": "File", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 12, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + }, + "identifierName": "foo" + }, + "name": "foo" + }, + "generator": false, + "async": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 15, + "end": 17, + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "body": [], + "directives": [] + } + }, + { + "type": "ExpressionStatement", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "expression": { + "type": "RegExpLiteral", + "start": 18, + "end": 23, + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 23 + } + }, + "extra": { + "raw": "/42/i" + }, + "pattern": "42", + "flags": "i" + } + } + ], + "directives": [] + } +} \ No newline at end of file