From 6eb066166ff05c30f6389fcbfd73579bbe91d059 Mon Sep 17 00:00:00 2001 From: kdy1 Date: Tue, 7 Jan 2020 12:36:51 +0000 Subject: [PATCH] Don't emit error messages while trying (#574) swc_ecma_parser: - don't emit error messages while trying (Closes #573) --- ecmascript/parser/src/parser/mod.rs | 7 + ecmascript/parser/src/parser/typescript.rs | 5 + .../typescript/custom/tsx-issue-573/input.tsx | 1 + .../custom/tsx-issue-573/input.tsx.json | 163 ++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx create mode 100644 ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx.json diff --git a/ecmascript/parser/src/parser/mod.rs b/ecmascript/parser/src/parser/mod.rs index f4f928ea458c..170f9e4bc3ea 100644 --- a/ecmascript/parser/src/parser/mod.rs +++ b/ecmascript/parser/src/parser/mod.rs @@ -37,6 +37,8 @@ pub type PResult<'a, T> = Result>; /// EcmaScript parser. #[derive(Clone)] pub struct Parser<'a, I: Tokens> { + /// [false] while backtracking + emit_err: bool, session: Session<'a>, state: State, input: Buffer, @@ -68,6 +70,7 @@ impl<'a, I: Input> Parser<'a, Lexer<'a, I>> { impl<'a, I: Tokens> Parser<'a, I> { pub fn new_from(session: Session<'a>, input: I) -> Self { Parser { + emit_err: true, session, input: Buffer::new(input), state: Default::default(), @@ -153,6 +156,10 @@ impl<'a, I: Tokens> Parser<'a, I> { } fn emit_err(&self, span: Span, error: SyntaxError) { + if !self.emit_err { + return; + } + DiagnosticBuilder::from(ErrorToDiag { handler: self.session.handler, span, diff --git a/ecmascript/parser/src/parser/typescript.rs b/ecmascript/parser/src/parser/typescript.rs index 558db06584aa..7aab1d0836f5 100644 --- a/ecmascript/parser/src/parser/typescript.rs +++ b/ecmascript/parser/src/parser/typescript.rs @@ -465,10 +465,12 @@ impl<'a, I: Tokens> Parser<'a, I> { return Ok(false); } let mut cloned = self.clone(); + cloned.emit_err = false; let res = op(&mut cloned); match res { Ok(Some(res)) if res => { *self = cloned; + self.emit_err = true; Ok(res) } Err(mut err) => { @@ -488,10 +490,12 @@ impl<'a, I: Tokens> Parser<'a, I> { return None; } let mut cloned = self.clone(); + cloned.emit_err = false; let res = op(&mut cloned); match res { Ok(Some(res)) => { *self = cloned; + self.emit_err = true; Some(res) } Ok(None) => None, @@ -1007,6 +1011,7 @@ impl<'a, I: Tokens> Parser<'a, I> { debug_assert!(self.input.syntax().typescript()); let mut cloned = self.clone(); + cloned.emit_err = false; op(&mut cloned) } diff --git a/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx b/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx new file mode 100644 index 000000000000..24366284db6c --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx @@ -0,0 +1 @@ +const fn = (x: T) => x; \ No newline at end of file diff --git a/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx.json b/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx.json new file mode 100644 index 000000000000..78275f874a69 --- /dev/null +++ b/ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx.json @@ -0,0 +1,163 @@ +{ + "type": "Module", + "span": { + "start": 0, + "end": 50, + "ctxt": 0 + }, + "body": [ + { + "type": "VariableDeclaration", + "span": { + "start": 0, + "end": 50, + "ctxt": 0 + }, + "kind": "const", + "declare": false, + "declarations": [ + { + "type": "VariableDeclarator", + "span": { + "start": 6, + "end": 49, + "ctxt": 0 + }, + "id": { + "type": "Identifier", + "span": { + "start": 6, + "end": 8, + "ctxt": 0 + }, + "value": "fn", + "typeAnnotation": null, + "optional": false + }, + "init": { + "type": "ArrowFunctionExpression", + "span": { + "start": 38, + "end": 49, + "ctxt": 0 + }, + "params": [ + { + "type": "Identifier", + "span": { + "start": 39, + "end": 43, + "ctxt": 0 + }, + "value": "x", + "typeAnnotation": { + "type": "TsTypeAnnotation", + "span": { + "start": 40, + "end": 43, + "ctxt": 0 + }, + "typeAnnotation": { + "type": "TsTypeReference", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "typeName": { + "type": "Identifier", + "span": { + "start": 42, + "end": 43, + "ctxt": 0 + }, + "value": "T", + "typeAnnotation": null, + "optional": false + }, + "typeParams": null + } + }, + "optional": false + } + ], + "body": { + "type": "Identifier", + "span": { + "start": 48, + "end": 49, + "ctxt": 0 + }, + "value": "x", + "typeAnnotation": null, + "optional": false + }, + "async": false, + "generator": false, + "typeParameters": { + "type": "TsTypeParameterDeclaration", + "span": { + "start": 11, + "end": 38, + "ctxt": 0 + }, + "parameters": [ + { + "type": "TsTypeParameter", + "span": { + "start": 12, + "end": 37, + "ctxt": 0 + }, + "name": { + "type": "Identifier", + "span": { + "start": 12, + "end": 13, + "ctxt": 0 + }, + "value": "T", + "typeAnnotation": null, + "optional": false + }, + "constraint": { + "type": "TsUnionType", + "span": { + "start": 22, + "end": 37, + "ctxt": 0 + }, + "types": [ + { + "type": "TsKeywordType", + "span": { + "start": 22, + "end": 28, + "ctxt": 0 + }, + "kind": "string" + }, + { + "type": "TsKeywordType", + "span": { + "start": 31, + "end": 37, + "ctxt": 0 + }, + "kind": "number" + } + ] + }, + "default": null + } + ] + }, + "returnType": null + }, + "definite": false + } + ] + } + ], + "interpreter": null +}