Skip to content

Commit

Permalink
feat: Improvements to CompilerNodeToWrappedType.
Browse files Browse the repository at this point in the history
Also, using it more internally.
  • Loading branch information
dsherret committed Jun 3, 2018
1 parent dddc2a0 commit 384613a
Show file tree
Hide file tree
Showing 98 changed files with 656 additions and 574 deletions.
465 changes: 244 additions & 221 deletions lib/ts-simple-ast.d.ts

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion lib/typescript/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5582,4 +5582,8 @@ export declare namespace ts {
let disableIncrementalParsing: boolean;
}
export { SyntaxKind, TypeFormatFlags, SymbolFlags, TypeFlags, ObjectFlags, DiagnosticCategory, ModuleResolutionKind, ModuleKind, JsxEmit, NewLineKind, ScriptKind, ScriptTarget, LanguageVariant, EmitHint, IndentStyle };
export declare type CompilerApiNodeBrandPropertyNamesType = "_classElementBrand" | "_declarationBrand" | "_expressionBrand" | "_functionLikeDeclarationBrand" | "_jsDocTypeBrand" | "_leftHandSideExpressionBrand" | "_literalExpressionBrand" | "_memberExpressionBrand" | "_objectLiteralBrandBrand" | "_primaryExpressionBrand" | "_propertyAccessExpressionLikeQualifiedNameBrand" | "_statementBrand" | "_typeElementBrand" | "_typeNodeBrand" | "_unaryExpressionBrand" | "_updateExpressionBrand";
export declare type CompilerNodeBrandPropertyNamesType = "_classElementBrand" | "_declarationBrand" | "_expressionBrand" | "_functionLikeDeclarationBrand" | "_jsDocTypeBrand" | "_leftHandSideExpressionBrand" | "_literalExpressionBrand" | "_memberExpressionBrand" | "_objectLiteralBrandBrand" | "_primaryExpressionBrand" | "_propertyAccessExpressionLikeQualifiedNameBrand" | "_statementBrand" | "_typeElementBrand" | "_typeNodeBrand" | "_unaryExpressionBrand" | "_updateExpressionBrand";
/**
* @deprecated Use CompilerNodeBrandPropertyNamesType.
*/
export declare type CompilerApiNodeBrandPropertyNamesType = CompilerNodeBrandPropertyNamesType;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"build": "rimraf dist && npx ttsc",
"test": "cross-env TS_NODE_COMPILER=\"ttypescript\" nyc --reporter=lcov mocha --opts mocha.opts",
"code-generate": "ts-node scripts/code-generate --ignoreDiagnostics",
"refactor": "ts-node scripts/refactor",
"refactor": "ts-node scripts/refactor --ignoreDiagnostics",
"output-wrapped-nodes": "ts-node scripts/outputWrappedNodesInfo",
"package": "npm run build && ts-node scripts/createDeclarationFile",
"publish-code-verification": "npm run code-verification && npm run ensure-no-declaration-file-errors && npm run ensure-declaration-file-not-changed",
Expand Down
24 changes: 20 additions & 4 deletions scripts/createCompilerNodeToWrappedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
import { ClassDeclaration, MethodDeclaration, MethodDeclarationStructure, MethodSignature, MethodSignatureStructure, JSDocStructure,
ParameterDeclarationStructure, SourceFile, InterfaceDeclaration, TypeGuards, SyntaxKind } from "ts-simple-ast";
import { hasDescendantBaseType } from "./common";
import { TsSimpleAstInspector } from "./inspectors";
import { TsSimpleAstInspector, WrappedNode } from "./inspectors";

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

export function createCompilerNodeToWrappedType(inspector: TsSimpleAstInspector) {
const project = inspector.getProject();
const kindToNodeMappingsFile = project.getSourceFileOrThrow("CompilerNodeToWrappedType.ts");
const wrappedNodes = inspector.getWrappedNodes();
const wrappedNodes = getWrappedNodesInDependencyOrder([...inspector.getWrappedNodes()]);
kindToNodeMappingsFile.removeText();

// add imports
Expand All @@ -36,13 +36,13 @@ export function createCompilerNodeToWrappedType(inspector: TsSimpleAstInspector)
let isFirst = true;
for (const wrapper of wrappedNodes) {
const nodes = wrapper.getAssociatedTsNodes();
if (nodes.length === 0 || wrapper.hasParent())
if (nodes.length === 0)
continue;
if (isFirst)
isFirst = false;
else
writer.newLine().indent();
writer.write(`[T] extends [ts.${nodes[0].getName()}] ? compiler.${wrapper.getName()} :`);
writer.write(`T extends ts.${nodes[0].getNameForType()} ? compiler.${wrapper.getName()} :`);
}
writer.write(" compiler.Node<T>");
}
Expand All @@ -51,3 +51,19 @@ export function createCompilerNodeToWrappedType(inspector: TsSimpleAstInspector)
kindToNodeMappingsFile.insertText(0, writer =>
writer.writeLine("// DO NOT EDIT - Automatically maintained by createCompilerNodeToWrappedType.ts"));
}

function getWrappedNodesInDependencyOrder(wrappedNodes: WrappedNode[]) {
// get the wrapped nodes with the base types at the end of the array
for (let i = 0; i < wrappedNodes.length; i++) {
const baseNodes = wrappedNodes[i].getBases();
for (const baseNode of baseNodes) {
const baseIndex = wrappedNodes.indexOf(baseNode);
if (baseIndex < i) {
wrappedNodes.splice(baseIndex, 1);
i--;
wrappedNodes.splice(i + 1, 0, baseNode);
}
}
}
return wrappedNodes;
}
2 changes: 1 addition & 1 deletion scripts/createDeclarationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ function hideBaseDeclarations() {

function removeImportTypes() {
for (const type of mainFile.getDescendantsOfKind(SyntaxKind.ImportType)) {
type.replaceWithText(type.getNodeProperty("qualifier" as any).getText());
type.replaceWithText(type.getText().replace(/import\([^\)]+\)\./, ""));
}
}
7 changes: 7 additions & 0 deletions scripts/inspectors/ts/TsNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export class TsNode {
return this.node.getName();
}

getNameForType() {
const typeParams = this.node.getTypeParameters().map(p => p.getDefaultNode() == null);
if (typeParams.length > 0)
return this.node.getName() + "<" + typeParams.map(_ => "any").join(", ") + ">";
return this.node.getName();
}

getInterface() {
return this.node;
}
Expand Down
16 changes: 16 additions & 0 deletions scripts/refactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import { InspectorFactory } from "./inspectors";
import { SyntaxKind, TypeGuards } from "ts-simple-ast";
import { ArrayUtils } from "../src/utils";

const factory = new InspectorFactory();
const tsaInspector = factory.getTsSimpleAstInspector();
Expand All @@ -20,6 +21,21 @@ for (let i = 0; i < sourceFiles.length; i++) {
const sourceFile = sourceFiles[i];
console.log(`[${i + 1}/${sourceFiles.length}] Updating: ${sourceFile.getFilePath()}`);
/* DON'T CHECK IN THE CHANGES WITHIN THIS BLOCK */
for (const method of ArrayUtils.flatten(sourceFile.getClasses().map(c => c.getInstanceMethods()))) {
const body = method.getBody();
if (body == null)
continue;
const returnStatement = body.getChildSyntaxList()!.getLastChildIfKind(SyntaxKind.ReturnStatement);
if (returnStatement == null)
continue;
const expression = returnStatement.getExpression();
if (expression == null || !TypeGuards.isCallExpression(expression))
continue;
if (expression.getExpression()!.getText() !== "this.getNodeFromCompilerNodeIfExists")
continue;
if (!method.getReturnType()!.isNullable())
method.setReturnType(method.getReturnTypeNodeOrThrow().getText() + " | undefined");
}
}

project.save();
Expand Down

0 comments on commit 384613a

Please sign in to comment.