@gi-tcg/gts-transpiler 0.4.0 → 0.4.2

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 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 ResolveGtsConfigSyncOptions {
619
- readFileFn: ReadFileFn;
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 ResolveGtsConfigAsyncOptions {
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 path from "path-browserify-esm";
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
  }
@@ -1981,9 +1981,10 @@ const DEFAULT_GTS_CONFIG = {
1981
1981
  queryBindings: ["my", "opp"]
1982
1982
  };
1983
1983
  function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
1984
- const startDir = normalizeStartDir(filePath, options.cwd);
1985
- const stopDir = options.stopDir ? path.resolve(options.stopDir) : void 0;
1986
- const pkgConfig = yield* findNearestPackageConfig(options.readFileFn, startDir, stopDir);
1984
+ const pathModule = options.pathModule || browserPath;
1985
+ const startDir = normalizeStartDir(filePath, pathModule, options.cwd);
1986
+ const stopDir = options.stopDir ? pathModule.resolve(options.stopDir) : void 0;
1987
+ const pkgConfig = yield* findNearestPackageConfig(options.readFileFn, pathModule, startDir, stopDir);
1987
1988
  return {
1988
1989
  ...DEFAULT_GTS_CONFIG,
1989
1990
  ...pkgConfig,
@@ -1991,6 +1992,7 @@ function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
1991
1992
  };
1992
1993
  }
1993
1994
  async function resolveGtsConfig(filePath, inlineConfig, options) {
1995
+ options.pathModule ??= await import("node:path").then((mod) => mod.default);
1994
1996
  const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
1995
1997
  let result = generator.next();
1996
1998
  while (!result.done) {
@@ -2011,16 +2013,16 @@ function resolveGtsConfigSync(filePath, inlineConfig, options) {
2011
2013
  }
2012
2014
  return result.value;
2013
2015
  }
2014
- function normalizeStartDir(sourceFile, cwd) {
2015
- const absolute = path.isAbsolute(sourceFile) ? sourceFile : path.resolve(cwd || ".", sourceFile);
2016
- return path.dirname(absolute);
2016
+ function normalizeStartDir(sourceFile, pathModule, cwd) {
2017
+ const absolute = pathModule.isAbsolute(sourceFile) ? sourceFile : pathModule.resolve(cwd || ".", sourceFile);
2018
+ return pathModule.dirname(absolute);
2017
2019
  }
2018
- function* findNearestPackageConfig(readFileFn, startDir, stopDir) {
2020
+ function* findNearestPackageConfig(readFileFn, pathModule, startDir, stopDir) {
2019
2021
  let currentDir = startDir;
2020
2022
  while (true) {
2021
- const config = yield* readPackageConfig(readFileFn, path.resolve(currentDir, "package.json"));
2023
+ const config = yield* readPackageConfig(readFileFn, pathModule.resolve(currentDir, "package.json"));
2022
2024
  if (config) return config;
2023
- const parentDir = path.dirname(currentDir);
2025
+ const parentDir = pathModule.dirname(currentDir);
2024
2026
  if (parentDir === currentDir) break;
2025
2027
  if (stopDir && currentDir === stopDir) break;
2026
2028
  currentDir = parentDir;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-transpiler",
3
- "version": "0.4.0",
3
+ "version": "0.4.2",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
package/src/config.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { TranspileOption } from "./transform/gts.ts";
2
- import path from "path-browserify-esm";
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 ResolveGtsConfigSyncOptions {
14
- readFileFn: ReadFileFn;
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 ResolveGtsConfigAsyncOptions {
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> = {
@@ -43,10 +51,14 @@ function* resolveGtsConfigImpl(
43
51
  inlineConfig: GtsConfig = {},
44
52
  options: ResolveGtsConfigAsyncOptions | ResolveGtsConfigSyncOptions,
45
53
  ): Generator<string | Promise<string>, Required<GtsConfig>, string> {
46
- const startDir = normalizeStartDir(filePath, options.cwd);
47
- const stopDir = options.stopDir ? path.resolve(options.stopDir) : void 0;
54
+ const pathModule = options.pathModule || browserPath;
55
+ const startDir = normalizeStartDir(filePath, pathModule, options.cwd);
56
+ const stopDir = options.stopDir
57
+ ? pathModule.resolve(options.stopDir)
58
+ : void 0;
48
59
  const pkgConfig = yield* findNearestPackageConfig(
49
60
  options.readFileFn,
61
+ pathModule,
50
62
  startDir,
51
63
  stopDir,
52
64
  );
@@ -62,6 +74,7 @@ export async function resolveGtsConfig(
62
74
  inlineConfig: GtsConfig,
63
75
  options: ResolveGtsConfigAsyncOptions,
64
76
  ): Promise<Required<GtsConfig>> {
77
+ options.pathModule ??= await import("node:path").then((mod) => mod.default);
65
78
  const generator = resolveGtsConfigImpl(filePath, inlineConfig, options);
66
79
  let result = generator.next();
67
80
  while (!result.done) {
@@ -94,26 +107,31 @@ export function resolveGtsConfigSync(
94
107
  return result.value;
95
108
  }
96
109
 
97
- function normalizeStartDir(sourceFile: string, cwd?: string): string {
98
- const absolute = path.isAbsolute(sourceFile)
110
+ function normalizeStartDir(
111
+ sourceFile: string,
112
+ pathModule: PathModule,
113
+ cwd?: string,
114
+ ): string {
115
+ const absolute = pathModule.isAbsolute(sourceFile)
99
116
  ? sourceFile
100
- : path.resolve(cwd || ".", sourceFile);
101
- return path.dirname(absolute);
117
+ : pathModule.resolve(cwd || ".", sourceFile);
118
+ return pathModule.dirname(absolute);
102
119
  }
103
120
 
104
121
  function* findNearestPackageConfig(
105
122
  readFileFn: ReadFileFn | ReadFileAsyncFn,
123
+ pathModule: PathModule,
106
124
  startDir: string,
107
125
  stopDir?: string,
108
126
  ): Generator<string | Promise<string>, GtsConfig, string> {
109
127
  let currentDir = startDir;
110
128
  while (true) {
111
- const pkgPath = path.resolve(currentDir, "package.json");
129
+ const pkgPath = pathModule.resolve(currentDir, "package.json");
112
130
  const config = yield* readPackageConfig(readFileFn, pkgPath);
113
131
  if (config) {
114
132
  return config;
115
133
  }
116
- const parentDir = path.dirname(currentDir);
134
+ const parentDir = pathModule.dirname(currentDir);
117
135
  if (parentDir === currentDir) {
118
136
  break;
119
137
  }
package/src/index.ts CHANGED
@@ -51,4 +51,7 @@ export {
51
51
  resolveGtsConfig,
52
52
  resolveGtsConfigSync,
53
53
  type GtsConfig,
54
+ type ResolveGtsConfigAsyncOptions,
55
+ type ResolveGtsConfigSyncOptions,
56
+ type PathModule,
54
57
  } from "./config.ts";
@@ -197,7 +197,8 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
197
197
  while (this.type !== tokTypes.braceR && this.type !== tokTypes.eof) {
198
198
  // Check for DirectShortcutFunction
199
199
  if (
200
- (specialIdentifiers as unknown[]).includes(this.value) ||
200
+ (this.type !== tokTypes.string &&
201
+ (specialIdentifiers as unknown[]).includes(this.value)) ||
201
202
  this.type === tokTypes.colon
202
203
  ) {
203
204
  node.directAction = this.gts_parseDirectFunction();
@@ -251,7 +252,10 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
251
252
  this.isContextual("as"))
252
253
  ) {
253
254
  // Allow omitting the attribute expression for language tooling
254
- const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc) as AST.Identifier;
255
+ const dummy = this.startNodeAt(
256
+ this.lastTokEnd,
257
+ this.lastTokEndLoc,
258
+ ) as AST.Identifier;
255
259
  dummy.name = DUMMY_PLACEHOLDER;
256
260
  dummy.isDummy = true;
257
261
  return this.finishNode(dummy, "Identifier");
@@ -297,7 +301,10 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
297
301
  this.type !== tokTypes.name
298
302
  ) {
299
303
  // Allow omitting the identifier after ':' for language tooling
300
- const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc) as AST.Identifier;
304
+ const dummy = this.startNodeAt(
305
+ this.lastTokEnd,
306
+ this.lastTokEndLoc,
307
+ ) as AST.Identifier;
301
308
  dummy.name = DUMMY_PLACEHOLDER;
302
309
  dummy.isDummy = true;
303
310
  node.property = this.finishNode(dummy, "Identifier");