Skip to content

Commit 4a5f56f

Browse files
authored
Merge pull request #11342 from Microsoft/syntaxKindLiterals
Added literal kind properties for each node.
2 parents 3212e25 + 519095f commit 4a5f56f

21 files changed

+659
-401
lines changed

scripts/tslint/typeOperatorSpacingRule.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ class TypeOperatorSpacingWalker extends Lint.RuleWalker {
1818
for (let i = 1; i < types.length; i++) {
1919
const currentType = types[i];
2020
if (expectedStart !== currentType.pos || currentType.getLeadingTriviaWidth() !== 1) {
21-
const failure = this.createFailure(currentType.pos, currentType.getWidth(), Rule.FAILURE_STRING);
22-
this.addFailure(failure);
21+
const sourceFile = currentType.getSourceFile();
22+
const previousTypeEndPos = sourceFile.getLineAndCharacterOfPosition(types[i - 1].end);
23+
const currentTypeStartPos = sourceFile.getLineAndCharacterOfPosition(currentType.pos);
24+
if (previousTypeEndPos.line === currentTypeStartPos.line) {
25+
const failure = this.createFailure(currentType.pos, currentType.getWidth(), Rule.FAILURE_STRING);
26+
this.addFailure(failure);
27+
}
2328
}
2429
expectedStart = currentType.end + 2;
2530
}

src/compiler/binder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ namespace ts {
11111111
}
11121112
else {
11131113
forEachChild(node, bind);
1114-
if (node.operator === SyntaxKind.PlusEqualsToken || node.operator === SyntaxKind.MinusMinusToken) {
1114+
if (node.operator === SyntaxKind.PlusPlusToken || node.operator === SyntaxKind.MinusMinusToken) {
11151115
bindAssignmentTargetFlow(node.operand);
11161116
}
11171117
}
@@ -1360,7 +1360,7 @@ namespace ts {
13601360
function hasExportDeclarations(node: ModuleDeclaration | SourceFile): boolean {
13611361
const body = node.kind === SyntaxKind.SourceFile ? node : (<ModuleDeclaration>node).body;
13621362
if (body && (body.kind === SyntaxKind.SourceFile || body.kind === SyntaxKind.ModuleBlock)) {
1363-
for (const stat of (<Block>body).statements) {
1363+
for (const stat of (<BlockLike>body).statements) {
13641364
if (stat.kind === SyntaxKind.ExportDeclaration || stat.kind === SyntaxKind.ExportAssignment) {
13651365
return true;
13661366
}

src/compiler/checker.ts

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,7 +3892,7 @@ namespace ts {
38923892
if (!links.declaredType) {
38933893
const enumType = <EnumType>getDeclaredTypeOfEnum(getParentOfSymbol(symbol));
38943894
links.declaredType = enumType.flags & TypeFlags.Union ?
3895-
enumType.memberTypes[getEnumMemberValue(<EnumDeclaration>symbol.valueDeclaration)] :
3895+
enumType.memberTypes[getEnumMemberValue(<EnumMember>symbol.valueDeclaration)] :
38963896
enumType;
38973897
}
38983898
return links.declaredType;
@@ -6075,7 +6075,7 @@ namespace ts {
60756075
return !node.typeParameters && areAllParametersUntyped && !isNullaryArrow;
60766076
}
60776077

6078-
function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | MethodDeclaration {
6078+
function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
60796079
return (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func);
60806080
}
60816081

@@ -9271,7 +9271,7 @@ namespace ts {
92719271
}
92729272

92739273
function findFirstSuperCall(n: Node): Node {
9274-
if (isSuperCallExpression(n)) {
9274+
if (isSuperCall(n)) {
92759275
return n;
92769276
}
92779277
else if (isFunctionLike(n)) {
@@ -9780,7 +9780,7 @@ namespace ts {
97809780
// corresponding set accessor has a type annotation, return statements in the function are contextually typed
97819781
if (functionDecl.type ||
97829782
functionDecl.kind === SyntaxKind.Constructor ||
9783-
functionDecl.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<AccessorDeclaration>getDeclarationOfKind(functionDecl.symbol, SyntaxKind.SetAccessor))) {
9783+
functionDecl.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<SetAccessorDeclaration>getDeclarationOfKind(functionDecl.symbol, SyntaxKind.SetAccessor))) {
97849784
return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl));
97859785
}
97869786

@@ -10049,7 +10049,7 @@ namespace ts {
1004910049
}
1005010050
}
1005110051

10052-
function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression {
10052+
function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression | ArrowFunction {
1005310053
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
1005410054
}
1005510055

@@ -10060,7 +10060,7 @@ namespace ts {
1006010060
: undefined;
1006110061
}
1006210062

10063-
function getContextualTypeForFunctionLikeDeclaration(node: FunctionExpression | MethodDeclaration) {
10063+
function getContextualTypeForFunctionLikeDeclaration(node: FunctionExpression | ArrowFunction | MethodDeclaration) {
1006410064
return isObjectLiteralMethod(node) ?
1006510065
getContextualTypeForObjectLiteralMethod(node) :
1006610066
getApparentTypeOfContextualType(node);
@@ -10071,7 +10071,7 @@ namespace ts {
1007110071
// If the contextual type is a union type, get the signature from each type possible and if they are
1007210072
// all identical ignoring their return type, the result is same signature but with return type as
1007310073
// union type of return types from these signatures
10074-
function getContextualSignature(node: FunctionExpression | MethodDeclaration): Signature {
10074+
function getContextualSignature(node: FunctionExpression | ArrowFunction | MethodDeclaration): Signature {
1007510075
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
1007610076
const type = getContextualTypeForFunctionLikeDeclaration(node);
1007710077
if (!type) {
@@ -11423,7 +11423,7 @@ namespace ts {
1142311423
argCount = getEffectiveArgumentCount(node, /*args*/ undefined, signature);
1142411424
}
1142511425
else {
11426-
const callExpression = <CallExpression>node;
11426+
const callExpression = <CallExpression | NewExpression>node;
1142711427
if (!callExpression.arguments) {
1142811428
// This only happens when we have something of the form: 'new C'
1142911429
Debug.assert(callExpression.kind === SyntaxKind.NewExpression);
@@ -11434,7 +11434,7 @@ namespace ts {
1143411434
argCount = signatureHelpTrailingComma ? args.length + 1 : args.length;
1143511435

1143611436
// If we are missing the close paren, the call is incomplete.
11437-
callIsIncomplete = (<CallExpression>callExpression).arguments.end === callExpression.end;
11437+
callIsIncomplete = callExpression.arguments.end === callExpression.end;
1143811438

1143911439
typeArguments = callExpression.typeArguments;
1144011440
spreadArgIndex = getSpreadArgumentIndex(args);
@@ -12521,7 +12521,7 @@ namespace ts {
1252112521
* @param node The call/new expression to be checked.
1252212522
* @returns On success, the expression's signature's return type. On failure, anyType.
1252312523
*/
12524-
function checkCallExpression(node: CallExpression): Type {
12524+
function checkCallExpression(node: CallExpression | NewExpression): Type {
1252512525
// Grammar checking; stop grammar-checking if checkGrammarTypeArguments return true
1252612526
checkGrammarTypeArguments(node, node.typeArguments) || checkGrammarArguments(node, node.arguments);
1252712527

@@ -12979,7 +12979,7 @@ namespace ts {
1297912979
}
1298012980
}
1298112981

12982-
if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) {
12982+
if (produceDiagnostics && node.kind !== SyntaxKind.MethodDeclaration) {
1298312983
checkCollisionWithCapturedSuperVariable(node, (<FunctionExpression>node).name);
1298412984
checkCollisionWithCapturedThisVariable(node, (<FunctionExpression>node).name);
1298512985
}
@@ -14441,7 +14441,7 @@ namespace ts {
1444114441
}
1444214442

1444314443
function containsSuperCall(n: Node): boolean {
14444-
if (isSuperCallExpression(n)) {
14444+
if (isSuperCall(n)) {
1444514445
return true;
1444614446
}
1444714447
else if (isFunctionLike(n)) {
@@ -14497,7 +14497,7 @@ namespace ts {
1449714497
let superCallStatement: ExpressionStatement;
1449814498

1449914499
for (const statement of statements) {
14500-
if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCallExpression((<ExpressionStatement>statement).expression)) {
14500+
if (statement.kind === SyntaxKind.ExpressionStatement && isSuperCall((<ExpressionStatement>statement).expression)) {
1450114501
superCallStatement = <ExpressionStatement>statement;
1450214502
break;
1450314503
}
@@ -16457,7 +16457,7 @@ namespace ts {
1645716457
}
1645816458

1645916459
function isGetAccessorWithAnnotatedSetAccessor(node: FunctionLikeDeclaration) {
16460-
return !!(node.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<AccessorDeclaration>getDeclarationOfKind(node.symbol, SyntaxKind.SetAccessor)));
16460+
return !!(node.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<SetAccessorDeclaration>getDeclarationOfKind(node.symbol, SyntaxKind.SetAccessor)));
1646116461
}
1646216462

1646316463
function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
@@ -17449,9 +17449,12 @@ namespace ts {
1744917449
}
1745017450
}
1745117451

17452-
checkCollisionWithCapturedThisVariable(node, node.name);
17453-
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
17454-
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
17452+
if (isIdentifier(node.name)) {
17453+
checkCollisionWithCapturedThisVariable(node, node.name);
17454+
checkCollisionWithRequireExportsInGeneratedCode(node, node.name);
17455+
checkCollisionWithGlobalPromiseInGeneratedCode(node, node.name);
17456+
}
17457+
1745517458
checkExportsOnMergedDeclarations(node);
1745617459
const symbol = getSymbolOfNode(node);
1745717460

@@ -19073,15 +19076,15 @@ namespace ts {
1907319076
return undefined;
1907419077
}
1907519078

19076-
function isLiteralConstDeclaration(node: VariableDeclaration): boolean {
19079+
function isLiteralConstDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration): boolean {
1907719080
if (isConst(node)) {
1907819081
const type = getTypeOfSymbol(getSymbolOfNode(node));
1907919082
return !!(type.flags & TypeFlags.StringOrNumberLiteral && type.flags & TypeFlags.FreshLiteral);
1908019083
}
1908119084
return false;
1908219085
}
1908319086

19084-
function writeLiteralConstValue(node: VariableDeclaration, writer: SymbolWriter) {
19087+
function writeLiteralConstValue(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration, writer: SymbolWriter) {
1908519088
const type = getTypeOfSymbol(getSymbolOfNode(node));
1908619089
writer.writeStringLiteral(literalTypeToString(<LiteralType>type));
1908719090
}
@@ -19818,7 +19821,7 @@ namespace ts {
1981819821
checkGrammarForAtLeastOneTypeArgument(node, typeArguments);
1981919822
}
1982019823

19821-
function checkGrammarForOmittedArgument(node: CallExpression, args: NodeArray<Expression>): boolean {
19824+
function checkGrammarForOmittedArgument(node: CallExpression | NewExpression, args: NodeArray<Expression>): boolean {
1982219825
if (args) {
1982319826
const sourceFile = getSourceFileOfNode(node);
1982419827
for (const arg of args) {
@@ -19829,7 +19832,7 @@ namespace ts {
1982919832
}
1983019833
}
1983119834

19832-
function checkGrammarArguments(node: CallExpression, args: NodeArray<Expression>): boolean {
19835+
function checkGrammarArguments(node: CallExpression | NewExpression, args: NodeArray<Expression>): boolean {
1983319836
return checkGrammarForOmittedArgument(node, args);
1983419837
}
1983519838

@@ -19951,8 +19954,7 @@ namespace ts {
1995119954

1995219955
for (const prop of node.properties) {
1995319956
const name = prop.name;
19954-
if (prop.kind === SyntaxKind.OmittedExpression ||
19955-
name.kind === SyntaxKind.ComputedPropertyName) {
19957+
if (name.kind === SyntaxKind.ComputedPropertyName) {
1995619958
// If the name is not a ComputedPropertyName, the grammar checking will skip it
1995719959
checkGrammarComputedPropertyName(<ComputedPropertyName>name);
1995819960
}
@@ -19999,7 +20001,7 @@ namespace ts {
1999920001
currentKind = SetAccessor;
2000020002
}
2000120003
else {
20002-
Debug.fail("Unexpected syntax kind:" + prop.kind);
20004+
Debug.fail("Unexpected syntax kind:" + (<Node>prop).kind);
2000320005
}
2000420006

2000520007
const effectiveName = getPropertyNameForPropertyNameNode(name);

src/compiler/core.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,9 +1813,9 @@ namespace ts {
18131813

18141814
export interface ObjectAllocator {
18151815
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
1816-
getTokenConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Token;
1817-
getIdentifierConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Token;
1818-
getSourceFileConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => SourceFile;
1816+
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;
1817+
getIdentifierConstructor(): new (kind: SyntaxKind.Identifier, pos?: number, end?: number) => Identifier;
1818+
getSourceFileConstructor(): new (kind: SyntaxKind.SourceFile, pos?: number, end?: number) => SourceFile;
18191819
getSymbolConstructor(): new (flags: SymbolFlags, name: string) => Symbol;
18201820
getTypeConstructor(): new (checker: TypeChecker, flags: TypeFlags) => Type;
18211821
getSignatureConstructor(): new (checker: TypeChecker) => Signature;

src/compiler/declarationEmitter.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ namespace ts {
11211121
writeLine();
11221122
}
11231123

1124-
function emitVariableDeclaration(node: VariableDeclaration) {
1124+
function emitVariableDeclaration(node: VariableDeclaration | PropertyDeclaration | PropertySignature | ParameterDeclaration) {
11251125
// If we are emitting property it isn't moduleElement and hence we already know it needs to be emitted
11261126
// so there is no check needed to see if declaration is visible
11271127
if (node.kind !== SyntaxKind.VariableDeclaration || resolver.isDeclarationVisible(node)) {
@@ -1136,7 +1136,7 @@ namespace ts {
11361136
// If optional property emit ? but in the case of parameterProperty declaration with "?" indicating optional parameter for the constructor
11371137
// we don't want to emit property declaration with "?"
11381138
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature ||
1139-
(node.kind === SyntaxKind.Parameter && !isParameterPropertyDeclaration(node))) && hasQuestionToken(node)) {
1139+
(node.kind === SyntaxKind.Parameter && !isParameterPropertyDeclaration(<ParameterDeclaration>node))) && hasQuestionToken(node)) {
11401140
write("?");
11411141
}
11421142
if ((node.kind === SyntaxKind.PropertyDeclaration || node.kind === SyntaxKind.PropertySignature) && node.parent.kind === SyntaxKind.TypeLiteral) {
@@ -1626,8 +1626,7 @@ namespace ts {
16261626
}
16271627
}
16281628

1629-
function emitBindingElement(bindingElement: BindingElement) {
1630-
1629+
function emitBindingElement(bindingElement: BindingElement | OmittedExpression) {
16311630
if (bindingElement.kind === SyntaxKind.OmittedExpression) {
16321631
// If bindingElement is an omittedExpression (i.e. containing elision),
16331632
// we will emit blank space (although this may differ from users' original code,

src/compiler/emitter.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ const _super = (function (geti, seti) {
655655
case SyntaxKind.ModuleDeclaration:
656656
return emitModuleDeclaration(<ModuleDeclaration>node);
657657
case SyntaxKind.ModuleBlock:
658-
return emitModuleBlock(<Block>node);
658+
return emitModuleBlock(<ModuleBlock>node);
659659
case SyntaxKind.CaseBlock:
660660
return emitCaseBlock(<CaseBlock>node);
661661
case SyntaxKind.ImportEqualsDeclaration:
@@ -1394,7 +1394,7 @@ const _super = (function (geti, seti) {
13941394
}
13951395
}
13961396

1397-
function emitBlockStatements(node: Block) {
1397+
function emitBlockStatements(node: BlockLike) {
13981398
if (getEmitFlags(node) & EmitFlags.SingleLine) {
13991399
emitList(node, node.statements, ListFormat.SingleLineBlockStatements);
14001400
}
@@ -1795,7 +1795,7 @@ const _super = (function (geti, seti) {
17951795
}
17961796

17971797
function emitModuleBlock(node: ModuleBlock) {
1798-
if (isSingleLineEmptyBlock(node)) {
1798+
if (isEmptyBlock(node)) {
17991799
write("{ }");
18001800
}
18011801
else {
@@ -2615,7 +2615,11 @@ const _super = (function (geti, seti) {
26152615

26162616
function isSingleLineEmptyBlock(block: Block) {
26172617
return !block.multiLine
2618-
&& block.statements.length === 0
2618+
&& isEmptyBlock(block);
2619+
}
2620+
2621+
function isEmptyBlock(block: BlockLike) {
2622+
return block.statements.length === 0
26192623
&& rangeEndIsOnSameLineAsRangeStart(block, block, currentSourceFile);
26202624
}
26212625

0 commit comments

Comments
 (0)