-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: #507 - Code verification for CompilerNodeToWrappedType.
- Loading branch information
Showing
2 changed files
with
56 additions
and
0 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
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,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); | ||
} | ||
} |