Skip to content

Always require '=' before parsing an initializer #19979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Nov 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 12 additions & 43 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2271,7 +2271,7 @@ namespace ts {
isStartOfType(/*inStartOfParameter*/ true);
}

function parseParameter(requireEqualsToken?: boolean): ParameterDeclaration {
function parseParameter(): ParameterDeclaration {
const node = <ParameterDeclaration>createNode(SyntaxKind.Parameter);
if (token() === SyntaxKind.ThisKeyword) {
node.name = createIdentifier(/*isIdentifier*/ true);
Expand Down Expand Up @@ -2300,7 +2300,7 @@ namespace ts {

node.questionToken = parseOptionalToken(SyntaxKind.QuestionToken);
node.type = parseParameterType();
node.initializer = parseInitializer(/*inParameter*/ true, requireEqualsToken);
node.initializer = parseInitializer();

return addJSDocComment(finishNode(node));
}
Expand Down Expand Up @@ -2357,8 +2357,7 @@ namespace ts {
setYieldContext(!!(flags & SignatureFlags.Yield));
setAwaitContext(!!(flags & SignatureFlags.Await));

const result = parseDelimitedList(ParsingContext.Parameters,
flags & SignatureFlags.JSDoc ? parseJSDocParameter : () => parseParameter(!!(flags & SignatureFlags.RequireCompleteParameterList)));
const result = parseDelimitedList(ParsingContext.Parameters, flags & SignatureFlags.JSDoc ? parseJSDocParameter : parseParameter);

setYieldContext(savedYieldContext);
setAwaitContext(savedAwaitContext);
Expand Down Expand Up @@ -2499,7 +2498,7 @@ namespace ts {
// Although type literal properties cannot not have initializers, we attempt
// to parse an initializer so we can report in the checker that an interface
// property or type literal property cannot have an initializer.
property.initializer = parseNonParameterInitializer();
property.initializer = parseInitializer();
}

parseTypeMemberSemicolon();
Expand Down Expand Up @@ -3039,34 +3038,8 @@ namespace ts {
return expr;
}

function parseInitializer(inParameter: boolean, requireEqualsToken?: boolean): Expression {
if (token() !== SyntaxKind.EqualsToken) {
// It's not uncommon during typing for the user to miss writing the '=' token. Check if
// there is no newline after the last token and if we're on an expression. If so, parse
// this as an equals-value clause with a missing equals.
// NOTE: There are two places where we allow equals-value clauses. The first is in a
// variable declarator. The second is with a parameter. For variable declarators
// it's more likely that a { would be a allowed (as an object literal). While this
// is also allowed for parameters, the risk is that we consume the { as an object
// literal when it really will be for the block following the parameter.
if (scanner.hasPrecedingLineBreak() || (inParameter && token() === SyntaxKind.OpenBraceToken) || !isStartOfExpression()) {
// preceding line break, open brace in a parameter (likely a function body) or current token is not an expression -
// do not try to parse initializer
return undefined;
}
if (inParameter && requireEqualsToken) {
// = is required when speculatively parsing arrow function parameters,
// so return a fake initializer as a signal that the equals token was missing
const result = createMissingNode(SyntaxKind.Identifier, /*reportAtCurrentPosition*/ true, Diagnostics._0_expected, "=") as Identifier;
result.escapedText = "= not found" as __String;
return result;
}
}

// Initializer[In, Yield] :
// = AssignmentExpression[?In, ?Yield]
parseExpected(SyntaxKind.EqualsToken);
return parseAssignmentExpressionOrHigher();
function parseInitializer(): Expression | undefined {
return parseOptional(SyntaxKind.EqualsToken) ? parseAssignmentExpressionOrHigher() : undefined;
}

function parseAssignmentExpressionOrHigher(): Expression {
Expand Down Expand Up @@ -5234,7 +5207,7 @@ namespace ts {
const node = <BindingElement>createNode(SyntaxKind.BindingElement);
node.dotDotDotToken = parseOptionalToken(SyntaxKind.DotDotDotToken);
node.name = parseIdentifierOrPattern();
node.initializer = parseInitializer(/*inParameter*/ false);
node.initializer = parseInitializer();
return finishNode(node);
}

Expand All @@ -5251,7 +5224,7 @@ namespace ts {
node.propertyName = propertyName;
node.name = parseIdentifierOrPattern();
}
node.initializer = parseInitializer(/*inParameter*/ false);
node.initializer = parseInitializer();
return finishNode(node);
}

Expand Down Expand Up @@ -5290,7 +5263,7 @@ namespace ts {
node.name = parseIdentifierOrPattern();
node.type = parseTypeAnnotation();
if (!isInOrOfKeyword(token())) {
node.initializer = parseNonParameterInitializer();
node.initializer = parseInitializer();
}
return finishNode(node);
}
Expand Down Expand Up @@ -5406,8 +5379,8 @@ namespace ts {
//
// The checker may still error in the static case to explicitly disallow the yield expression.
property.initializer = hasModifier(property, ModifierFlags.Static)
? allowInAnd(parseNonParameterInitializer)
: doOutsideOfContext(NodeFlags.YieldContext | NodeFlags.DisallowInContext, parseNonParameterInitializer);
? allowInAnd(parseInitializer)
: doOutsideOfContext(NodeFlags.YieldContext | NodeFlags.DisallowInContext, parseInitializer);

parseSemicolon();
return addJSDocComment(finishNode(property));
Expand All @@ -5428,10 +5401,6 @@ namespace ts {
}
}

function parseNonParameterInitializer() {
return parseInitializer(/*inParameter*/ false);
}

function parseAccessorDeclaration(kind: SyntaxKind, fullStart: number, decorators: NodeArray<Decorator>, modifiers: NodeArray<Modifier>): AccessorDeclaration {
const node = <AccessorDeclaration>createNode(kind, fullStart);
node.decorators = decorators;
Expand Down Expand Up @@ -5755,7 +5724,7 @@ namespace ts {
function parseEnumMember(): EnumMember {
const node = <EnumMember>createNode(SyntaxKind.EnumMember, scanner.getStartPos());
node.name = parsePropertyName();
node.initializer = allowInAnd(parseNonParameterInitializer);
node.initializer = allowInAnd(parseInitializer);
return addJSDocComment(finishNode(node));
}

Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ClassDeclaration26.errors.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
tests/cases/compiler/ClassDeclaration26.ts(2,22): error TS1005: ';' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,5): error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
tests/cases/compiler/ClassDeclaration26.ts(4,20): error TS1005: '=' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,20): error TS1005: ',' expected.
tests/cases/compiler/ClassDeclaration26.ts(4,23): error TS1005: '=>' expected.
tests/cases/compiler/ClassDeclaration26.ts(5,1): error TS1128: Declaration or statement expected.

Expand All @@ -15,7 +15,7 @@ tests/cases/compiler/ClassDeclaration26.ts(5,1): error TS1128: Declaration or st
~~~
!!! error TS1068: Unexpected token. A constructor, method, accessor, or property was expected.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~
!!! error TS1005: '=>' expected.
}
Expand Down
3 changes: 2 additions & 1 deletion tests/baselines/reference/ClassDeclaration26.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var C = /** @class */ (function () {
}
return C;
}());
var constructor = function () { };
var constructor;
(function () { });
2 changes: 1 addition & 1 deletion tests/baselines/reference/ClassDeclaration26.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class C {
>10 : 10

var constructor() { }
>constructor : () => void
>constructor : any
>() { } : () => void
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration12_es6.ts(1,20): error TS1005: '(' expected.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration12_es6.ts(1,25): error TS1005: '=' expected.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration12_es6.ts(1,25): error TS1005: ',' expected.
tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration12_es6.ts(1,28): error TS1005: '=>' expected.


Expand All @@ -8,6 +8,6 @@ tests/cases/conformance/es6/functionDeclarations/FunctionDeclaration12_es6.ts(1,
~~~~~
!!! error TS1005: '(' expected.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~
!!! error TS1005: '=>' expected.
3 changes: 2 additions & 1 deletion tests/baselines/reference/FunctionDeclaration12_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
var v = function * yield() { }

//// [FunctionDeclaration12_es6.js]
var v = function* () { }, yield = () => { };
var v = function* () { }, yield;
() => { };
2 changes: 1 addition & 1 deletion tests/baselines/reference/FunctionDeclaration12_es6.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
var v = function * yield() { }
>v : () => any
>function * : () => any
>yield : () => void
>yield : any
>() { } : () => void

13 changes: 5 additions & 8 deletions tests/baselines/reference/VariableDeclaration13_es6.errors.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,5): error TS1181: Array element destructuring pattern expected.
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,6): error TS1005: ',' expected.
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,8): error TS1134: Variable declaration expected.
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,10): error TS1134: Variable declaration expected.
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,6): error TS1005: ';' expected.
tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts(4,8): error TS1128: Declaration or statement expected.


==== tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts (4 errors) ====
==== tests/cases/conformance/es6/variableDeclarations/VariableDeclaration13_es6.ts (3 errors) ====
// An ExpressionStatement cannot start with the two token sequence `let [` because
// that would make it ambiguous with a `let` LexicalDeclaration whose first LexicalBinding was an ArrayBindingPattern.
var let: any;
let[0] = 100;
~
!!! error TS1181: Array element destructuring pattern expected.
~
!!! error TS1005: ',' expected.
!!! error TS1005: ';' expected.
~
!!! error TS1134: Variable declaration expected.
~~~
!!! error TS1134: Variable declaration expected.
!!! error TS1128: Declaration or statement expected.
3 changes: 2 additions & 1 deletion tests/baselines/reference/VariableDeclaration13_es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ let[0] = 100;
// An ExpressionStatement cannot start with the two token sequence `let [` because
// that would make it ambiguous with a `let` LexicalDeclaration whose first LexicalBinding was an ArrayBindingPattern.
var let;
let [] = 0;
let [];
0;
100;
16 changes: 5 additions & 11 deletions tests/baselines/reference/arrayTypeOfTypeOf.errors.txt
Original file line number Diff line number Diff line change
@@ -1,28 +1,22 @@
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(6,5): error TS2322: Type 'number' is not assignable to type 'ArrayConstructor'.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(6,22): error TS1005: '=' expected.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(6,22): error TS1005: ',' expected.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(6,30): error TS1109: Expression expected.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(7,5): error TS2322: Type 'number' is not assignable to type 'ArrayConstructor'.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(7,22): error TS1005: '=' expected.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(7,22): error TS1005: ',' expected.
tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts(7,32): error TS1109: Expression expected.


==== tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts (6 errors) ====
==== tests/cases/conformance/types/specifyingTypes/typeLiterals/arrayTypeOfTypeOf.ts (4 errors) ====
// array type cannot use typeof.

var x = 1;
var xs: typeof x[]; // Not an error. This is equivalent to Array<typeof x>
var xs2: typeof Array;
var xs3: typeof Array<number>;
~~~
!!! error TS2322: Type 'number' is not assignable to type 'ArrayConstructor'.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~
!!! error TS1109: Expression expected.
var xs4: typeof Array<typeof x>;
~~~
!!! error TS2322: Type 'number' is not assignable to type 'ArrayConstructor'.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~
!!! error TS1109: Expression expected.
6 changes: 4 additions & 2 deletions tests/baselines/reference/arrayTypeOfTypeOf.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ var xs4: typeof Array<typeof x>;
var x = 1;
var xs; // Not an error. This is equivalent to Array<typeof x>
var xs2;
var xs3 = ;
var xs4 = ;
var xs3;
;
var xs4;
;
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,18): error TS2304: Cannot find name 'await'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,24): error TS1005: ',' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,33): error TS1005: '=' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'any'.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,33): error TS1005: ',' expected.
tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es2017.ts(1,40): error TS1109: Expression expected.


Expand All @@ -15,9 +15,9 @@ tests/cases/conformance/async/es2017/asyncArrowFunction/asyncArrowFunction5_es20
~
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'any'.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~~
!!! error TS1109: Expression expected.
}
3 changes: 2 additions & 1 deletion tests/baselines/reference/asyncArrowFunction5_es2017.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var foo = async (await): Promise<void> => {
}

//// [asyncArrowFunction5_es2017.js]
var foo = async(await), Promise = ;
var foo = async(await), Promise;
;
{
}
8 changes: 4 additions & 4 deletions tests/baselines/reference/asyncArrowFunction5_es5.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,11): error TS2304: Cannot find name 'async'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,18): error TS2304: Cannot find name 'await'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,24): error TS1005: ',' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,33): error TS1005: '=' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,26): error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'any'.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,33): error TS1005: ',' expected.
tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(1,40): error TS1109: Expression expected.


Expand All @@ -15,9 +15,9 @@ tests/cases/conformance/async/es5/asyncArrowFunction/asyncArrowFunction5_es5.ts(
~
!!! error TS1005: ',' expected.
~~~~~~~
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'void'.
!!! error TS2403: Subsequent variable declarations must have the same type. Variable 'Promise' has type 'PromiseConstructor' at lib.es2015.promise.d.ts 222:12, but here has type 'any'.
~
!!! error TS1005: '=' expected.
!!! error TS1005: ',' expected.
~~
!!! error TS1109: Expression expected.
}
3 changes: 2 additions & 1 deletion tests/baselines/reference/asyncArrowFunction5_es5.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var foo = async (await): Promise<void> => {
}

//// [asyncArrowFunction5_es5.js]
var foo = async(await), Promise = ;
var foo = async(await), Promise;
;
{
}
Loading