Skip to content

Commit ad98fad

Browse files
Merge pull request #2283 from caitp/issue-2282
Disallow line terminator after arrow function parameters, before =>
2 parents a02f85d + 02d3568 commit ad98fad

16 files changed

+656
-205
lines changed

src/compiler/checker.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ module ts {
448448
let declaration = forEach(result.declarations, d => isBlockOrCatchScoped(d) ? d : undefined);
449449

450450
Debug.assert(declaration !== undefined, "Block-scoped variable declaration is undefined");
451-
451+
452452
// first check if usage is lexically located after the declaration
453453
let isUsedBeforeDeclaration = !isDefinedBefore(declaration, errorLocation);
454454
if (!isUsedBeforeDeclaration) {
@@ -465,7 +465,7 @@ module ts {
465465

466466
if (variableDeclaration.parent.parent.kind === SyntaxKind.VariableStatement ||
467467
variableDeclaration.parent.parent.kind === SyntaxKind.ForStatement) {
468-
// variable statement/for statement case,
468+
// variable statement/for statement case,
469469
// use site should not be inside variable declaration (initializer of declaration or binding element)
470470
isUsedBeforeDeclaration = isSameScopeDescendentOf(errorLocation, variableDeclaration, container);
471471
}
@@ -4412,7 +4412,7 @@ module ts {
44124412
}
44134413

44144414
/**
4415-
* Check if a Type was written as a tuple type literal.
4415+
* Check if a Type was written as a tuple type literal.
44164416
* Prefer using isTupleLikeType() unless the use of `elementTypes` is required.
44174417
*/
44184418
function isTupleType(type: Type) : boolean {
@@ -9092,7 +9092,7 @@ module ts {
90929092
*/
90939093
function checkElementTypeOfArrayOrString(arrayOrStringType: Type, expressionForError: Expression): Type {
90949094
Debug.assert(languageVersion < ScriptTarget.ES6);
9095-
9095+
90969096
// After we remove all types that are StringLike, we will know if there was a string constituent
90979097
// based on whether the remaining type is the same as the initial type.
90989098
let arrayType = removeTypesFromUnionType(arrayOrStringType, TypeFlags.StringLike, /*isTypeOfKind*/ true, /*allowEmptyUnionResult*/ true);
@@ -11336,16 +11336,15 @@ module ts {
1133611336
}
1133711337
}
1133811338

11339-
function checkGrammarTypeParameterList(node: FunctionLikeDeclaration, typeParameters: NodeArray<TypeParameterDeclaration>): boolean {
11339+
function checkGrammarTypeParameterList(node: FunctionLikeDeclaration, typeParameters: NodeArray<TypeParameterDeclaration>, file: SourceFile): boolean {
1134011340
if (checkGrammarForDisallowedTrailingComma(typeParameters)) {
1134111341
return true;
1134211342
}
1134311343

1134411344
if (typeParameters && typeParameters.length === 0) {
1134511345
let start = typeParameters.pos - "<".length;
11346-
let sourceFile = getSourceFileOfNode(node);
11347-
let end = skipTrivia(sourceFile.text, typeParameters.end) + ">".length;
11348-
return grammarErrorAtPos(sourceFile, start, end - start, Diagnostics.Type_parameter_list_cannot_be_empty);
11346+
let end = skipTrivia(file.text, typeParameters.end) + ">".length;
11347+
return grammarErrorAtPos(file, start, end - start, Diagnostics.Type_parameter_list_cannot_be_empty);
1134911348
}
1135011349
}
1135111350

@@ -11389,7 +11388,21 @@ module ts {
1138911388

1139011389
function checkGrammarFunctionLikeDeclaration(node: FunctionLikeDeclaration): boolean {
1139111390
// Prevent cascading error by short-circuit
11392-
return checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters) || checkGrammarParameterList(node.parameters);
11391+
let file = getSourceFileOfNode(node);
11392+
return checkGrammarModifiers(node) || checkGrammarTypeParameterList(node, node.typeParameters, file) ||
11393+
checkGrammarParameterList(node.parameters) || checkGrammarArrowFunction(node, file);
11394+
}
11395+
11396+
function checkGrammarArrowFunction(node: FunctionLikeDeclaration, file: SourceFile): boolean {
11397+
if (node.kind === SyntaxKind.ArrowFunction) {
11398+
let arrowFunction = <ArrowFunction>node;
11399+
let startLine = getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.pos).line;
11400+
let endLine = getLineAndCharacterOfPosition(file, arrowFunction.equalsGreaterThanToken.end).line;
11401+
if (startLine !== endLine) {
11402+
return grammarErrorOnNode(arrowFunction.equalsGreaterThanToken, Diagnostics.Line_terminator_not_permitted_before_arrow);
11403+
}
11404+
}
11405+
return false;
1139311406
}
1139411407

1139511408
function checkGrammarIndexSignatureParameters(node: SignatureDeclaration): boolean {

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ module ts {
157157
Catch_clause_variable_cannot_have_an_initializer: { code: 1197, category: DiagnosticCategory.Error, key: "Catch clause variable cannot have an initializer." },
158158
An_extended_Unicode_escape_value_must_be_between_0x0_and_0x10FFFF_inclusive: { code: 1198, category: DiagnosticCategory.Error, key: "An extended Unicode escape value must be between 0x0 and 0x10FFFF inclusive." },
159159
Unterminated_Unicode_escape_sequence: { code: 1199, category: DiagnosticCategory.Error, key: "Unterminated Unicode escape sequence." },
160+
Line_terminator_not_permitted_before_arrow: { code: 1200, category: DiagnosticCategory.Error, key: "Line terminator not permitted before arrow." },
160161
Duplicate_identifier_0: { code: 2300, category: DiagnosticCategory.Error, key: "Duplicate identifier '{0}'." },
161162
Initializer_of_instance_member_variable_0_cannot_reference_identifier_1_declared_in_the_constructor: { code: 2301, category: DiagnosticCategory.Error, key: "Initializer of instance member variable '{0}' cannot reference identifier '{1}' declared in the constructor." },
162163
Static_members_cannot_reference_class_type_parameters: { code: 2302, category: DiagnosticCategory.Error, key: "Static members cannot reference class type parameters." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@
619619
"category": "Error",
620620
"code": 1199
621621
},
622+
"Line terminator not permitted before arrow.": {
623+
"category": "Error",
624+
"code": 1200
625+
},
622626
"Duplicate identifier '{0}'.": {
623627
"category": "Error",
624628
"code": 2300

0 commit comments

Comments
 (0)