diff --git a/packages/babylon/src/tokenizer/context.js b/packages/babylon/src/tokenizer/context.js index ef44998c8d..7b7236e500 100644 --- a/packages/babylon/src/tokenizer/context.js +++ b/packages/babylon/src/tokenizer/context.js @@ -5,6 +5,7 @@ // See https://github.com/mozilla/sweet.js/wiki/design import { types as tt } from "./types"; +import { lineBreak } from "../util/whitespace"; export class TokContext { constructor( @@ -54,6 +55,14 @@ tt.parenR.updateContext = tt.braceR.updateContext = function () { } }; +tt.name.updateContext = function (prevType) { + if (prevType === tt._let || prevType === tt._const || prevType === tt._var) { + if (lineBreak.test(this.input.slice(this.state.end))) { + this.state.exprAllowed = true; + } + } +}; + tt.braceL.updateContext = function (prevType) { this.state.context.push(this.braceIsBlock(prevType) ? types.b_stat : types.b_expr); this.state.exprAllowed = true; diff --git a/packages/babylon/test/fixtures/core/regression/2591/actual.js b/packages/babylon/test/fixtures/core/regression/2591/actual.js new file mode 100644 index 0000000000..2199165df2 --- /dev/null +++ b/packages/babylon/test/fixtures/core/regression/2591/actual.js @@ -0,0 +1,4 @@ +function x() { + let x + /wow/; +} diff --git a/packages/babylon/test/fixtures/core/regression/2591/expected.json b/packages/babylon/test/fixtures/core/regression/2591/expected.json new file mode 100644 index 0000000000..f7fa0d0dd4 --- /dev/null +++ b/packages/babylon/test/fixtures/core/regression/2591/expected.json @@ -0,0 +1,171 @@ +{ + "type": "File", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "x" + }, + "generator": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 13, + "end": 33, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 17, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start": 25, + "end": 31, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 8 + } + }, + "expression": { + "type": "RegExpLiteral", + "start": 25, + "end": 30, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 7 + } + }, + "extra": { + "raw": "/wow/" + }, + "pattern": "wow", + "flags": "" + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file diff --git a/packages/babylon/test/fixtures/jsx/basic/asi/actual.js b/packages/babylon/test/fixtures/jsx/basic/asi/actual.js new file mode 100644 index 0000000000..3157a3aff4 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/basic/asi/actual.js @@ -0,0 +1,4 @@ +function x() { + let x +
+} diff --git a/packages/babylon/test/fixtures/jsx/basic/asi/expected.json b/packages/babylon/test/fixtures/jsx/basic/asi/expected.json new file mode 100644 index 0000000000..73e695d2a6 --- /dev/null +++ b/packages/babylon/test/fixtures/jsx/basic/asi/expected.json @@ -0,0 +1,201 @@ +{ + "type": "File", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "program": { + "type": "Program", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "sourceType": "script", + "body": [ + { + "type": "FunctionDeclaration", + "start": 0, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "id": { + "type": "Identifier", + "start": 9, + "end": 10, + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "x" + }, + "generator": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "start": 13, + "end": 34, + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 4, + "column": 1 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "start": 17, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 2 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "id": { + "type": "Identifier", + "start": 21, + "end": 22, + "loc": { + "start": { + "line": 2, + "column": 6 + }, + "end": { + "line": 2, + "column": 7 + } + }, + "name": "x" + }, + "init": null + } + ], + "kind": "let" + }, + { + "type": "ExpressionStatement", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "expression": { + "type": "JSXElement", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "openingElement": { + "type": "JSXOpeningElement", + "start": 25, + "end": 32, + "loc": { + "start": { + "line": 3, + "column": 2 + }, + "end": { + "line": 3, + "column": 9 + } + }, + "attributes": [], + "name": { + "type": "JSXIdentifier", + "start": 26, + "end": 29, + "loc": { + "start": { + "line": 3, + "column": 3 + }, + "end": { + "line": 3, + "column": 6 + } + }, + "name": "div" + }, + "selfClosing": true + }, + "closingElement": null, + "children": [] + } + } + ], + "directives": [] + } + } + ], + "directives": [] + } +} \ No newline at end of file