From 2814b1d9c29c830fcb20e5b871b391e14174debf Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Mon, 6 Apr 2020 08:17:31 +0800 Subject: [PATCH] fix failed test --- src/compiler/parse/read/context.ts | 21 +++++-- .../each-block-destructured/output.json | 58 +++++++++++++++++-- .../errors.json | 2 +- .../errors.json | 4 +- .../errors.json | 4 +- 5 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/compiler/parse/read/context.ts b/src/compiler/parse/read/context.ts index f651135691..a11c6933c0 100644 --- a/src/compiler/parse/read/context.ts +++ b/src/compiler/parse/read/context.ts @@ -10,13 +10,20 @@ import { import { parse_expression_at } from "../acorn"; import { Pattern } from "estree"; -export default function read_context(parser: Parser): Pattern { +export default function read_context( + parser: Parser +): Pattern & { start: number; end: number } { const start = parser.index; let i = parser.index; const code = full_char_code_at(parser.template, i); if (isIdentifierStart(code, true)) { - return { type: "Identifier", name: parser.read_identifier() }; + return { + type: "Identifier", + name: parser.read_identifier(), + start, + end: parser.index + }; } if (!is_bracket_open(code)) { @@ -54,6 +61,12 @@ export default function read_context(parser: Parser): Pattern { parser.index = i; const pattern_string = parser.template.slice(start, i); - return (parse_expression_at(`${' '.repeat(start - 1)}(${pattern_string} = 1)`, start - 1) as any) - .left as Pattern; + try { + return (parse_expression_at( + `${" ".repeat(start - 1)}(${pattern_string} = 1)`, + start - 1 + ) as any).left; + } catch (error) { + parser.acorn_error(error); + } } diff --git a/test/parser/samples/each-block-destructured/output.json b/test/parser/samples/each-block-destructured/output.json index ce280cb296..ce263226f1 100644 --- a/test/parser/samples/each-block-destructured/output.json +++ b/test/parser/samples/each-block-destructured/output.json @@ -85,30 +85,80 @@ } ], "context": { + "type": "ArrayPattern", "start": 18, "end": 39, - "type": "ArrayPattern", + "loc": { + "start": { + "line": 1, + "column": 18 + }, + "end": { + "line": 1, + "column": 39 + } + }, "elements": [ { + "type": "Identifier", "start": 19, "end": 22, - "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 19 + }, + "end": { + "line": 1, + "column": 22 + } + }, "name": "key" }, { + "type": "Identifier", "start": 24, "end": 29, - "type": "Identifier", + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 29 + } + }, "name": "value" }, { + "type": "RestElement", "start": 31, "end": 38, - "type": "RestElement", + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 38 + } + }, "argument": { "type": "Identifier", "start": 34, "end": 38, + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 38 + } + }, "name": "rest" } } diff --git a/test/validator/samples/each-block-destructured-object-rest-comma-after/errors.json b/test/validator/samples/each-block-destructured-object-rest-comma-after/errors.json index 549b6960eb..df899b7702 100644 --- a/test/validator/samples/each-block-destructured-object-rest-comma-after/errors.json +++ b/test/validator/samples/each-block-destructured-object-rest-comma-after/errors.json @@ -1,5 +1,5 @@ [{ - "code": "comma-after-rest", + "code": "parse-error", "message": "Comma is not permitted after the rest element", "pos": 100, "start": { diff --git a/test/validator/samples/each-block-invalid-context-destructured-object/errors.json b/test/validator/samples/each-block-invalid-context-destructured-object/errors.json index 085021ff5a..c96e3d2c8c 100644 --- a/test/validator/samples/each-block-invalid-context-destructured-object/errors.json +++ b/test/validator/samples/each-block-invalid-context-destructured-object/errors.json @@ -1,6 +1,6 @@ [{ - "code": "unexpected-reserved-word", - "message": "'case' is a reserved word in JavaScript and cannot be used here", + "code": "parse-error", + "message": "Unexpected keyword 'case'", "start": { "line": 1, "column": 18, diff --git a/test/validator/samples/each-block-invalid-context-destructured/errors.json b/test/validator/samples/each-block-invalid-context-destructured/errors.json index afe99ee219..62d6f62e87 100644 --- a/test/validator/samples/each-block-invalid-context-destructured/errors.json +++ b/test/validator/samples/each-block-invalid-context-destructured/errors.json @@ -1,6 +1,6 @@ [{ - "code": "unexpected-reserved-word", - "message": "'case' is a reserved word in JavaScript and cannot be used here", + "code": "parse-error", + "message": "Unexpected token", "start": { "line": 1, "column": 17,