Skip to content

Commit

Permalink
Don't emit error messages while trying (#574)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - don't emit error messages while trying (Closes #573)
  • Loading branch information
kdy1 committed Jan 7, 2020
1 parent 02d4fb6 commit 6eb0661
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ecmascript/parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub type PResult<'a, T> = Result<T, DiagnosticBuilder<'a>>;
/// EcmaScript parser.
#[derive(Clone)]
pub struct Parser<'a, I: Tokens> {
/// [false] while backtracking
emit_err: bool,
session: Session<'a>,
state: State,
input: Buffer<I>,
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions ecmascript/parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const fn = <T extends string | number>(x: T) => x;
163 changes: 163 additions & 0 deletions ecmascript/parser/tests/typescript/custom/tsx-issue-573/input.tsx.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6eb0661

Please sign in to comment.