-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#36, convert type node: string literals.
- Loading branch information
Showing
7 changed files
with
262 additions
and
6 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,73 @@ | ||
import { | ||
Node, | ||
SyntaxKind, | ||
TypeNode, | ||
} from "ts-morph"; | ||
|
||
import type { | ||
TypeStructures, | ||
} from "../snapshot/source/exports.js"; | ||
|
||
import type { | ||
SubstructureResolver, | ||
TypeNodeToTypeStructure, | ||
TypeNodeToTypeStructureConsole | ||
} from "./types/conversions.js"; | ||
|
||
const LiteralKeywords: ReadonlyMap<SyntaxKind, string> = new Map([ | ||
[SyntaxKind.AnyKeyword, "any"], | ||
[SyntaxKind.BooleanKeyword, "boolean"], | ||
[SyntaxKind.FalseKeyword, "false"], | ||
[SyntaxKind.NeverKeyword, "never"], | ||
[SyntaxKind.NumberKeyword, "number"], | ||
[SyntaxKind.NullKeyword, "null"], | ||
[SyntaxKind.ObjectKeyword, "object"], | ||
[SyntaxKind.StringKeyword, "string"], | ||
[SyntaxKind.SymbolKeyword, "symbol"], | ||
[SyntaxKind.TrueKeyword, "true"], | ||
[SyntaxKind.UndefinedKeyword, "undefined"], | ||
[SyntaxKind.UnknownKeyword, "unknown"], | ||
[SyntaxKind.VoidKeyword, "void"], | ||
]); | ||
|
||
export default function convertTypeNode( | ||
typeNode: TypeNode, | ||
consoleTrap: TypeNodeToTypeStructureConsole, | ||
subStructureResolver: SubstructureResolver | ||
): string | TypeStructures | null | ||
{ | ||
|
||
if (Node.isLiteralTypeNode(typeNode)) { | ||
typeNode = typeNode.getFirstChildOrThrow(); | ||
} | ||
const kind: SyntaxKind = typeNode.getKind(); | ||
|
||
const keyword = LiteralKeywords.get(kind); | ||
if (keyword) { | ||
return keyword; | ||
} | ||
|
||
reportConversionFailure( | ||
"unsupported type node", typeNode, typeNode, consoleTrap | ||
); | ||
void(subStructureResolver); | ||
return null; | ||
} | ||
convertTypeNode satisfies TypeNodeToTypeStructure; | ||
|
||
function reportConversionFailure( | ||
prefixMessage: string, | ||
failingNode: Node, | ||
failingTypeNode: TypeNode, | ||
conversionFailCallback: TypeNodeToTypeStructureConsole, | ||
): null | ||
{ | ||
const pos = failingNode.getPos(); | ||
const { line, column } = failingNode.getSourceFile().getLineAndColumnAtPos(pos); | ||
|
||
conversionFailCallback( | ||
`${prefixMessage}: "${failingNode.getKindName()}" at line ${line}, column ${column}`, | ||
failingTypeNode | ||
); | ||
return null; | ||
} |
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
73 changes: 73 additions & 0 deletions
73
stage_2_fullset/snapshot/source/bootstrap/convertTypeNode.ts
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,73 @@ | ||
import { | ||
Node, | ||
SyntaxKind, | ||
TypeNode, | ||
} from "ts-morph"; | ||
|
||
import type { | ||
TypeStructures, | ||
} from "../exports.js"; | ||
|
||
import type { | ||
SubstructureResolver, | ||
TypeNodeToTypeStructure, | ||
TypeNodeToTypeStructureConsole | ||
} from "./types/conversions.js"; | ||
|
||
const LiteralKeywords: ReadonlyMap<SyntaxKind, string> = new Map([ | ||
[SyntaxKind.AnyKeyword, "any"], | ||
[SyntaxKind.BooleanKeyword, "boolean"], | ||
[SyntaxKind.FalseKeyword, "false"], | ||
[SyntaxKind.NeverKeyword, "never"], | ||
[SyntaxKind.NumberKeyword, "number"], | ||
[SyntaxKind.NullKeyword, "null"], | ||
[SyntaxKind.ObjectKeyword, "object"], | ||
[SyntaxKind.StringKeyword, "string"], | ||
[SyntaxKind.SymbolKeyword, "symbol"], | ||
[SyntaxKind.TrueKeyword, "true"], | ||
[SyntaxKind.UndefinedKeyword, "undefined"], | ||
[SyntaxKind.UnknownKeyword, "unknown"], | ||
[SyntaxKind.VoidKeyword, "void"], | ||
]); | ||
|
||
export default function convertTypeNode( | ||
typeNode: TypeNode, | ||
consoleTrap: TypeNodeToTypeStructureConsole, | ||
subStructureResolver: SubstructureResolver | ||
): string | TypeStructures | null | ||
{ | ||
|
||
if (Node.isLiteralTypeNode(typeNode)) { | ||
typeNode = typeNode.getFirstChildOrThrow(); | ||
} | ||
const kind: SyntaxKind = typeNode.getKind(); | ||
|
||
const keyword = LiteralKeywords.get(kind); | ||
if (keyword) { | ||
return keyword; | ||
} | ||
|
||
reportConversionFailure( | ||
"unsupported type node", typeNode, typeNode, consoleTrap | ||
); | ||
void(subStructureResolver); | ||
return null; | ||
} | ||
convertTypeNode satisfies TypeNodeToTypeStructure; | ||
|
||
function reportConversionFailure( | ||
prefixMessage: string, | ||
failingNode: Node, | ||
failingTypeNode: TypeNode, | ||
conversionFailCallback: TypeNodeToTypeStructureConsole, | ||
): null | ||
{ | ||
const pos = failingNode.getPos(); | ||
const { line, column } = failingNode.getSourceFile().getLineAndColumnAtPos(pos); | ||
|
||
conversionFailCallback( | ||
`${prefixMessage}: "${failingNode.getKindName()}" at line ${line}, column ${column}`, | ||
failingTypeNode | ||
); | ||
return null; | ||
} |
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
110 changes: 110 additions & 0 deletions
110
stage_2_fullset/spec-snapshot/source/bootstrap/convertTypeNode.ts
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,110 @@ | ||
import { | ||
ModuleKind, | ||
ModuleResolutionKind, | ||
Project, | ||
ProjectOptions, | ||
ScriptTarget, | ||
TypeNode, | ||
VariableDeclaration, | ||
} from "ts-morph"; | ||
|
||
import { | ||
TypeStructures | ||
} from "#stage_two/snapshot/source/exports.js"; | ||
|
||
import { | ||
StructuresClassesMap | ||
} from "#stage_two/snapshot/source/internal-exports.js"; | ||
|
||
import convertTypeNode from "#stage_two/snapshot/source/bootstrap/convertTypeNode.js"; | ||
|
||
import type { | ||
TypeNodeToTypeStructureConsole | ||
} from "#stage_two/snapshot/source/bootstrap/types/conversions.js"; | ||
|
||
describe("convertTypeNode generates correct type structures, with type", () => { | ||
let declaration: VariableDeclaration; | ||
let structure: string | TypeStructures | null; | ||
|
||
let failMessage: string | undefined; | ||
let failNode: TypeNode | null; | ||
function failCallback(message: string, typeNode: TypeNode): void { | ||
failMessage = message; | ||
failNode = typeNode; | ||
} | ||
failCallback satisfies TypeNodeToTypeStructureConsole; | ||
|
||
beforeAll(() => { | ||
failMessage = undefined; | ||
failNode = null; | ||
|
||
const TSC_CONFIG: ProjectOptions = { | ||
"compilerOptions": { | ||
"lib": ["es2022"], | ||
"module": ModuleKind.ESNext, | ||
"target": ScriptTarget.ESNext, | ||
"moduleResolution": ModuleResolutionKind.NodeNext, | ||
}, | ||
skipAddingFilesFromTsConfig: true, | ||
useInMemoryFileSystem: true, | ||
}; | ||
|
||
const project = new Project(TSC_CONFIG); | ||
const sourceFile = project.createSourceFile("file.ts", ` | ||
const refSymbol = Symbol("reference symbol"); | ||
enum NumberEnum { | ||
one = 1, | ||
two, | ||
three, | ||
} | ||
const A: string; | ||
`.trim() + "\n"); | ||
declaration = sourceFile.getVariableDeclarationOrThrow("A"); | ||
}); | ||
beforeEach(() => { | ||
failMessage = undefined; | ||
failNode = null; | ||
}); | ||
|
||
afterEach(() => structure = null); | ||
|
||
function setTypeStructure( | ||
rawType: string, | ||
console: TypeNodeToTypeStructureConsole | ||
): void | ||
{ | ||
declaration.setType(rawType); | ||
const typeNode = declaration.getTypeNodeOrThrow(); | ||
structure = convertTypeNode( | ||
typeNode, | ||
console, | ||
node => StructuresClassesMap.clone(node.getStructure()) | ||
); | ||
} | ||
|
||
describe("string literal", () => { | ||
function stringSpec(s: string): void { | ||
it(s, () => { | ||
setTypeStructure(s, failCallback); | ||
expect(structure).toBe(s); | ||
expect(failMessage).toBe(undefined); | ||
expect(failNode).toBe(null); | ||
}); | ||
} | ||
|
||
stringSpec("any"); | ||
stringSpec("boolean"); | ||
stringSpec("false"); | ||
stringSpec("never"); | ||
stringSpec("number"); | ||
stringSpec("null"); | ||
stringSpec("object"); | ||
stringSpec("string"); | ||
stringSpec("symbol"); | ||
stringSpec("true"); | ||
stringSpec("undefined"); | ||
stringSpec("unknown"); | ||
stringSpec("void"); | ||
}); | ||
}); |