diff --git a/internal/parser/parser.go b/internal/parser/parser.go index 4f2ea48ffb..8ced9412b8 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -261,6 +261,7 @@ type ParserState struct { scannerState scanner.ScannerState contextFlags ast.NodeFlags diagnosticsLen int + jsDiagnosticsLen int statementHasAwaitIdentifier bool hasParseError bool } @@ -270,6 +271,7 @@ func (p *Parser) mark() ParserState { scannerState: p.scanner.Mark(), contextFlags: p.contextFlags, diagnosticsLen: len(p.diagnostics), + jsDiagnosticsLen: len(p.jsDiagnostics), statementHasAwaitIdentifier: p.statementHasAwaitIdentifier, hasParseError: p.hasParseError, } @@ -280,6 +282,7 @@ func (p *Parser) rewind(state ParserState) { p.token = p.scanner.Token() p.contextFlags = state.contextFlags p.diagnostics = p.diagnostics[0:state.diagnosticsLen] + p.jsDiagnostics = p.jsDiagnostics[0:state.jsDiagnosticsLen] p.statementHasAwaitIdentifier = state.statementHasAwaitIdentifier p.hasParseError = state.hasParseError } diff --git a/testdata/baselines/reference/compiler/jsSpeculativeParsingError.symbols b/testdata/baselines/reference/compiler/jsSpeculativeParsingError.symbols new file mode 100644 index 0000000000..f5840192c2 --- /dev/null +++ b/testdata/baselines/reference/compiler/jsSpeculativeParsingError.symbols @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/jsSpeculativeParsingError.ts] //// + +=== t.js === +const is_morning = new Date().getHours() < 12; +>is_morning : Symbol(is_morning, Decl(t.js, 0, 5)) +>new Date().getHours : Symbol(Date.getHours, Decl(lib.es5.d.ts, --, --)) +>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --)) +>getHours : Symbol(Date.getHours, Decl(lib.es5.d.ts, --, --)) + +// prettier-ignore +const greeting = ([ +>greeting : Symbol(greeting, Decl(t.js, 3, 5)) + + is_morning ? 'good morning' : 'good evening' +>is_morning : Symbol(is_morning, Decl(t.js, 0, 5)) + +]); + diff --git a/testdata/baselines/reference/compiler/jsSpeculativeParsingError.types b/testdata/baselines/reference/compiler/jsSpeculativeParsingError.types new file mode 100644 index 0000000000..cae4c55f58 --- /dev/null +++ b/testdata/baselines/reference/compiler/jsSpeculativeParsingError.types @@ -0,0 +1,27 @@ +//// [tests/cases/compiler/jsSpeculativeParsingError.ts] //// + +=== t.js === +const is_morning = new Date().getHours() < 12; +>is_morning : boolean +>new Date().getHours() < 12 : boolean +>new Date().getHours() : number +>new Date().getHours : () => number +>new Date() : Date +>Date : DateConstructor +>getHours : () => number +>12 : 12 + +// prettier-ignore +const greeting = ([ +>greeting : string[] +>([ is_morning ? 'good morning' : 'good evening']) : string[] +>[ is_morning ? 'good morning' : 'good evening'] : string[] + + is_morning ? 'good morning' : 'good evening' +>is_morning ? 'good morning' : 'good evening' : "good evening" | "good morning" +>is_morning : boolean +>'good morning' : "good morning" +>'good evening' : "good evening" + +]); + diff --git a/testdata/tests/cases/compiler/jsSpeculativeParsingError.ts b/testdata/tests/cases/compiler/jsSpeculativeParsingError.ts new file mode 100644 index 0000000000..3fd91571fd --- /dev/null +++ b/testdata/tests/cases/compiler/jsSpeculativeParsingError.ts @@ -0,0 +1,12 @@ +// @strict: true +// @allowJs: true +// @checkJs: true +// @noEmit: true +// @filename: t.js + +const is_morning = new Date().getHours() < 12; + +// prettier-ignore +const greeting = ([ + is_morning ? 'good morning' : 'good evening' +]);