@gi-tcg/gts-transpiler 0.4.1 → 0.4.3
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/dist/index.d.ts +12 -6
- package/dist/index.js +17 -14
- package/package.json +1 -1
- package/src/config.ts +33 -14
- package/src/index.ts +3 -0
- package/src/parse/gts_plugin.ts +14 -6
- package/src/transform/volar/walker.ts +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -615,15 +615,21 @@ declare class GtsTranspilerError extends Error {
|
|
|
615
615
|
interface GtsConfig extends TranspileOption {}
|
|
616
616
|
type ReadFileFn = (path: string, encoding: "utf8") => string;
|
|
617
617
|
type ReadFileAsyncFn = (path: string, encoding: "utf8") => Promise<string>;
|
|
618
|
-
interface
|
|
619
|
-
|
|
618
|
+
interface PathModule {
|
|
619
|
+
resolve(...paths: string[]): string;
|
|
620
|
+
dirname(path: string): string;
|
|
621
|
+
isAbsolute(path: string): boolean;
|
|
622
|
+
}
|
|
623
|
+
interface ResolveGtsConfigBaseOptions {
|
|
620
624
|
cwd?: string;
|
|
625
|
+
pathModule?: PathModule;
|
|
621
626
|
stopDir?: string;
|
|
622
627
|
}
|
|
623
|
-
interface
|
|
628
|
+
interface ResolveGtsConfigSyncOptions extends ResolveGtsConfigBaseOptions {
|
|
629
|
+
readFileFn: ReadFileFn;
|
|
630
|
+
}
|
|
631
|
+
interface ResolveGtsConfigAsyncOptions extends ResolveGtsConfigBaseOptions {
|
|
624
632
|
readFileFn: ReadFileAsyncFn;
|
|
625
|
-
cwd?: string;
|
|
626
|
-
stopDir?: string;
|
|
627
633
|
}
|
|
628
634
|
declare function resolveGtsConfig(filePath: string, inlineConfig: GtsConfig, options: ResolveGtsConfigAsyncOptions): Promise<Required<GtsConfig>>;
|
|
629
635
|
declare function resolveGtsConfigSync(filePath: string, inlineConfig: GtsConfig, options: ResolveGtsConfigSyncOptions): Required<GtsConfig>;
|
|
@@ -632,4 +638,4 @@ declare function resolveGtsConfigSync(filePath: string, inlineConfig: GtsConfig,
|
|
|
632
638
|
declare function transpile(source: string, filename: string, option: TranspileOption): TranspileResult;
|
|
633
639
|
declare function transpileForVolar(source: string, filename: string, option: TranspileOption): VolarMappingResult;
|
|
634
640
|
//#endregion
|
|
635
|
-
export { type GtsConfig, type ParseLooseOptions as GtsParseLooseOptions, type ParseOptions as GtsParseOptions, GtsTranspilerError, type TranspileOption, type TranspileResult, type VolarMappingResult, parse, parseLoose, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|
|
641
|
+
export { type GtsConfig, type ParseLooseOptions as GtsParseLooseOptions, type ParseOptions as GtsParseOptions, GtsTranspilerError, type PathModule, type ResolveGtsConfigAsyncOptions, type ResolveGtsConfigSyncOptions, type TranspileOption, type TranspileResult, type VolarMappingResult, parse, parseLoose, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,7 @@ import { print } from "esrap";
|
|
|
5
5
|
import jsPrinter from "esrap/languages/ts";
|
|
6
6
|
import { defaultPrinters, print as print$1 } from "espolar";
|
|
7
7
|
import dedent from "dedent";
|
|
8
|
-
import
|
|
8
|
+
import browserPath from "path-browserify-esm";
|
|
9
9
|
//#region src/keywords.ts
|
|
10
10
|
const specialIdentifiers = [
|
|
11
11
|
"break",
|
|
@@ -166,7 +166,7 @@ function gtsPlugin(options = {}) {
|
|
|
166
166
|
node.attributes = [];
|
|
167
167
|
this.expect(tokTypes.braceL);
|
|
168
168
|
while (this.type !== tokTypes.braceR && this.type !== tokTypes.eof) {
|
|
169
|
-
if (specialIdentifiers.includes(this.value) || this.type === tokTypes.colon) {
|
|
169
|
+
if (this.type !== tokTypes.string && specialIdentifiers.includes(this.value) || this.type === tokTypes.colon) {
|
|
170
170
|
node.directAction = this.gts_parseDirectFunction();
|
|
171
171
|
break;
|
|
172
172
|
}
|
|
@@ -203,7 +203,7 @@ function gtsPlugin(options = {}) {
|
|
|
203
203
|
dummy.isDummy = true;
|
|
204
204
|
return this.finishNode(dummy, "Identifier");
|
|
205
205
|
}
|
|
206
|
-
return this.
|
|
206
|
+
return this.parseExprSubscripts(void 0, false);
|
|
207
207
|
}
|
|
208
208
|
gts_parseShortcutFunction() {
|
|
209
209
|
const node = this.startNode();
|
|
@@ -1563,7 +1563,7 @@ const gtsToTypingsWalker = {
|
|
|
1563
1563
|
};
|
|
1564
1564
|
state.literalFromIdentifier.add(lit);
|
|
1565
1565
|
return lit;
|
|
1566
|
-
} else return
|
|
1566
|
+
} else return visit(attr);
|
|
1567
1567
|
});
|
|
1568
1568
|
const returnValue = {
|
|
1569
1569
|
type: "Identifier",
|
|
@@ -1976,14 +1976,16 @@ const DEFAULT_GTS_CONFIG = {
|
|
|
1976
1976
|
"anemo",
|
|
1977
1977
|
"geo",
|
|
1978
1978
|
"dendro",
|
|
1979
|
-
"omni"
|
|
1979
|
+
"omni",
|
|
1980
|
+
"$"
|
|
1980
1981
|
],
|
|
1981
1982
|
queryBindings: ["my", "opp"]
|
|
1982
1983
|
};
|
|
1983
1984
|
function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
|
|
1984
|
-
const
|
|
1985
|
-
const
|
|
1986
|
-
const
|
|
1985
|
+
const pathModule = options.pathModule || browserPath;
|
|
1986
|
+
const startDir = normalizeStartDir(filePath, pathModule, options.cwd);
|
|
1987
|
+
const stopDir = options.stopDir ? pathModule.resolve(options.stopDir) : void 0;
|
|
1988
|
+
const pkgConfig = yield* findNearestPackageConfig(options.readFileFn, pathModule, startDir, stopDir);
|
|
1987
1989
|
return {
|
|
1988
1990
|
...DEFAULT_GTS_CONFIG,
|
|
1989
1991
|
...pkgConfig,
|
|
@@ -1991,6 +1993,7 @@ function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
|
|
|
1991
1993
|
};
|
|
1992
1994
|
}
|
|
1993
1995
|
async function resolveGtsConfig(filePath, inlineConfig, options) {
|
|
1996
|
+
options.pathModule ??= await import("node:path").then((mod) => mod.default);
|
|
1994
1997
|
const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
|
|
1995
1998
|
let result = generator.next();
|
|
1996
1999
|
while (!result.done) {
|
|
@@ -2011,16 +2014,16 @@ function resolveGtsConfigSync(filePath, inlineConfig, options) {
|
|
|
2011
2014
|
}
|
|
2012
2015
|
return result.value;
|
|
2013
2016
|
}
|
|
2014
|
-
function normalizeStartDir(sourceFile, cwd) {
|
|
2015
|
-
const absolute =
|
|
2016
|
-
return
|
|
2017
|
+
function normalizeStartDir(sourceFile, pathModule, cwd) {
|
|
2018
|
+
const absolute = pathModule.isAbsolute(sourceFile) ? sourceFile : pathModule.resolve(cwd || ".", sourceFile);
|
|
2019
|
+
return pathModule.dirname(absolute);
|
|
2017
2020
|
}
|
|
2018
|
-
function* findNearestPackageConfig(readFileFn, startDir, stopDir) {
|
|
2021
|
+
function* findNearestPackageConfig(readFileFn, pathModule, startDir, stopDir) {
|
|
2019
2022
|
let currentDir = startDir;
|
|
2020
2023
|
while (true) {
|
|
2021
|
-
const config = yield* readPackageConfig(readFileFn,
|
|
2024
|
+
const config = yield* readPackageConfig(readFileFn, pathModule.resolve(currentDir, "package.json"));
|
|
2022
2025
|
if (config) return config;
|
|
2023
|
-
const parentDir =
|
|
2026
|
+
const parentDir = pathModule.dirname(currentDir);
|
|
2024
2027
|
if (parentDir === currentDir) break;
|
|
2025
2028
|
if (stopDir && currentDir === stopDir) break;
|
|
2026
2029
|
currentDir = parentDir;
|
package/package.json
CHANGED
package/src/config.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TranspileOption } from "./transform/gts.ts";
|
|
2
|
-
import
|
|
2
|
+
import browserPath from "path-browserify-esm";
|
|
3
3
|
|
|
4
4
|
export interface GtsConfig extends TranspileOption {}
|
|
5
5
|
|
|
@@ -10,16 +10,24 @@ export interface PackageJson {
|
|
|
10
10
|
type ReadFileFn = (path: string, encoding: "utf8") => string;
|
|
11
11
|
type ReadFileAsyncFn = (path: string, encoding: "utf8") => Promise<string>;
|
|
12
12
|
|
|
13
|
-
export interface
|
|
14
|
-
|
|
13
|
+
export interface PathModule {
|
|
14
|
+
resolve(...paths: string[]): string;
|
|
15
|
+
dirname(path: string): string;
|
|
16
|
+
isAbsolute(path: string): boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface ResolveGtsConfigBaseOptions {
|
|
15
20
|
cwd?: string;
|
|
21
|
+
pathModule?: PathModule;
|
|
16
22
|
stopDir?: string;
|
|
17
23
|
}
|
|
18
24
|
|
|
19
|
-
export interface
|
|
25
|
+
export interface ResolveGtsConfigSyncOptions extends ResolveGtsConfigBaseOptions {
|
|
26
|
+
readFileFn: ReadFileFn;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface ResolveGtsConfigAsyncOptions extends ResolveGtsConfigBaseOptions {
|
|
20
30
|
readFileFn: ReadFileAsyncFn;
|
|
21
|
-
cwd?: string;
|
|
22
|
-
stopDir?: string;
|
|
23
31
|
}
|
|
24
32
|
|
|
25
33
|
const DEFAULT_GTS_CONFIG: Required<GtsConfig> = {
|
|
@@ -34,6 +42,7 @@ const DEFAULT_GTS_CONFIG: Required<GtsConfig> = {
|
|
|
34
42
|
"geo",
|
|
35
43
|
"dendro",
|
|
36
44
|
"omni",
|
|
45
|
+
"$",
|
|
37
46
|
],
|
|
38
47
|
queryBindings: ["my", "opp"],
|
|
39
48
|
};
|
|
@@ -43,10 +52,14 @@ function* resolveGtsConfigImpl(
|
|
|
43
52
|
inlineConfig: GtsConfig = {},
|
|
44
53
|
options: ResolveGtsConfigAsyncOptions | ResolveGtsConfigSyncOptions,
|
|
45
54
|
): Generator<string | Promise<string>, Required<GtsConfig>, string> {
|
|
46
|
-
const
|
|
47
|
-
const
|
|
55
|
+
const pathModule = options.pathModule || browserPath;
|
|
56
|
+
const startDir = normalizeStartDir(filePath, pathModule, options.cwd);
|
|
57
|
+
const stopDir = options.stopDir
|
|
58
|
+
? pathModule.resolve(options.stopDir)
|
|
59
|
+
: void 0;
|
|
48
60
|
const pkgConfig = yield* findNearestPackageConfig(
|
|
49
61
|
options.readFileFn,
|
|
62
|
+
pathModule,
|
|
50
63
|
startDir,
|
|
51
64
|
stopDir,
|
|
52
65
|
);
|
|
@@ -62,6 +75,7 @@ export async function resolveGtsConfig(
|
|
|
62
75
|
inlineConfig: GtsConfig,
|
|
63
76
|
options: ResolveGtsConfigAsyncOptions,
|
|
64
77
|
): Promise<Required<GtsConfig>> {
|
|
78
|
+
options.pathModule ??= await import("node:path").then((mod) => mod.default);
|
|
65
79
|
const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
|
|
66
80
|
let result = generator.next();
|
|
67
81
|
while (!result.done) {
|
|
@@ -94,26 +108,31 @@ export function resolveGtsConfigSync(
|
|
|
94
108
|
return result.value;
|
|
95
109
|
}
|
|
96
110
|
|
|
97
|
-
function normalizeStartDir(
|
|
98
|
-
|
|
111
|
+
function normalizeStartDir(
|
|
112
|
+
sourceFile: string,
|
|
113
|
+
pathModule: PathModule,
|
|
114
|
+
cwd?: string,
|
|
115
|
+
): string {
|
|
116
|
+
const absolute = pathModule.isAbsolute(sourceFile)
|
|
99
117
|
? sourceFile
|
|
100
|
-
:
|
|
101
|
-
return
|
|
118
|
+
: pathModule.resolve(cwd || ".", sourceFile);
|
|
119
|
+
return pathModule.dirname(absolute);
|
|
102
120
|
}
|
|
103
121
|
|
|
104
122
|
function* findNearestPackageConfig(
|
|
105
123
|
readFileFn: ReadFileFn | ReadFileAsyncFn,
|
|
124
|
+
pathModule: PathModule,
|
|
106
125
|
startDir: string,
|
|
107
126
|
stopDir?: string,
|
|
108
127
|
): Generator<string | Promise<string>, GtsConfig, string> {
|
|
109
128
|
let currentDir = startDir;
|
|
110
129
|
while (true) {
|
|
111
|
-
const pkgPath =
|
|
130
|
+
const pkgPath = pathModule.resolve(currentDir, "package.json");
|
|
112
131
|
const config = yield* readPackageConfig(readFileFn, pkgPath);
|
|
113
132
|
if (config) {
|
|
114
133
|
return config;
|
|
115
134
|
}
|
|
116
|
-
const parentDir =
|
|
135
|
+
const parentDir = pathModule.dirname(currentDir);
|
|
117
136
|
if (parentDir === currentDir) {
|
|
118
137
|
break;
|
|
119
138
|
}
|
package/src/index.ts
CHANGED
package/src/parse/gts_plugin.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { tokTypes, type Parser as ParserClass } from "acorn";
|
|
1
|
+
import { tokTypes, type Parser as ParserClass, type Node } from "acorn";
|
|
2
2
|
import type { Parse, AST } from "../types.ts";
|
|
3
3
|
import { specialIdentifiers } from "../keywords.ts";
|
|
4
4
|
import { DUMMY_PLACEHOLDER } from "./loose_plugin.ts";
|
|
@@ -46,7 +46,8 @@ AttributeExpression:
|
|
|
46
46
|
# foo bar { baz = 1 };
|
|
47
47
|
# foo bar, { baz: 1 }; // If allowed, hard to distinguish
|
|
48
48
|
# foo bar, ({ baz: 1 }); // OK
|
|
49
|
-
[lookahead != "{"]
|
|
49
|
+
[lookahead != "{"] CallExpression OptionalChain?
|
|
50
|
+
[lookahead != "{"] MemberExpression OptionalChain?
|
|
50
51
|
|
|
51
52
|
DirectShortcutFunction:
|
|
52
53
|
[lookahead = one of ":", ReservedWord]
|
|
@@ -197,7 +198,8 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
|
|
|
197
198
|
while (this.type !== tokTypes.braceR && this.type !== tokTypes.eof) {
|
|
198
199
|
// Check for DirectShortcutFunction
|
|
199
200
|
if (
|
|
200
|
-
(
|
|
201
|
+
(this.type !== tokTypes.string &&
|
|
202
|
+
(specialIdentifiers as unknown[]).includes(this.value)) ||
|
|
201
203
|
this.type === tokTypes.colon
|
|
202
204
|
) {
|
|
203
205
|
node.directAction = this.gts_parseDirectFunction();
|
|
@@ -251,12 +253,15 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
|
|
|
251
253
|
this.isContextual("as"))
|
|
252
254
|
) {
|
|
253
255
|
// Allow omitting the attribute expression for language tooling
|
|
254
|
-
const dummy = this.startNodeAt(
|
|
256
|
+
const dummy = this.startNodeAt(
|
|
257
|
+
this.lastTokEnd,
|
|
258
|
+
this.lastTokEndLoc,
|
|
259
|
+
) as AST.Identifier;
|
|
255
260
|
dummy.name = DUMMY_PLACEHOLDER;
|
|
256
261
|
dummy.isDummy = true;
|
|
257
262
|
return this.finishNode(dummy, "Identifier");
|
|
258
263
|
}
|
|
259
|
-
return this.
|
|
264
|
+
return this.parseExprSubscripts(void 0, false);
|
|
260
265
|
}
|
|
261
266
|
|
|
262
267
|
gts_parseShortcutFunction() {
|
|
@@ -297,7 +302,10 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
|
|
|
297
302
|
this.type !== tokTypes.name
|
|
298
303
|
) {
|
|
299
304
|
// Allow omitting the identifier after ':' for language tooling
|
|
300
|
-
const dummy = this.startNodeAt(
|
|
305
|
+
const dummy = this.startNodeAt(
|
|
306
|
+
this.lastTokEnd,
|
|
307
|
+
this.lastTokEndLoc,
|
|
308
|
+
) as AST.Identifier;
|
|
301
309
|
dummy.name = DUMMY_PLACEHOLDER;
|
|
302
310
|
dummy.isDummy = true;
|
|
303
311
|
node.property = this.finishNode(dummy, "Identifier");
|