diff --git a/src/compiler/parser.ts b/src/compiler/parser.ts index 0be7734968fa9..156a6698e2313 100644 --- a/src/compiler/parser.ts +++ b/src/compiler/parser.ts @@ -2214,6 +2214,7 @@ namespace ts { return finishNode(node); } + const startPos = scanner.getStartPos(); node.decorators = parseDecorators(); node.modifiers = parseModifiers(); node.dotDotDotToken = parseOptionalToken(SyntaxKind.DotDotDotToken); @@ -2237,14 +2238,13 @@ namespace ts { node.type = parseParameterType(); node.initializer = parseBindingElementInitializer(/*inParameter*/ true); - // Do not check for initializers in an ambient context for parameters. This is not - // a grammar error because the grammar allows arbitrary call signatures in - // an ambient context. - // It is actually not necessary for this to be an error at all. The reason is that - // function/constructor implementations are syntactically disallowed in ambient - // contexts. In addition, parameter initializers are semantically disallowed in - // overload signatures. So parameter initializers are transitively disallowed in - // ambient contexts. + if (startPos === scanner.getStartPos()) { + // What we're parsing isn't actually remotely recognizable as a parameter and we've consumed no tokens whatsoever + // Consume a token to advance the parser in some way and avoid an infinite loop in `parseDelimitedList` + // This can happen when we're speculatively parsing parenthesized expressions which we think may be arrow functions, + // or when a modifier keyword which is disallowed as a parameter name (ie, `static` in strict mode) is supplied + nextToken(); + } return addJSDocComment(finishNode(node)); } diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineNew.errors.txt b/tests/baselines/reference/parseCommaSeperatedNewlineNew.errors.txt new file mode 100644 index 0000000000000..ff3ff1a033d6c --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineNew.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/parseCommaSeperatedNewlineNew.ts(1,2): error TS2304: Cannot find name 'a'. +tests/cases/compiler/parseCommaSeperatedNewlineNew.ts(1,2): error TS2695: Left side of comma operator is unused and has no side effects. +tests/cases/compiler/parseCommaSeperatedNewlineNew.ts(2,4): error TS1109: Expression expected. + + +==== tests/cases/compiler/parseCommaSeperatedNewlineNew.ts (3 errors) ==== + (a, + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS2695: Left side of comma operator is unused and has no side effects. + new) + ~ +!!! error TS1109: Expression expected. \ No newline at end of file diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineNew.js b/tests/baselines/reference/parseCommaSeperatedNewlineNew.js new file mode 100644 index 0000000000000..c1326c47802a8 --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineNew.js @@ -0,0 +1,7 @@ +//// [parseCommaSeperatedNewlineNew.ts] +(a, +new) + +//// [parseCommaSeperatedNewlineNew.js] +(a, + new ); diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineNumber.errors.txt b/tests/baselines/reference/parseCommaSeperatedNewlineNumber.errors.txt new file mode 100644 index 0000000000000..73f087cc1e680 --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineNumber.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts(1,2): error TS2304: Cannot find name 'a'. +tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts(1,2): error TS2695: Left side of comma operator is unused and has no side effects. + + +==== tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts (2 errors) ==== + (a, + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS2695: Left side of comma operator is unused and has no side effects. + 1) \ No newline at end of file diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineNumber.js b/tests/baselines/reference/parseCommaSeperatedNewlineNumber.js new file mode 100644 index 0000000000000..167e604665a87 --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineNumber.js @@ -0,0 +1,7 @@ +//// [parseCommaSeperatedNewlineNumber.ts] +(a, +1) + +//// [parseCommaSeperatedNewlineNumber.js] +(a, + 1); diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineString.errors.txt b/tests/baselines/reference/parseCommaSeperatedNewlineString.errors.txt new file mode 100644 index 0000000000000..2c70bfbfc3017 --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineString.errors.txt @@ -0,0 +1,11 @@ +tests/cases/compiler/parseCommaSeperatedNewlineString.ts(1,2): error TS2304: Cannot find name 'a'. +tests/cases/compiler/parseCommaSeperatedNewlineString.ts(1,2): error TS2695: Left side of comma operator is unused and has no side effects. + + +==== tests/cases/compiler/parseCommaSeperatedNewlineString.ts (2 errors) ==== + (a, + ~ +!!! error TS2304: Cannot find name 'a'. + ~ +!!! error TS2695: Left side of comma operator is unused and has no side effects. + '') \ No newline at end of file diff --git a/tests/baselines/reference/parseCommaSeperatedNewlineString.js b/tests/baselines/reference/parseCommaSeperatedNewlineString.js new file mode 100644 index 0000000000000..f7f96541c77c8 --- /dev/null +++ b/tests/baselines/reference/parseCommaSeperatedNewlineString.js @@ -0,0 +1,7 @@ +//// [parseCommaSeperatedNewlineString.ts] +(a, +'') + +//// [parseCommaSeperatedNewlineString.js] +(a, + ''); diff --git a/tests/cases/compiler/parseCommaSeperatedNewlineNew.ts b/tests/cases/compiler/parseCommaSeperatedNewlineNew.ts new file mode 100644 index 0000000000000..a0062db6cf119 --- /dev/null +++ b/tests/cases/compiler/parseCommaSeperatedNewlineNew.ts @@ -0,0 +1,2 @@ +(a, +new) \ No newline at end of file diff --git a/tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts b/tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts new file mode 100644 index 0000000000000..cc07845674db9 --- /dev/null +++ b/tests/cases/compiler/parseCommaSeperatedNewlineNumber.ts @@ -0,0 +1,2 @@ +(a, +1) \ No newline at end of file diff --git a/tests/cases/compiler/parseCommaSeperatedNewlineString.ts b/tests/cases/compiler/parseCommaSeperatedNewlineString.ts new file mode 100644 index 0000000000000..e6cff2438a5ae --- /dev/null +++ b/tests/cases/compiler/parseCommaSeperatedNewlineString.ts @@ -0,0 +1,2 @@ +(a, +'') \ No newline at end of file