Skip to content

Commit 784daf5

Browse files
--ignore-not-supported, GenericRecords to object, resolve namespaces
Do not throw exception on non supported features. GenericRecords transforms to object Namespaces content are parsed
1 parent e87d585 commit 784daf5

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
dist/
2+
.idea
23

34
# Logs
45
logs
@@ -58,3 +59,4 @@ typings/
5859

5960
# dotenv environment variables file
6061
.env
62+

lib/index.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const ignoreNode = "";
2020
export interface ICompilerOptions {
2121
format?: "ts" | "js:esm" | "js:cjs"
2222
ignoreGenerics?: boolean;
23+
ignoreNotSupported?: boolean;
2324
ignoreIndexSignature?: boolean;
2425
inlineImports?: boolean;
2526
}
@@ -37,7 +38,7 @@ export class Compiler {
3738
if (!topNode) {
3839
throw new Error(`Can't process ${filePath}: ${collectDiagnostics(program)}`);
3940
}
40-
options = {format: defaultFormat, ignoreGenerics: false, ignoreIndexSignature: false, inlineImports: false, ...options}
41+
options = {format: defaultFormat, ignoreGenerics: false, ignoreNotSupported: false, ignoreIndexSignature: false, inlineImports: false, ...options}
4142
return new Compiler(checker, options, topNode).compileNode(topNode);
4243
}
4344

@@ -80,6 +81,8 @@ export class Compiler {
8081
case ts.SyntaxKind.ExportDeclaration:
8182
case ts.SyntaxKind.ImportDeclaration:
8283
return this._compileImportDeclaration(node as ts.ImportDeclaration);
84+
case ts.SyntaxKind.ModuleDeclaration:
85+
return node.getSourceFile().statements.map(this.compileNode).filter(s => s).join("\n\n");
8386
case ts.SyntaxKind.SourceFile: return this._compileSourceFile(node as ts.SourceFile);
8487
case ts.SyntaxKind.AnyKeyword: return '"any"';
8588
case ts.SyntaxKind.NumberKeyword: return '"number"';
@@ -97,6 +100,12 @@ export class Compiler {
97100
}
98101
// Skip top-level statements that we haven't handled.
99102
if (ts.isSourceFile(node.parent!)) { return ""; }
103+
104+
if (this.options.ignoreNotSupported) {
105+
console.log(`Node ${ts.SyntaxKind[node.kind]} not supported by ts-interface-builder: ` +
106+
node.getText());
107+
return node.getText();
108+
}
100109
throw new Error(`Node ${ts.SyntaxKind[node.kind]} not supported by ts-interface-builder: ` +
101110
node.getText());
102111
}
@@ -139,6 +148,8 @@ export class Compiler {
139148
return this.compileNode(node.typeArguments[0]);
140149
} else if (node.typeName.getText() === "Array") {
141150
return `t.array(${this.compileNode(node.typeArguments[0])})`;
151+
} else if (node.typeName.getText() === "Record") {
152+
return '"object"';
142153
} else if (this.options.ignoreGenerics) {
143154
return '"any"';
144155
} else {
@@ -302,6 +313,7 @@ export function main() {
302313
.option("--format <format>", `Format to use for output; options are 'ts' (default), 'js:esm', 'js:cjs'`)
303314
.option("-g, --ignore-generics", `Ignores generics`)
304315
.option("-i, --ignore-index-signature", `Ignores index signature`)
316+
.option("--ignore-not-supported", `Skip all not supported code`)
305317
.option("--inline-imports", `Traverses the full import tree and inlines all types into output`)
306318
.option("-s, --suffix <suffix>", `Suffix to append to generated files (default ${defaultSuffix})`, defaultSuffix)
307319
.option("-o, --outDir <path>", `Directory for output files; same as source file if omitted`)
@@ -317,6 +329,7 @@ export function main() {
317329
const options: ICompilerOptions = {
318330
format: commander.format || defaultFormat,
319331
ignoreGenerics: commander.ignoreGenerics,
332+
ignoreNotSupported: commander.ignoreNotSupported,
320333
ignoreIndexSignature: commander.ignoreIndexSignature,
321334
inlineImports: commander.inlineImports,
322335
};

lib/macro.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ const macroHandler: MacroHandler = (params) => {
4848

4949
// Begin mutations
5050

51-
programPath.scope.rename(tsInterfaceCheckerIdentifier);
52-
programPath.scope.rename(onceIdentifier);
51+
programPath?.scope.rename(tsInterfaceCheckerIdentifier);
52+
programPath?.scope.rename(onceIdentifier);
5353
toReplace.forEach(({ callPath, id }) => {
5454
scopeRenameRecursive(callPath.scope, id);
5555
});
@@ -127,7 +127,7 @@ const macroHandler: MacroHandler = (params) => {
127127
}
128128

129129
function prependProgramStatement(statement: types.Statement) {
130-
(programPath.get("body.0") as NodePath).insertBefore(statement);
130+
(programPath?.get("body.0") as NodePath).insertBefore(statement);
131131
}
132132
};
133133

tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"compilerOptions": {
3-
"target": "es6",
4-
"strict": true,
3+
"target": "es2018",
4+
"strict": false,
55
"noUnusedLocals": true,
66
"module": "commonjs",
77
"baseUrl": ".",

0 commit comments

Comments
 (0)