From 6a9316118da1f8dd68a138816466c565c2d7a6b2 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Mon, 8 Feb 2021 18:41:16 -0500 Subject: [PATCH] Fix bug in code generation script (removing the kindToWrapperMappings file was causing diagnostics to occur) --- .../ts-morph/scripts/common/getProject.ts | 7 +- .../generation/createKindToNodeMappings.ts | 65 +++++++++++-------- .../src/compiler/ast/kindToNodeMappings.ts | 5 +- 3 files changed, 41 insertions(+), 36 deletions(-) diff --git a/packages/ts-morph/scripts/common/getProject.ts b/packages/ts-morph/scripts/common/getProject.ts index f3ae79596..08bc23fe5 100644 --- a/packages/ts-morph/scripts/common/getProject.ts +++ b/packages/ts-morph/scripts/common/getProject.ts @@ -3,15 +3,12 @@ import * as path from "path"; import { rootFolder } from "../config"; export function getProject() { - const project = new tsMorph.Project({ + return new tsMorph.Project({ tsConfigFilePath: path.join(rootFolder, "tsconfig.json"), - skipAddingFilesFromTsConfig: true, + skipAddingFilesFromTsConfig: false, manipulationSettings: { newLineKind: tsMorph.NewLineKind.LineFeed, insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: true, }, }); - project.addSourceFilesAtPaths(path.join(rootFolder, "src/**/*{.d.ts,.ts}")); - project.addSourceFileAtPath(path.join(rootFolder, "node_modules/typescript/lib/typescript.d.ts")); - return project; } diff --git a/packages/ts-morph/scripts/generation/createKindToNodeMappings.ts b/packages/ts-morph/scripts/generation/createKindToNodeMappings.ts index 823304ca2..91caed590 100644 --- a/packages/ts-morph/scripts/generation/createKindToNodeMappings.ts +++ b/packages/ts-morph/scripts/generation/createKindToNodeMappings.ts @@ -13,36 +13,43 @@ export function createKindToNodeMappings(inspector: TsMorphInspector, tsInspecto const project = inspector.getProject(); const kindToNodeMappingsFile = project.getSourceFileOrThrow("kindToNodeMappings.ts"); const kindToWrapperMappings = inspector.getKindToWrapperMappings(); - kindToNodeMappingsFile.removeText(); - - // add imports - kindToNodeMappingsFile.insertText(0, writer => - writer - .writeLine("// DO NOT EDIT - Automatically maintained by createKindToNodeMappings.ts") - .writeLine("// Edit factories/kindToWrapperMappings.ts then run yarn code-generate instead.")); - kindToNodeMappingsFile.addImportDeclarations([{ - namedImports: ["SyntaxKind", "ts"], - moduleSpecifier: "@ts-morph/common", - }, { - namespaceImport: "compiler", - moduleSpecifier: kindToNodeMappingsFile.getRelativePathAsModuleSpecifierTo(project.getSourceFileOrThrow("src/compiler/ast/index.ts")), - }]); - addTypeForSubSet("ImplementedKindToNodeMappings", project.getSourceFileOrThrow("Node.ts").getClassOrThrow("Node")); - addDefaultIndexSignature(kindToNodeMappingsFile.addInterface({ + const interfaceStructures: tsMorph.InterfaceDeclarationStructure[] = []; + interfaceStructures.push(getTypeForSubSet("ImplementedKindToNodeMappings", project.getSourceFileOrThrow("Node.ts").getClassOrThrow("Node"))); + interfaceStructures.push(withDefaultIndexSignature({ + kind: tsMorph.StructureKind.Interface, isExported: true, name: "KindToNodeMappings", extends: ["ImplementedKindToNodeMappings"], })); - addDefaultIndexSignature(addTypeForSubSet("KindToExpressionMappings", project.getSourceFileOrThrow("Expression.ts").getClassOrThrow("Expression"))); + interfaceStructures.push(withDefaultIndexSignature(getTypeForSubSet("KindToExpressionMappings", project.getSourceFileOrThrow("Expression.ts").getClassOrThrow("Expression")))); + + // add imports + kindToNodeMappingsFile.removeText(); + kindToNodeMappingsFile.addStatements([ + writer => writer + .writeLine("// DO NOT EDIT - Automatically maintained by createKindToNodeMappings.ts") + .writeLine("// Edit factories/kindToWrapperMappings.ts then run yarn code-generate instead."), + { + kind: tsMorph.StructureKind.ImportDeclaration, + namedImports: ["SyntaxKind", "ts"], + moduleSpecifier: "@ts-morph/common", + }, { + kind: tsMorph.StructureKind.ImportDeclaration, + namespaceImport: "compiler", + moduleSpecifier: kindToNodeMappingsFile.getRelativePathAsModuleSpecifierTo(project.getSourceFileOrThrow("src/compiler/ast/index.ts")), + }, + ...interfaceStructures, + ]); - function addTypeForSubSet(name: string, nodeClass: tsMorph.ClassDeclaration) { + function getTypeForSubSet(name: string, nodeClass: tsMorph.ClassDeclaration) { const classType = nodeClass.getType(); const addingProperties: tsMorph.PropertySignatureStructure[] = []; - const newInterface = kindToNodeMappingsFile.addInterface({ + const newInterface: tsMorph.InterfaceDeclarationStructure = { + kind: tsMorph.StructureKind.Interface, isExported: true, name, - }); + }; for (const mapping of kindToWrapperMappings) { if (!hasDescendantBaseType(mapping.wrappedNode.getType(), t => t.getText() === classType.getText())) @@ -58,8 +65,7 @@ export function createKindToNodeMappings(inspector: TsMorphInspector, tsInspecto } } - newInterface.addProperties(addingProperties); - newInterface.getChildren().forEach(c => c.forget()); + newInterface.properties = addingProperties; return newInterface; @@ -76,11 +82,14 @@ export function createKindToNodeMappings(inspector: TsMorphInspector, tsInspecto } } - function addDefaultIndexSignature(interfaceDec: tsMorph.InterfaceDeclaration) { - interfaceDec.insertIndexSignature(0, { - keyName: "kind", - keyType: "number", - returnType: "compiler.Node", - }); + function withDefaultIndexSignature(interfaceStructure: tsMorph.InterfaceDeclarationStructure) { + interfaceStructure.indexSignatures = [{ + keyName: "kind", + keyType: "number", + returnType: "compiler.Node", + }, + ...interfaceStructure.indexSignatures ?? [] + ]; + return interfaceStructure; } } diff --git a/packages/ts-morph/src/compiler/ast/kindToNodeMappings.ts b/packages/ts-morph/src/compiler/ast/kindToNodeMappings.ts index 2cf89f8a6..375421510 100644 --- a/packages/ts-morph/src/compiler/ast/kindToNodeMappings.ts +++ b/packages/ts-morph/src/compiler/ast/kindToNodeMappings.ts @@ -1,8 +1,7 @@ -import { SyntaxKind, ts } from "@ts-morph/common"; -import * as compiler from "./index"; - // DO NOT EDIT - Automatically maintained by createKindToNodeMappings.ts // Edit factories/kindToWrapperMappings.ts then run yarn code-generate instead. +import { SyntaxKind, ts } from "@ts-morph/common"; +import * as compiler from "./index"; export interface ImplementedKindToNodeMappings { [SyntaxKind.SourceFile]: compiler.SourceFile;