-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: #249 - Remove TypeScript compiler peer dependency.
BREAKING CHANGE: Access the TypeScript compiler via `import {ts} from "ts-simple-ast";` * NewLineKind is now the TypeScript compiler's enum.
- Loading branch information
Showing
408 changed files
with
6,475 additions
and
2,280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import {StatementedNode, EnumDeclaration, EnumMemberStructure, InterfaceDeclaration, TypeAliasDeclaration, ClassDeclaration, | ||
PropertyDeclaration} from "./../../src/main"; | ||
|
||
// todo: in the future this should be done in the library (ex. node.addInterface(cloningInterface.getStructure())) | ||
|
||
export function cloneInterfaces(node: StatementedNode, cloningInterfaces: InterfaceDeclaration[]) { | ||
node.addInterfaces(cloningInterfaces.map(cloningInterface => ({ | ||
isExported: true, | ||
name: cloningInterface.getName(), | ||
typeParameters: cloningInterface.getTypeParameters().map(p => ({ | ||
name: p.getName(), | ||
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText() | ||
})), | ||
extends: cloningInterface.getExtends().map(e => e.getText()), | ||
docs: cloningInterface.getJsDocs().map(d => ({ description: d.getInnerText() })), | ||
properties: cloningInterface.getProperties().map(nodeProp => ({ | ||
name: nodeProp.getName(), | ||
type: nodeProp.getTypeNodeOrThrow().getText(), | ||
hasQuestionToken: nodeProp.hasQuestionToken(), | ||
docs: nodeProp.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })) | ||
})), | ||
methods: cloningInterface.getMethods().map(method => ({ | ||
name: method.getName(), | ||
hasQuestionToken: method.hasQuestionToken(), | ||
returnType: method.getReturnTypeNodeOrThrow().getText(), | ||
typeParameters: method.getTypeParameters().map(p => ({ | ||
name: p.getName(), | ||
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText() | ||
})), | ||
parameters: method.getParameters().map(p => ({ | ||
name: p.getNameOrThrow(), | ||
hasQuestionToken: p.hasQuestionToken(), | ||
type: p.getTypeNodeOrThrow().getText() | ||
})) | ||
})), | ||
indexSignatures: cloningInterface.getIndexSignatures().map(s => ({ | ||
keyName: s.getKeyName(), | ||
keyType: s.getKeyTypeNode().getText(), | ||
returnType: s.getReturnTypeNode().getText(), | ||
docs: s.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })) | ||
})) | ||
}))); | ||
} | ||
|
||
export function cloneEnums(node: StatementedNode, cloningEnums: EnumDeclaration[]) { | ||
node.addEnums(cloningEnums.map(cloningEnum => ({ | ||
name: cloningEnum.getName(), | ||
isExported: true, | ||
members: cloningEnum.getMembers().map(m => ({ | ||
name: m.getName(), | ||
docs: m.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })), | ||
value: m.getValue() | ||
}) as EnumMemberStructure) | ||
}))); | ||
} | ||
|
||
export function cloneTypeAliases(node: StatementedNode, typeAliases: TypeAliasDeclaration[]) { | ||
node.addTypeAliases(typeAliases.map(typeAlias => ({ | ||
name: typeAlias.getName(), | ||
isExported: true, | ||
typeParameters: typeAlias.getTypeParameters().map(p => ({ | ||
name: p.getName(), | ||
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText() | ||
})), | ||
docs: typeAlias.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })), | ||
type: typeAlias.getTypeNodeOrThrow().getText() | ||
}))); | ||
} | ||
|
||
export function cloneClasses(node: StatementedNode, classes: ClassDeclaration[]) { | ||
node.addClasses(classes.map(c => ({ | ||
name: c.getName(), | ||
isExported: true, | ||
typeParameters: c.getTypeParameters().map(p => ({ | ||
name: p.getName(), | ||
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText() | ||
})), | ||
docs: c.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })), | ||
properties: (c.getInstanceProperties() as PropertyDeclaration[]).map(nodeProp => ({ | ||
name: nodeProp.getName(), | ||
type: nodeProp.getType().getText(), | ||
hasQuestionToken: nodeProp.hasQuestionToken(), | ||
docs: nodeProp.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })) | ||
})), | ||
methods: c.getInstanceMethods().map(method => ({ | ||
name: method.getName(), | ||
returnType: method.getReturnTypeNodeOrThrow().getText(), | ||
typeParameters: method.getTypeParameters().map(p => ({ | ||
name: p.getName(), | ||
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText() | ||
})), | ||
parameters: method.getParameters().map(p => ({ | ||
name: p.getNameOrThrow(), | ||
hasQuestionToken: p.hasQuestionToken(), | ||
type: p.getTypeNodeOrThrow().getText() | ||
})) | ||
})) | ||
}))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Code generation: Create Compiler API Layer | ||
* ------------------------------------------ | ||
* This creates a file that contains the typings from the TypeScript compiler API. | ||
* ------------------------------------------ | ||
*/ | ||
import * as path from "path"; | ||
import {rootFolder} from "./config"; | ||
import {InspectorFactory} from "./inspectors"; | ||
import {EnumDeclaration, InterfaceDeclaration, TypeAliasDeclaration, ClassDeclaration, TypeGuards, ts, SyntaxKind, | ||
UnionTypeNode} from "./../src/main"; | ||
import {cloneEnums, cloneInterfaces, cloneTypeAliases, cloneClasses} from "./common/cloning"; | ||
|
||
const enumsToSeparate = ["SyntaxKind", "ScriptTarget", "ScriptKind", "LanguageVariant", "EmitHint", "JsxEmit", "ModuleKind", "ModuleResolutionKind", | ||
"NewLineKind", "TypeFlags", "ObjectFlags", "SymbolFlags", "TypeFormatFlags", "DiagnosticCategory"]; | ||
const interfacesToSeparate = ["CompilerOptions", "MapLike"]; | ||
const typeAliasesToSeparate: string[] = []; | ||
|
||
export function createCompilerApiLayer(factory: InspectorFactory) { | ||
const tsInspector = factory.getTsInspector(); | ||
const ast = factory.getAst(); | ||
const declarationFile = tsInspector.getDeclarationFile(); | ||
|
||
const allEnums = declarationFile.getDescendantsOfKind(SyntaxKind.EnumDeclaration) as EnumDeclaration[]; | ||
const allInterfaces = declarationFile.getDescendantsOfKind(SyntaxKind.InterfaceDeclaration) as InterfaceDeclaration[]; | ||
const allTypeAliases = declarationFile.getDescendantsOfKind(SyntaxKind.TypeAliasDeclaration) as TypeAliasDeclaration[]; | ||
|
||
createTsSourceFile(); | ||
|
||
function createTsSourceFile() { | ||
const sourceFile = getOrCreateSourceFile("ts.ts"); | ||
|
||
sourceFile.addImportDeclarations([{ | ||
namespaceImport: "tsCompiler", | ||
moduleSpecifier: "typescript" | ||
}, { | ||
namedImports: [{ name: "ObjectUtils" }], | ||
moduleSpecifier: sourceFile.getRelativePathToSourceFileAsModuleSpecifier(ast.getSourceFileOrThrow("src/utils/ObjectUtils.ts")) | ||
}]); | ||
|
||
addSeparatedDeclarations(); | ||
|
||
const tsNamespace = sourceFile.addNamespace({ | ||
name: "ts", | ||
isExported: true | ||
}); | ||
|
||
cloneInterfaces(tsNamespace, allInterfaces.filter(i => interfacesToSeparate.indexOf(i.getName()) === -1)); | ||
cloneEnums(tsNamespace, allEnums.filter(e => enumsToSeparate.indexOf(e.getName()) === -1)); | ||
cloneTypeAliases(tsNamespace, allTypeAliases.filter(t => typeAliasesToSeparate.indexOf(t.getName()) === -1)); | ||
cloneClasses(tsNamespace, declarationFile.getDescendantsOfKind(SyntaxKind.ClassDeclaration) as ClassDeclaration[]); | ||
tsNamespace.getStatements().forEach(s => { | ||
if (TypeGuards.isAmbientableNode(s)) | ||
s.setHasDeclareKeyword(true); | ||
}); | ||
|
||
sourceFile.insertStatements(0, writer => { | ||
writer.writeLine("/* tslint:disable */"); | ||
writer.writeLine("// DO NOT EDIT - This file is automatically generated by createCompilerApiLayer.ts"); | ||
}); | ||
|
||
tsNamespace.addStatements(writer => { | ||
writer.newLine(); | ||
writer.writeLine("// overwrite this namespace with the TypeScript compiler"); | ||
writer.write("ObjectUtils.assign((ts as any), tsCompiler);"); | ||
}); | ||
|
||
sourceFile.replaceWithText(sourceFile.getFullText().replace(/\r?\n/g, "\r\n")); | ||
|
||
sourceFile.save(); | ||
|
||
function addSeparatedDeclarations() { | ||
for (const enumDec of allEnums.filter(e => enumsToSeparate.indexOf(e.getName()) >= 0)) | ||
cloneEnums(sourceFile, [enumDec]); | ||
|
||
for (const interfaceDec of allInterfaces.filter(i => interfacesToSeparate.indexOf(i.getName()) >= 0)) | ||
cloneInterfaces(sourceFile, [interfaceDec]); | ||
|
||
for (const typeAliasDec of allTypeAliases.filter(t => typeAliasesToSeparate.indexOf(t.getName()) >= 0)) | ||
cloneTypeAliases(sourceFile, [typeAliasDec]); | ||
|
||
// todo: need a better way of doing this in the future... | ||
const returnTypeNode = sourceFile.getInterfaceOrThrow("CompilerOptions").getIndexSignatures()[0].getReturnTypeNode() as UnionTypeNode; | ||
returnTypeNode.getTypeNodes().map(n => { | ||
if (n.getText() === "CompilerOptionsValue" || n.getText() === "JsonSourceFile") | ||
n.replaceWithText(`ts.${n.getText()}`); | ||
}); | ||
} | ||
} | ||
|
||
function getOrCreateSourceFile(fileName: string) { | ||
const filePath = path.join(rootFolder, "src/typescript", fileName); | ||
const existingSourceFile = ast.getSourceFile(filePath); | ||
if (existingSourceFile != null) | ||
existingSourceFile.replaceWithText(""); | ||
return existingSourceFile || ast.createSourceFile(filePath); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.