typenative 0.0.20 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/npm-publish-github-packages.yml +14 -6
- package/CHANGELOG.md +70 -0
- package/README.md +41 -4
- package/TODO.md +38 -0
- package/bin/cli.js +62 -0
- package/bin/index.js +105 -134
- package/bin/main.js +31 -0
- package/bin/parse-native.js +37 -0
- package/bin/parse-node.js +110 -0
- package/bin/prompt.js +29 -0
- package/bin/transpiler.js +4277 -629
- package/package.json +56 -37
- package/tsparser/go.mod +14 -0
- package/tsparser/go.sum +22 -0
- package/tsparser/main.go +279 -0
- package/tsparser/tsparser.exe +4 -0
- package/types/typenative-npm.d.ts +44 -0
package/bin/main.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Self-hosting entry point: this file is what TypeNative compiles to produce
|
|
2
|
+
// the native compiler binary. Interactive prompts use the transpilable
|
|
3
|
+
// prompt module (sync stdin) instead of inquirer.
|
|
4
|
+
import { run, createProject } from './index.js';
|
|
5
|
+
import { ask, askMultiline, confirm } from './prompt.js';
|
|
6
|
+
import { parseAstJsonNative } from './parse-native.js';
|
|
7
|
+
(function main() {
|
|
8
|
+
const scriptMode = process.argv.findIndex((a) => a === '--script') > -1;
|
|
9
|
+
const newCommand = process.argv.findIndex((a) => a === '--new') > -1;
|
|
10
|
+
const sourceIndex = process.argv.findIndex((a) => a === '--source');
|
|
11
|
+
let source = sourceIndex > -1 ? process.argv[sourceIndex + 1] : null;
|
|
12
|
+
const outputIndex = process.argv.findIndex((a) => a === '--output');
|
|
13
|
+
let output = outputIndex > -1 ? process.argv[outputIndex + 1] : null;
|
|
14
|
+
if (newCommand) {
|
|
15
|
+
const projectName = ask('Enter Project Name:');
|
|
16
|
+
const installDependencies = confirm('Do you want to install dependencies?');
|
|
17
|
+
createProject(projectName, installDependencies);
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
let tsCode = null;
|
|
21
|
+
if (!newCommand && !scriptMode && !source) {
|
|
22
|
+
source = ask('Enter Path to typescript main file:');
|
|
23
|
+
}
|
|
24
|
+
if (!newCommand && !scriptMode && !output) {
|
|
25
|
+
output = ask('Enter Output Path:');
|
|
26
|
+
}
|
|
27
|
+
if (!newCommand && scriptMode && !source) {
|
|
28
|
+
tsCode = askMultiline('Write your typescript code here:');
|
|
29
|
+
}
|
|
30
|
+
run({ source, output, scriptMode, tsCode, parse: parseAstJsonNative });
|
|
31
|
+
})();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Self-hosted parser: spawns the tsparser Go tool (tsparser/main.go) to parse
|
|
2
|
+
// TypeScript into the normalized JSON AST. Fully transpilable — used by the
|
|
3
|
+
// native compiler binary instead of the typescript npm package.
|
|
4
|
+
import * as path from 'node:path';
|
|
5
|
+
import * as fs from 'node:fs';
|
|
6
|
+
import * as childProcess from 'node:child_process';
|
|
7
|
+
export function parseAstJsonNative(code) {
|
|
8
|
+
const exe = findTsparser();
|
|
9
|
+
const result = childProcess.spawnSync(exe, [], { input: code, encoding: 'utf-8' });
|
|
10
|
+
if (result.status !== 0) {
|
|
11
|
+
throw new Error('tsparser failed: ' + result.stderr);
|
|
12
|
+
}
|
|
13
|
+
return JSON.parse(result.stdout);
|
|
14
|
+
}
|
|
15
|
+
// Locates the tsparser binary: TYPENATIVE_TSPARSER env var, next to this
|
|
16
|
+
// executable, or in a sibling tsparser/ directory (dev checkout layout).
|
|
17
|
+
function findTsparser() {
|
|
18
|
+
const exeName = platformIsWindows() ? 'tsparser.exe' : 'tsparser';
|
|
19
|
+
const envPath = process.env.TYPENATIVE_TSPARSER;
|
|
20
|
+
if (envPath && fs.existsSync(envPath))
|
|
21
|
+
return envPath;
|
|
22
|
+
const thisDir = path.dirname(path.resolve(process.argv[0]));
|
|
23
|
+
const candidates = [
|
|
24
|
+
path.join(thisDir, exeName),
|
|
25
|
+
path.join(thisDir, 'tsparser', exeName),
|
|
26
|
+
path.join(thisDir, '..', 'tsparser', exeName),
|
|
27
|
+
path.join(thisDir, '..', '..', 'tsparser', exeName)
|
|
28
|
+
];
|
|
29
|
+
for (const candidate of candidates) {
|
|
30
|
+
if (fs.existsSync(candidate))
|
|
31
|
+
return candidate;
|
|
32
|
+
}
|
|
33
|
+
throw new Error('tsparser binary not found; set TYPENATIVE_TSPARSER');
|
|
34
|
+
}
|
|
35
|
+
function platformIsWindows() {
|
|
36
|
+
return process.platform === 'win32';
|
|
37
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
// Node-side AST adapter: converts a `typescript` npm package AST into the same
|
|
2
|
+
// normalized JSON shape that the `tsparser` Go tool emits
|
|
3
|
+
// (tsparser/main.go). The transpiler consumes this shape, so dev-CLI output and
|
|
4
|
+
// self-hosted (Go parser) output are field-compatible.
|
|
5
|
+
//
|
|
6
|
+
// Node-only module: imports the `typescript` package directly and is injected
|
|
7
|
+
// into the transpiler by the Node CLI wrapper (src/cli.ts). The self-hosted
|
|
8
|
+
// binary uses src/parse-native.ts instead.
|
|
9
|
+
import ts from 'typescript';
|
|
10
|
+
// Internal ts.Node bookkeeping properties that must not leak into the JSON
|
|
11
|
+
const SKIP_PROPS = new Set([
|
|
12
|
+
'pos',
|
|
13
|
+
'end',
|
|
14
|
+
'flags',
|
|
15
|
+
'modifierFlagsCache',
|
|
16
|
+
'transformFlags',
|
|
17
|
+
'parent',
|
|
18
|
+
'original',
|
|
19
|
+
'symbol',
|
|
20
|
+
'id',
|
|
21
|
+
'jsDoc',
|
|
22
|
+
'flowNode',
|
|
23
|
+
'contextFlags',
|
|
24
|
+
'parserContextFlags',
|
|
25
|
+
'excludeFlags',
|
|
26
|
+
'modifierFlags',
|
|
27
|
+
'flowId',
|
|
28
|
+
'symbolCount',
|
|
29
|
+
'identifierCount',
|
|
30
|
+
'nodeArrayCache',
|
|
31
|
+
'parsedByParser',
|
|
32
|
+
'autoGenerateKind',
|
|
33
|
+
'autoGenerateId',
|
|
34
|
+
'emitNode',
|
|
35
|
+
'localSymbol',
|
|
36
|
+
// 'text'/'escapedText' are only emitted for token-like kinds (TEXT_KINDS);
|
|
37
|
+
// SourceFile.text would otherwise leak the whole source into the AST
|
|
38
|
+
'text',
|
|
39
|
+
'escapedText',
|
|
40
|
+
'endOfFileToken',
|
|
41
|
+
'externalModuleIndicator',
|
|
42
|
+
'commonJsModuleIndicator',
|
|
43
|
+
'localJsxNamespace',
|
|
44
|
+
'localJsxFactory',
|
|
45
|
+
'localJsxFragmentFactory'
|
|
46
|
+
]);
|
|
47
|
+
const TEXT_KINDS = new Set([
|
|
48
|
+
ts.SyntaxKind.Identifier,
|
|
49
|
+
ts.SyntaxKind.PrivateIdentifier,
|
|
50
|
+
ts.SyntaxKind.StringLiteral,
|
|
51
|
+
ts.SyntaxKind.NumericLiteral,
|
|
52
|
+
ts.SyntaxKind.BigIntLiteral,
|
|
53
|
+
ts.SyntaxKind.NoSubstitutionTemplateLiteral,
|
|
54
|
+
ts.SyntaxKind.RegularExpressionLiteral,
|
|
55
|
+
ts.SyntaxKind.TemplateHead,
|
|
56
|
+
ts.SyntaxKind.TemplateMiddle,
|
|
57
|
+
ts.SyntaxKind.TemplateTail,
|
|
58
|
+
ts.SyntaxKind.JsxText
|
|
59
|
+
]);
|
|
60
|
+
// Reverse enum map preferring canonical names over aliases
|
|
61
|
+
// (ts.SyntaxKind[8] is "FirstLiteralToken", the alias of NumericLiteral)
|
|
62
|
+
const KIND_NAMES = (() => {
|
|
63
|
+
const names = {};
|
|
64
|
+
for (const key of Object.keys(ts.SyntaxKind)) {
|
|
65
|
+
const val = ts.SyntaxKind[key];
|
|
66
|
+
if (typeof val === 'number' && names[val] === undefined)
|
|
67
|
+
names[val] = key;
|
|
68
|
+
}
|
|
69
|
+
return names;
|
|
70
|
+
})();
|
|
71
|
+
function isNode(v) {
|
|
72
|
+
return typeof v === 'object' && v !== null && typeof v.kind === 'number' && typeof v.pos === 'number';
|
|
73
|
+
}
|
|
74
|
+
function convertNode(node) {
|
|
75
|
+
const obj = { kind: KIND_NAMES[node.kind] ?? String(node.kind) };
|
|
76
|
+
if (TEXT_KINDS.has(node.kind)) {
|
|
77
|
+
obj.text = node.text ?? node.escapedText;
|
|
78
|
+
}
|
|
79
|
+
for (const key of Object.keys(node)) {
|
|
80
|
+
if (SKIP_PROPS.has(key))
|
|
81
|
+
continue;
|
|
82
|
+
const v = node[key];
|
|
83
|
+
if (typeof v === 'function')
|
|
84
|
+
continue;
|
|
85
|
+
if (v === null || v === undefined)
|
|
86
|
+
continue;
|
|
87
|
+
if (isNode(v)) {
|
|
88
|
+
obj[key] = convertNode(v);
|
|
89
|
+
}
|
|
90
|
+
else if (Array.isArray(v)) {
|
|
91
|
+
const arr = v.filter((e) => isNode(e)).map((e) => convertNode(e));
|
|
92
|
+
if (arr.length > 0)
|
|
93
|
+
obj[key] = arr;
|
|
94
|
+
}
|
|
95
|
+
else if (typeof v === 'number' && (key === 'operator' || key === 'token')) {
|
|
96
|
+
// operator / heritage-clause token: emit the kind name (matches the Go producer)
|
|
97
|
+
obj[key] = KIND_NAMES[v] ?? String(v);
|
|
98
|
+
}
|
|
99
|
+
else if (typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean') {
|
|
100
|
+
if (key !== 'kind')
|
|
101
|
+
obj[key] = v;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return obj;
|
|
105
|
+
}
|
|
106
|
+
// Parse TypeScript source into the normalized JSON AST shape.
|
|
107
|
+
export function parseAstJson(code) {
|
|
108
|
+
const sourceFile = ts.createSourceFile('main.ts', code, ts.ScriptTarget.ES2020, true, ts.ScriptKind.TS);
|
|
109
|
+
return convertNode(sourceFile);
|
|
110
|
+
}
|
package/bin/prompt.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
// Interactive prompts for the self-hosted native binary.
|
|
2
|
+
// Implemented exclusively with TypeNative-mapped APIs (node:readline → Go helper)
|
|
3
|
+
// so this module transpiles. Under Node the namespace import of node:readline is
|
|
4
|
+
// valid but `question` is only available once transpiled — the Node CLI wrapper
|
|
5
|
+
// (src/cli.ts) resolves prompts via inquirer instead and never calls these.
|
|
6
|
+
import * as readlineSync from 'node:readline';
|
|
7
|
+
export function ask(message) {
|
|
8
|
+
let input = readlineSync.question(message + ' ');
|
|
9
|
+
while (input.trim() === '') {
|
|
10
|
+
console.log('Please enter a non-empty value.');
|
|
11
|
+
input = readlineSync.question(message + ' ');
|
|
12
|
+
}
|
|
13
|
+
return input.trim();
|
|
14
|
+
}
|
|
15
|
+
export function askMultiline(message) {
|
|
16
|
+
console.log(message);
|
|
17
|
+
console.log("(finish with a line containing only 'END')");
|
|
18
|
+
const lines = [];
|
|
19
|
+
let line = readlineSync.question('');
|
|
20
|
+
while (line.trim() !== 'END') {
|
|
21
|
+
lines.push(line);
|
|
22
|
+
line = readlineSync.question('');
|
|
23
|
+
}
|
|
24
|
+
return lines.join('\n');
|
|
25
|
+
}
|
|
26
|
+
export function confirm(message) {
|
|
27
|
+
const answer = readlineSync.question(message + ' (y/n): ');
|
|
28
|
+
return answer.trim().toLowerCase() === 'y';
|
|
29
|
+
}
|