Skip to content

Commit

Permalink
fix: Syntax kind to node mappings should include aliased kind names.
Browse files Browse the repository at this point in the history
Also, fixed up some code to stop using "First" and "Last" syntax kinds.
  • Loading branch information
dsherret committed Mar 19, 2018
1 parent 873c604 commit 5c7109e
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 64 deletions.
3 changes: 2 additions & 1 deletion scripts/code-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {InspectorFactory} from "./inspectors";
// setup
const factory = new InspectorFactory();
const inspector = factory.getTsSimpleAstInspector();
const tsInspector = factory.getTsInspector();

// create
console.log("Creating get structure functions...");
Expand All @@ -16,6 +17,6 @@ createTypeGuardsUtility(inspector);
console.log("Creating compiler api layer...");
createCompilerApiLayer(factory);
console.log("Creating kind to node mappings...");
createKindToNodeMappings(inspector);
createKindToNodeMappings(inspector, tsInspector);

inspector.getProject().save();
18 changes: 10 additions & 8 deletions scripts/createKindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
* ----------------------------------------------
*/
import {ClassDeclaration, MethodDeclaration, MethodDeclarationStructure, MethodSignature, MethodSignatureStructure, JSDocStructure,
ParameterDeclarationStructure, SourceFile, InterfaceDeclaration, TypeGuards} from "./../src/main";
ParameterDeclarationStructure, SourceFile, InterfaceDeclaration, TypeGuards, SyntaxKind} from "./../src/main";
import {hasDescendantBaseType} from "./common";
import {TsSimpleAstInspector} from "./inspectors";
import {TsSimpleAstInspector, TsInspector} from "./inspectors";

// this can go away once conditional types are well supported (maybe a few versions after)

export function createKindToNodeMappings(inspector: TsSimpleAstInspector) {
export function createKindToNodeMappings(inspector: TsSimpleAstInspector, tsInspector: TsInspector) {
const project = inspector.getProject();
const kindToNodeMappingsFile = project.getSourceFileOrThrow("kindToNodeMappings.ts");
const nodeToWrapperMappings = inspector.getNodeToWrapperMappings();
Expand Down Expand Up @@ -50,11 +50,13 @@ export function createKindToNodeMappings(inspector: TsSimpleAstInspector) {
for (const mapping of nodeToWrapperMappings) {
if (!hasDescendantBaseType(mapping.wrappedNode.getType(), t => t.getText() === classType.getText()))
continue;
for (const kind of mapping.syntaxKindNames) {
newInterface.addProperty({
name: `[SyntaxKind.${kind}]`,
type: `compiler.${mapping.wrapperName}`
});
for (const kindName of mapping.syntaxKindNames) {
for (const possibleKindName of tsInspector.getNamesFromKind((SyntaxKind as any)[kindName])) {
newInterface.addProperty({
name: `[SyntaxKind.${possibleKindName}]`,
type: `compiler.${mapping.wrapperName}`
});
}
}
}
}
Expand Down
20 changes: 19 additions & 1 deletion scripts/inspectors/TsInspector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Project, {InterfaceDeclaration, SourceFile} from "./../../src/main";
import Project, {InterfaceDeclaration, SourceFile, SyntaxKind} from "./../../src/main";
import {Memoize, ArrayUtils} from "./../../src/utils";
import {hasDescendantBaseType} from "./../common";
import {TsNode} from "./ts";
Expand All @@ -22,4 +22,22 @@ export class TsInspector {
}
return ArrayUtils.sortByProperty(interfaces.map(i => this.wrapperFactory.getTsNode(i)), item => item.getName());
}

getNamesFromKind(kind: SyntaxKind) {
const kindToNameMappings = this.getKindToNameMappings();
return [...kindToNameMappings[kind]];
}

@Memoize
private getKindToNameMappings() {
const kindToNameMappings: { [kind: number]: string[]; } = {};
for (const name of Object.keys(SyntaxKind).filter(k => isNaN(parseInt(k, 10)))) {
const value = (SyntaxKind as any)[name];
if (kindToNameMappings[value] == null)
kindToNameMappings[value] = [];
kindToNameMappings[value].push(name);
}

return kindToNameMappings;
}
}
2 changes: 1 addition & 1 deletion src/compiler/base/TextInsertableNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function TextInsertableNode<T extends Constructor<TextInsertableNodeExten

function getValidRange(thisNode: Node): [number, number] {
const rangeNode = getRangeNode();
const openBrace = TypeGuards.isSourceFile(rangeNode) ? undefined : rangeNode.getPreviousSiblingIfKind(SyntaxKind.FirstPunctuation);
const openBrace = TypeGuards.isSourceFile(rangeNode) ? undefined : rangeNode.getPreviousSiblingIfKind(SyntaxKind.OpenBraceToken);
const closeBrace = openBrace == null ? undefined : rangeNode.getNextSiblingIfKind(SyntaxKind.CloseBraceToken);
if (openBrace != null && closeBrace != null)
return [openBrace.getEnd(), closeBrace.getStart()];
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/base/TypeArgumentedNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function TypeArgumentedNode<T extends Constructor<TypeArgumentedNodeExten
}
else {
insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(SyntaxKind.FirstBinaryOperator).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
parent: this.getFirstChildByKindOrThrow(SyntaxKind.LessThanToken).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: typeArguments,
insertIndex: index,
newTexts: argumentTexts
Expand All @@ -113,7 +113,7 @@ export function TypeArgumentedNode<T extends Constructor<TypeArgumentedNodeExten
const childSyntaxList = typeArguments[0].getParentSyntaxListOrThrow();
removeChildren({
children: [
childSyntaxList.getPreviousSiblingIfKindOrThrow(SyntaxKind.FirstBinaryOperator),
childSyntaxList.getPreviousSiblingIfKindOrThrow(SyntaxKind.LessThanToken),
childSyntaxList,
childSyntaxList.getNextSiblingIfKindOrThrow(SyntaxKind.GreaterThanToken)
]
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/base/TypeParameteredNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function TypeParameteredNode<T extends Constructor<TypeParameteredNodeExt
}
else {
insertIntoCommaSeparatedNodes({
parent: this.getFirstChildByKindOrThrow(SyntaxKind.FirstBinaryOperator).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
parent: this.getFirstChildByKindOrThrow(SyntaxKind.LessThanToken).getNextSiblingIfKindOrThrow(SyntaxKind.SyntaxList),
currentNodes: typeParameters,
insertIndex: index,
newTexts: typeParamCodes
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/base/helpers/setBodyTextForNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {getBodyText} from "./getBodyText";
*/
export function setBodyTextForNode(body: Node, textOrWriterFunction: string | ((writer: CodeBlockWriter) => void)) {
const newText = getBodyText(body.getWriterWithIndentation(), textOrWriterFunction);
const openBrace = body.getFirstChildByKindOrThrow(SyntaxKind.FirstPunctuation);
const openBrace = body.getFirstChildByKindOrThrow(SyntaxKind.OpenBraceToken);
const closeBrace = body.getFirstChildByKindOrThrow(SyntaxKind.CloseBraceToken);

insertIntoParentTextRange({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function InitializerSetExpressionableNode<T extends Constructor<Initializ
const initializer = this.getInitializer();
if (initializer == null)
return this;
const previousSibling = initializer.getPreviousSiblingIfKindOrThrow(SyntaxKind.FirstAssignment);
const previousSibling = initializer.getPreviousSiblingIfKindOrThrow(SyntaxKind.EqualsToken);

removeChildren({
children: [previousSibling, initializer],
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ export class Node<NodeType extends ts.Node = ts.Node> {
let passedBrace = false;
for (const child of node.getCompilerChildren()) {
if (!passedBrace)
passedBrace = child.kind === SyntaxKind.FirstPunctuation;
passedBrace = child.kind === SyntaxKind.OpenBraceToken;
else if (child.kind === SyntaxKind.SyntaxList)
return getWrappedNode(this, child) as SyntaxList;
}
Expand Down
19 changes: 14 additions & 5 deletions src/compiler/kindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ export interface KindToNodeMappings {
[SyntaxKind.ExpressionWithTypeArguments]: compiler.ExpressionWithTypeArguments;
[SyntaxKind.ExpressionStatement]: compiler.ExpressionStatement;
[SyntaxKind.ExternalModuleReference]: compiler.ExternalModuleReference;
[SyntaxKind.FirstLiteralToken]: compiler.NumericLiteral;
[SyntaxKind.NumericLiteral]: compiler.NumericLiteral;
[SyntaxKind.FirstNode]: compiler.QualifiedName;
[SyntaxKind.QualifiedName]: compiler.QualifiedName;
[SyntaxKind.FirstNode]: compiler.QualifiedName;
[SyntaxKind.ForInStatement]: compiler.ForInStatement;
[SyntaxKind.ForOfStatement]: compiler.ForOfStatement;
[SyntaxKind.ForStatement]: compiler.ForStatement;
Expand All @@ -62,13 +60,16 @@ export interface KindToNodeMappings {
[SyntaxKind.InterfaceDeclaration]: compiler.InterfaceDeclaration;
[SyntaxKind.IntersectionType]: compiler.IntersectionTypeNode;
[SyntaxKind.JSDocTag]: compiler.JSDocUnknownTag;
[SyntaxKind.FirstJSDocTagNode]: compiler.JSDocUnknownTag;
[SyntaxKind.JSDocAugmentsTag]: compiler.JSDocAugmentsTag;
[SyntaxKind.JSDocClassTag]: compiler.JSDocClassTag;
[SyntaxKind.JSDocReturnTag]: compiler.JSDocReturnTag;
[SyntaxKind.JSDocTypeTag]: compiler.JSDocTypeTag;
[SyntaxKind.JSDocTypedefTag]: compiler.JSDocTypedefTag;
[SyntaxKind.JSDocParameterTag]: compiler.JSDocParameterTag;
[SyntaxKind.JSDocPropertyTag]: compiler.JSDocPropertyTag;
[SyntaxKind.LastJSDocNode]: compiler.JSDocPropertyTag;
[SyntaxKind.LastJSDocTagNode]: compiler.JSDocPropertyTag;
[SyntaxKind.JsxAttribute]: compiler.JsxAttribute;
[SyntaxKind.JsxClosingElement]: compiler.JsxClosingElement;
[SyntaxKind.JsxClosingFragment]: compiler.JsxClosingFragment;
Expand All @@ -91,6 +92,10 @@ export interface KindToNodeMappings {
[SyntaxKind.NonNullExpression]: compiler.NonNullExpression;
[SyntaxKind.NotEmittedStatement]: compiler.NotEmittedStatement;
[SyntaxKind.NoSubstitutionTemplateLiteral]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.LastLiteralToken]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.FirstTemplateToken]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.NumericLiteral]: compiler.NumericLiteral;
[SyntaxKind.FirstLiteralToken]: compiler.NumericLiteral;
[SyntaxKind.ObjectLiteralExpression]: compiler.ObjectLiteralExpression;
[SyntaxKind.OmittedExpression]: compiler.OmittedExpression;
[SyntaxKind.Parameter]: compiler.ParameterDeclaration;
Expand All @@ -117,6 +122,7 @@ export interface KindToNodeMappings {
[SyntaxKind.TemplateMiddle]: compiler.TemplateMiddle;
[SyntaxKind.TemplateSpan]: compiler.TemplateSpan;
[SyntaxKind.TemplateTail]: compiler.TemplateTail;
[SyntaxKind.LastTemplateToken]: compiler.TemplateTail;
[SyntaxKind.ThrowStatement]: compiler.ThrowStatement;
[SyntaxKind.TryStatement]: compiler.TryStatement;
[SyntaxKind.TupleType]: compiler.TupleTypeNode;
Expand All @@ -130,6 +136,7 @@ export interface KindToNodeMappings {
[SyntaxKind.VariableDeclarationList]: compiler.VariableDeclarationList;
[SyntaxKind.VariableStatement]: compiler.VariableStatement;
[SyntaxKind.JSDocComment]: compiler.JSDoc;
[SyntaxKind.TypePredicate]: compiler.TypeNode;
[SyntaxKind.FirstTypeNode]: compiler.TypeNode;
[SyntaxKind.SemicolonToken]: compiler.Node;
[SyntaxKind.TypeOfExpression]: compiler.TypeOfExpression;
Expand Down Expand Up @@ -165,8 +172,6 @@ export interface KindToExpressionMappings {
[SyntaxKind.ConditionalExpression]: compiler.ConditionalExpression;
[SyntaxKind.DeleteExpression]: compiler.DeleteExpression;
[SyntaxKind.ElementAccessExpression]: compiler.ElementAccessExpression;
[SyntaxKind.FirstLiteralToken]: compiler.NumericLiteral;
[SyntaxKind.NumericLiteral]: compiler.NumericLiteral;
[SyntaxKind.FunctionExpression]: compiler.FunctionExpression;
[SyntaxKind.Identifier]: compiler.Identifier;
[SyntaxKind.JsxClosingFragment]: compiler.JsxClosingFragment;
Expand All @@ -180,6 +185,10 @@ export interface KindToExpressionMappings {
[SyntaxKind.NewExpression]: compiler.NewExpression;
[SyntaxKind.NonNullExpression]: compiler.NonNullExpression;
[SyntaxKind.NoSubstitutionTemplateLiteral]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.LastLiteralToken]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.FirstTemplateToken]: compiler.NoSubstitutionTemplateLiteral;
[SyntaxKind.NumericLiteral]: compiler.NumericLiteral;
[SyntaxKind.FirstLiteralToken]: compiler.NumericLiteral;
[SyntaxKind.ObjectLiteralExpression]: compiler.ObjectLiteralExpression;
[SyntaxKind.OmittedExpression]: compiler.OmittedExpression;
[SyntaxKind.ParenthesizedExpression]: compiler.ParenthesizedExpression;
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/type/TypeParameterDeclaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class TypeParameterDeclaration extends TypeParameterDeclarationBase<ts.Ty

function removeAllTypeParameters() {
const children = [
parentSyntaxList.getPreviousSiblingIfKindOrThrow(SyntaxKind.FirstBinaryOperator),
parentSyntaxList.getPreviousSiblingIfKindOrThrow(SyntaxKind.LessThanToken),
parentSyntaxList,
parentSyntaxList.getNextSiblingIfKindOrThrow(SyntaxKind.GreaterThanToken)
];
Expand Down
9 changes: 3 additions & 6 deletions src/factories/nodeToWrapperMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[SyntaxKind.ExpressionWithTypeArguments]: compiler.ExpressionWithTypeArguments,
[SyntaxKind.ExpressionStatement]: compiler.ExpressionStatement,
[SyntaxKind.ExternalModuleReference]: compiler.ExternalModuleReference,
[SyntaxKind.FirstLiteralToken]: compiler.NumericLiteral,
[SyntaxKind.FirstNode]: compiler.QualifiedName,
[SyntaxKind.QualifiedName]: compiler.QualifiedName,
[SyntaxKind.ForInStatement]: compiler.ForInStatement,
[SyntaxKind.ForOfStatement]: compiler.ForOfStatement,
[SyntaxKind.ForStatement]: compiler.ForStatement,
Expand Down Expand Up @@ -102,7 +101,6 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[SyntaxKind.PropertyAssignment]: compiler.PropertyAssignment,
[SyntaxKind.PropertyDeclaration]: compiler.PropertyDeclaration,
[SyntaxKind.PropertySignature]: compiler.PropertySignature,
[SyntaxKind.QualifiedName]: compiler.QualifiedName,
[SyntaxKind.RegularExpressionLiteral]: compiler.RegularExpressionLiteral,
[SyntaxKind.ReturnStatement]: compiler.ReturnStatement,
[SyntaxKind.SetAccessor]: compiler.SetAccessorDeclaration,
Expand Down Expand Up @@ -131,14 +129,13 @@ export const nodeToWrapperMappings: { [key: number]: any } = {
[SyntaxKind.VariableDeclarationList]: compiler.VariableDeclarationList,
[SyntaxKind.VariableStatement]: compiler.VariableStatement,
[SyntaxKind.JSDocComment]: compiler.JSDoc,
[SyntaxKind.FirstTypeNode]: compiler.TypeNode, // todo: should be changed when implementing TypePredicateNode
[SyntaxKind.LastTypeNode]: compiler.LiteralTypeNode,
[SyntaxKind.TypePredicate]: compiler.TypeNode, // todo: should be changed when implementing TypePredicateNode
[SyntaxKind.SemicolonToken]: compiler.Node,
[SyntaxKind.TypeOfExpression]: compiler.TypeOfExpression,
[SyntaxKind.WhileStatement]: compiler.WhileStatement,
[SyntaxKind.WithStatement]: compiler.WithStatement,
[SyntaxKind.YieldExpression]: compiler.YieldExpression,
// literals
// keywords
[SyntaxKind.AnyKeyword]: compiler.Expression,
[SyntaxKind.BooleanKeyword]: compiler.Expression,
[SyntaxKind.FalseKeyword]: compiler.BooleanLiteral,
Expand Down
2 changes: 1 addition & 1 deletion src/tests/compiler/common/nodeTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ describe(nameof(Node), () => {
});

it("should return a the node at the specified pos when specifying a space", () => {
expect(sourceFile.getDescendantAtPos(variableStatement.getPos() - 1)!.getKind()).to.equal(SyntaxKind.FirstPunctuation);
expect(sourceFile.getDescendantAtPos(variableStatement.getPos() - 1)!.getKind()).to.equal(SyntaxKind.OpenBraceToken);
});
});

Expand Down

0 comments on commit 5c7109e

Please sign in to comment.