From fd2c88c8fadd3f8e00f09e2bc7307a0f8797c005 Mon Sep 17 00:00:00 2001 From: yosuke ota Date: Thu, 18 Nov 2021 13:32:24 +0900 Subject: [PATCH] Fix parse error for paren. --- src/parser/parser.ts | 1 + .../fixtures/parser/ast/paren-01-input.jsonx | 1 + .../fixtures/parser/ast/paren-01-output.json | 112 ++++++++++++++++++ tests/src/parser/errors.ts | 8 ++ 4 files changed, 122 insertions(+) create mode 100644 tests/fixtures/parser/ast/paren-01-input.jsonx create mode 100644 tests/fixtures/parser/ast/paren-01-output.json diff --git a/src/parser/parser.ts b/src/parser/parser.ts index 118ba44..68cbe2d 100644 --- a/src/parser/parser.ts +++ b/src/parser/parser.ts @@ -60,6 +60,7 @@ export function parseForESLint( const ast = convertProgramNode(baseAst as never, tokenStore, ctx, code) let lastIndex = Math.max( baseAst.range![1], + tokens[tokens.length - 1]?.range[1] ?? 0, comments[comments.length - 1]?.range![1] ?? 0, ) let lastChar = code[lastIndex] diff --git a/tests/fixtures/parser/ast/paren-01-input.jsonx b/tests/fixtures/parser/ast/paren-01-input.jsonx new file mode 100644 index 0000000..93022ef --- /dev/null +++ b/tests/fixtures/parser/ast/paren-01-input.jsonx @@ -0,0 +1 @@ +(42) \ No newline at end of file diff --git a/tests/fixtures/parser/ast/paren-01-output.json b/tests/fixtures/parser/ast/paren-01-output.json new file mode 100644 index 0000000..14d3ce9 --- /dev/null +++ b/tests/fixtures/parser/ast/paren-01-output.json @@ -0,0 +1,112 @@ +{ + "type": "Program", + "body": [ + { + "type": "JSONExpressionStatement", + "expression": { + "type": "JSONLiteral", + "value": 42, + "raw": "42", + "range": [ + 1, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "range": [ + 1, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "comments": [], + "tokens": [ + { + "type": "Punctuator", + "value": "(", + "range": [ + 0, + 1 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "Numeric", + "value": "42", + "range": [ + 1, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "range": [ + 3, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 4 + } + } + } + ], + "range": [ + 0, + 4 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 4 + } + } +} \ No newline at end of file diff --git a/tests/src/parser/errors.ts b/tests/src/parser/errors.ts index 08d65be..f7cf783 100644 --- a/tests/src/parser/errors.ts +++ b/tests/src/parser/errors.ts @@ -327,6 +327,14 @@ typeof 123 index: 4, char: "i", }, + { + code: "(42)-", + message: "Unexpected token '-'.", + lineNumber: 1, + column: 5, + index: 4, + char: "-", + }, ]) { it(`parseForESLint error on ${JSON.stringify(code)}`, () => { const e = getParseError(code)