@backtickjs/tsc 0.2.2 → 0.2.4
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/README.md +11 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +17 -12
- package/dist/index.js.map +1 -1
- package/package.json +11 -4
- package/src/index.ts +19 -18
package/README.md
CHANGED
|
@@ -36,6 +36,17 @@ backtick-tsc --emitDeclarationOnly
|
|
|
36
36
|
|
|
37
37
|
A script's declaration carries its real type, where plain `tsc` would write `Client<unknown>`, and its parameter names read as you wrote them.
|
|
38
38
|
|
|
39
|
+
To check files in-process, `createProgram` is `ts.createProgram` as `backtick-tsc` creates one. It takes the options form, with a host:
|
|
40
|
+
|
|
41
|
+
```ts
|
|
42
|
+
import { createProgram } from "@backtickjs/tsc";
|
|
43
|
+
import ts from "typescript";
|
|
44
|
+
|
|
45
|
+
const host = ts.createCompilerHost(options);
|
|
46
|
+
const program = createProgram({ rootNames: ["App.tsx"], options, host });
|
|
47
|
+
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
48
|
+
```
|
|
49
|
+
|
|
39
50
|
## Notes
|
|
40
51
|
|
|
41
52
|
- It writes declarations only. Run it with `--noEmit` or `--emitDeclarationOnly`; any other emit is refused with an error, because the JavaScript it would write is the code the type checker reads, not code that runs. To build JavaScript, use a loader ([`@backtickjs/node-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/node-plugin), [`@backtickjs/bun-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/bun-plugin)) or `tspc` with [`@backtickjs/tspatch-plugin`](https://github.com/backtickjs/backtick/tree/main/packages/tspatch-plugin).
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -2,16 +2,17 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
import { unmangle } from "@backtickjs/compiler";
|
|
3
3
|
import { getBacktickLanguagePlugin } from "@backtickjs/language-plugin";
|
|
4
4
|
import { getBacktickDiagnostics, unmangleDiagnostic, } from "@backtickjs/language-service";
|
|
5
|
+
import { proxyCreateProgram } from "@volar/typescript/lib/node/proxyCreateProgram.js";
|
|
5
6
|
import { fillSourceFileText } from "@volar/typescript/lib/node/transform.js";
|
|
6
7
|
import { runTsc } from "@volar/typescript/lib/quickstart/runTsc.js";
|
|
8
|
+
// The standalone TypeScript API, not the `ts` `runTsc` hands its callback: that
|
|
9
|
+
// one is its internal tsc.js CLI bundle, whose namespace members are lazy
|
|
10
|
+
// `eval`-based getters that throw when read outside their module scope
|
|
11
|
+
// (Backtick's `parseFile` reads `ts.ScriptTarget`).
|
|
12
|
+
import ts from "typescript";
|
|
7
13
|
const require = createRequire(import.meta.url);
|
|
14
|
+
patchDecorateProgram();
|
|
8
15
|
export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
9
|
-
// Use the standalone TypeScript API, not the `ts` handed to the callback: that
|
|
10
|
-
// one is `runTsc`'s internal tsc.js CLI bundle, whose namespace members are
|
|
11
|
-
// lazy `eval`-based getters that throw when read outside their module scope
|
|
12
|
-
// (Backtick's `parseFile` reads `ts.ScriptTarget`).
|
|
13
|
-
const ts = require("typescript");
|
|
14
|
-
patchDecorateProgram(ts);
|
|
15
16
|
return runTsc(tscPath,
|
|
16
17
|
// No extra file extensions: Backtick virtualizes standard .ts/.tsx/.js/.jsx
|
|
17
18
|
// in place, matching the TS server plugins (which also pass []).
|
|
@@ -19,6 +20,10 @@ export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
|
19
20
|
languagePlugins: [getBacktickLanguagePlugin(ts, (fileName) => fileName)],
|
|
20
21
|
}));
|
|
21
22
|
}
|
|
23
|
+
// `ts.createProgram` as `backtick-tsc` creates one, to check files in-process.
|
|
24
|
+
export const createProgram = proxyCreateProgram(ts, ts.createProgram, () => ({
|
|
25
|
+
languagePlugins: [getBacktickLanguagePlugin(ts, (fileName) => fileName)],
|
|
26
|
+
}));
|
|
22
27
|
// What this tool is for beyond checking: a package that exports a client script
|
|
23
28
|
// gets a declaration carrying the script's real type. Plain `tsc` types
|
|
24
29
|
// `cs`...`` by the tag's own signature, `Client<unknown>`, because the
|
|
@@ -30,7 +35,7 @@ export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
|
30
35
|
// the emitter prints them, on identifiers alone, so nothing inside a string
|
|
31
36
|
// literal is touched. Not at the file system: `sys.writeFile` writes through
|
|
32
37
|
// `openSync`, and the CLI bundle's `sys` is not this module's anyway.
|
|
33
|
-
function unmangleDeclarations(
|
|
38
|
+
function unmangleDeclarations(program) {
|
|
34
39
|
const emit = program.emit.bind(program);
|
|
35
40
|
program.emit = (targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, customTransformers) => emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles, {
|
|
36
41
|
...customTransformers,
|
|
@@ -72,16 +77,16 @@ function refuseJavaScriptEmit(options) {
|
|
|
72
77
|
}
|
|
73
78
|
// Surfaces Backtick's own compiler diagnostics through `tsc` by wrapping the
|
|
74
79
|
// program's `getSemanticDiagnostics`. This has to patch Volar's `decorateProgram`
|
|
75
|
-
// module in place:
|
|
76
|
-
//
|
|
77
|
-
//
|
|
78
|
-
function patchDecorateProgram(
|
|
80
|
+
// module in place: Volar's program proxy calls the module's export by property
|
|
81
|
+
// access at program-creation time, so reassigning it here is picked up. Once,
|
|
82
|
+
// when this module loads, for `run` and `createProgram` alike.
|
|
83
|
+
function patchDecorateProgram() {
|
|
79
84
|
const decorateProgramModule = require("@volar/typescript/lib/node/decorateProgram.js");
|
|
80
85
|
const volarDecorateProgram = decorateProgramModule.decorateProgram;
|
|
81
86
|
decorateProgramModule.decorateProgram = (language, program) => {
|
|
82
87
|
volarDecorateProgram(language, program);
|
|
83
88
|
refuseJavaScriptEmit(program.getCompilerOptions());
|
|
84
|
-
unmangleDeclarations(
|
|
89
|
+
unmangleDeclarations(program);
|
|
85
90
|
const getSemanticDiagnostics = program.getSemanticDiagnostics.bind(program);
|
|
86
91
|
program.getSemanticDiagnostics = (sourceFile, cancellationToken) => {
|
|
87
92
|
const files = sourceFile ? [sourceFile] : program.getSourceFiles();
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EACL,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,4CAA4C,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC5C,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAChD,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAC;AACxE,OAAO,EACL,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,kBAAkB,EAAE,MAAM,kDAAkD,CAAC;AACtF,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAC;AAC7E,OAAO,EAAE,MAAM,EAAE,MAAM,4CAA4C,CAAC;AACpE,gFAAgF;AAChF,0EAA0E;AAC1E,uEAAuE;AACvE,oDAAoD;AACpD,OAAO,EAAE,MAAM,YAAY,CAAC;AAE5B,MAAM,OAAO,GAAG,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAE/C,oBAAoB,EAAE,CAAC;AAEvB,MAAM,UAAU,GAAG,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,uBA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OAAO,CAAC,kBAAkB,EAAE,CAAC,CAAC;QACnD,oBAAoB,CAAC,OAAO,CAAC,CAAC;QAE9B,MAAM,sBAAsB,GAAG,OAAO,CAAC,sBAAsB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5E,OAAO,CAAC,sBAAsB,GAAG,CAAC,UAAU,EAAE,iBAAiB,EAAE,EAAE;YACjE,MAAM,KAAK,GAAG,UAAU,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;YACnE,OAAO;gBACL,GAAG,sBAAsB,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC,GAAG,CAC1D,kBAAkB,CACnB;gBACD,GAAG,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE;oBACxB,MAAM,WAAW,GAAG,sBAAsB,CACxC,QAAQ,EACR,IAAI,EACJ,IAAI,CAAC,QAAQ,CACd,CAAC;oBACF,IAAI,WAAW,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;wBAC3B,mEAAmE;wBACnE,4DAA4D;wBAC5D,iEAAiE;wBACjE,oEAAoE;wBACpE,oEAAoE;wBACpE,kDAAkD;wBAClD,kBAAkB,CAAC,QAAQ,EAAE,IAAI,CAAC,CAAC;oBACrC,CAAC;oBACD,OAAO,WAAW,CAAC;gBACrB,CAAC,CAAC;aACH,CAAC;QACJ,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backtickjs/tsc",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.4",
|
|
4
4
|
"description": "backtick-tsc: tsc that type-checks Backtick client scripts and every splice crossing into them",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"backtick",
|
|
@@ -24,6 +24,13 @@
|
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"author": "Jean-François Bisson <jf@backtickjs.com>",
|
|
26
26
|
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./bin/backtick-tsc.js": "./bin/backtick-tsc.js"
|
|
33
|
+
},
|
|
27
34
|
"bin": {
|
|
28
35
|
"backtick-tsc": "./bin/backtick-tsc.js"
|
|
29
36
|
},
|
|
@@ -37,9 +44,9 @@
|
|
|
37
44
|
"access": "public"
|
|
38
45
|
},
|
|
39
46
|
"dependencies": {
|
|
40
|
-
"@backtickjs/compiler": "^0.2.
|
|
41
|
-
"@backtickjs/language-service": "^0.2.
|
|
42
|
-
"@backtickjs/language-plugin": "^0.2.
|
|
47
|
+
"@backtickjs/compiler": "^0.2.4",
|
|
48
|
+
"@backtickjs/language-service": "^0.2.4",
|
|
49
|
+
"@backtickjs/language-plugin": "^0.2.4",
|
|
43
50
|
"@volar/typescript": "^2.4.28"
|
|
44
51
|
},
|
|
45
52
|
"peerDependencies": {
|
package/src/index.ts
CHANGED
|
@@ -5,21 +5,20 @@ import {
|
|
|
5
5
|
getBacktickDiagnostics,
|
|
6
6
|
unmangleDiagnostic,
|
|
7
7
|
} from "@backtickjs/language-service";
|
|
8
|
+
import { proxyCreateProgram } from "@volar/typescript/lib/node/proxyCreateProgram.js";
|
|
8
9
|
import { fillSourceFileText } from "@volar/typescript/lib/node/transform.js";
|
|
9
10
|
import { runTsc } from "@volar/typescript/lib/quickstart/runTsc.js";
|
|
10
|
-
|
|
11
|
+
// The standalone TypeScript API, not the `ts` `runTsc` hands its callback: that
|
|
12
|
+
// one is its internal tsc.js CLI bundle, whose namespace members are lazy
|
|
13
|
+
// `eval`-based getters that throw when read outside their module scope
|
|
14
|
+
// (Backtick's `parseFile` reads `ts.ScriptTarget`).
|
|
15
|
+
import ts from "typescript";
|
|
11
16
|
|
|
12
17
|
const require = createRequire(import.meta.url);
|
|
13
18
|
|
|
14
|
-
|
|
15
|
-
// Use the standalone TypeScript API, not the `ts` handed to the callback: that
|
|
16
|
-
// one is `runTsc`'s internal tsc.js CLI bundle, whose namespace members are
|
|
17
|
-
// lazy `eval`-based getters that throw when read outside their module scope
|
|
18
|
-
// (Backtick's `parseFile` reads `ts.ScriptTarget`).
|
|
19
|
-
const ts = require("typescript") as typeof import("typescript");
|
|
20
|
-
|
|
21
|
-
patchDecorateProgram(ts);
|
|
19
|
+
patchDecorateProgram();
|
|
22
20
|
|
|
21
|
+
export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
23
22
|
return runTsc(
|
|
24
23
|
tscPath,
|
|
25
24
|
// No extra file extensions: Backtick virtualizes standard .ts/.tsx/.js/.jsx
|
|
@@ -31,6 +30,11 @@ export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
|
31
30
|
);
|
|
32
31
|
}
|
|
33
32
|
|
|
33
|
+
// `ts.createProgram` as `backtick-tsc` creates one, to check files in-process.
|
|
34
|
+
export const createProgram = proxyCreateProgram(ts, ts.createProgram, () => ({
|
|
35
|
+
languagePlugins: [getBacktickLanguagePlugin(ts, (fileName) => fileName)],
|
|
36
|
+
}));
|
|
37
|
+
|
|
34
38
|
// What this tool is for beyond checking: a package that exports a client script
|
|
35
39
|
// gets a declaration carrying the script's real type. Plain `tsc` types
|
|
36
40
|
// `cs`...`` by the tag's own signature, `Client<unknown>`, because the
|
|
@@ -42,10 +46,7 @@ export function run(tscPath = require.resolve("typescript/lib/tsc.js")) {
|
|
|
42
46
|
// the emitter prints them, on identifiers alone, so nothing inside a string
|
|
43
47
|
// literal is touched. Not at the file system: `sys.writeFile` writes through
|
|
44
48
|
// `openSync`, and the CLI bundle's `sys` is not this module's anyway.
|
|
45
|
-
function unmangleDeclarations(
|
|
46
|
-
ts: typeof import("typescript"),
|
|
47
|
-
program: ts.Program,
|
|
48
|
-
): void {
|
|
49
|
+
function unmangleDeclarations(program: ts.Program): void {
|
|
49
50
|
const emit = program.emit.bind(program);
|
|
50
51
|
program.emit = (
|
|
51
52
|
targetSourceFile,
|
|
@@ -101,10 +102,10 @@ function refuseJavaScriptEmit(options: {
|
|
|
101
102
|
|
|
102
103
|
// Surfaces Backtick's own compiler diagnostics through `tsc` by wrapping the
|
|
103
104
|
// program's `getSemanticDiagnostics`. This has to patch Volar's `decorateProgram`
|
|
104
|
-
// module in place:
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
function patchDecorateProgram(
|
|
105
|
+
// module in place: Volar's program proxy calls the module's export by property
|
|
106
|
+
// access at program-creation time, so reassigning it here is picked up. Once,
|
|
107
|
+
// when this module loads, for `run` and `createProgram` alike.
|
|
108
|
+
function patchDecorateProgram() {
|
|
108
109
|
const decorateProgramModule =
|
|
109
110
|
require("@volar/typescript/lib/node/decorateProgram.js") as typeof import("@volar/typescript/lib/node/decorateProgram.js");
|
|
110
111
|
const volarDecorateProgram = decorateProgramModule.decorateProgram;
|
|
@@ -112,7 +113,7 @@ function patchDecorateProgram(ts: typeof import("typescript")) {
|
|
|
112
113
|
decorateProgramModule.decorateProgram = (language, program) => {
|
|
113
114
|
volarDecorateProgram(language, program);
|
|
114
115
|
refuseJavaScriptEmit(program.getCompilerOptions());
|
|
115
|
-
unmangleDeclarations(
|
|
116
|
+
unmangleDeclarations(program);
|
|
116
117
|
|
|
117
118
|
const getSemanticDiagnostics = program.getSemanticDiagnostics.bind(program);
|
|
118
119
|
program.getSemanticDiagnostics = (sourceFile, cancellationToken) => {
|