Skip to content

Commit

Permalink
chore: #507 - Code verification for CompilerNodeToWrappedType.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Dec 2, 2018
1 parent c89615f commit 03f509e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
3 changes: 3 additions & 0 deletions scripts/verification/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ensureStructuresMatchClasses } from "./ensureStructuresMatchClasses";
import { ensureClassesImplementStructureMethods } from "./ensureClassesImplementStructureMethods";
import { ensurePublicApiHasTests } from "./ensurePublicApiHasTests";
import { validatePublicApiClassMemberNames } from "./validatePublicApiClassMemberNames";
import { validateCompilerNodeToWrappedType } from "./validateCompilerNodeToWrappedType";
import { Problem } from "./Problem";

const args = process.argv.slice(2);
Expand All @@ -31,6 +32,8 @@ if (checkHasArg("ensure-public-api-has-tests"))
ensurePublicApiHasTests(factory.getTsSimpleAstInspector(), addProblem);
if (checkHasArg("validate-public-api-class-member-names"))
validatePublicApiClassMemberNames(factory.getTsSimpleAstInspector(), addProblem);
if (checkHasArg("validate-compiler-node-to-wrapped-type"))
validateCompilerNodeToWrappedType(factory.getTsSimpleAstInspector(), addProblem);

if (args.length > 0)
console.error(`Unknown args: ${args}`);
Expand Down
53 changes: 53 additions & 0 deletions scripts/verification/validateCompilerNodeToWrappedType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* Code Verification - Validate CompilerNodeToWrappedType
* ------------------------------------------------------
* This code verification validates that the CompilerNodeToWrappedType
* type alias properly converts the passed in compiler node type to a
* wrapped node type.
* ------------------------------------------------------
*/
import { TypeAliasDeclarationStructure } from "ts-simple-ast";
import { TsSimpleAstInspector } from "../inspectors";
import { Problem } from "./Problem";

export function validateCompilerNodeToWrappedType(inspector: TsSimpleAstInspector, addProblem: (problem: Problem) => void) {
const wrappedNodes = inspector.getWrappedNodes();
const sourceFile = inspector.getProject().getSourceFileOrThrow("CompilerNodeToWrappedType.ts");
const initialText = sourceFile.getFullText();

try {
const structures: TypeAliasDeclarationStructure[] = [];
for (let i = 0; i < wrappedNodes.length; i++) {
const wrapper = wrappedNodes[i];
const nodes = wrapper.getAssociatedTsNodes();
if (nodes.length === 0)
continue;

structures.push({
name: `${wrapper.getName()}_test`,
type: `CompilerNodeToWrappedType<ts.${nodes[0].getNameForType()}>`
});
}
const addedNodes = sourceFile.addTypeAliases(structures);
const diagnostics = sourceFile.getPreEmitDiagnostics();

if (diagnostics.length > 0) {
console.log(inspector.getProject().formatDiagnosticsWithColorAndContext(diagnostics));
throw new Error("Stopping -- Compile errors in validation.");
}

for (const addedNode of addedNodes) {
const typeText = addedNode.getType().getText(addedNode).replace(/\<.*\>$/, "");
const nodeText = "compiler." + addedNode.getName().replace("_test", "");
if (typeText !== nodeText) {
addProblem({
filePath: sourceFile.getFilePath(),
lineNumber: sourceFile.getTypeAliasOrThrow("CompilerNodeToWrappedType").getStartLineNumber(),
message: `Could not get wrapped type from node "${nodeText.replace("compiler.", "")}". Got "${typeText}".`
});
}
}
} finally {
sourceFile.replaceWithText(initialText);
}
}

0 comments on commit 03f509e

Please sign in to comment.