@gi-tcg/gts-transpiler 0.3.0 → 0.3.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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-transpiler",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "repository": "https://github.com/piovium/gts.git",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",
7
7
  "exports": {
8
8
  ".": {
9
9
  "types": "./dist/index.d.ts",
10
- "bun": "./src/index.ts",
10
+ "development": "./src/index.ts",
11
11
  "default": "./dist/index.js"
12
12
  }
13
13
  },
@@ -15,18 +15,21 @@
15
15
  "src",
16
16
  "dist"
17
17
  ],
18
- "scripts": {},
19
18
  "dependencies": {
20
19
  "@jridgewell/sourcemap-codec": "^1.5.5",
21
20
  "@sveltejs/acorn-typescript": "^1.0.8",
22
- "acorn": "8.15.0",
21
+ "acorn": "^8.15.0",
23
22
  "esrap": "2.2.1",
24
23
  "magic-string": "^0.30.21",
24
+ "path-browserify-esm": "^1.0.6",
25
25
  "zimmerframe": "^1.1.4"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@types/node": "^24.3.0",
29
29
  "@types/estree": "^1.0.8",
30
30
  "@volar/language-core": "^2.4.27"
31
+ },
32
+ "scripts": {
33
+ "build": "tsdown --platform neutral"
31
34
  }
32
- }
35
+ }
package/src/config.ts CHANGED
@@ -1,89 +1,5 @@
1
- import type { TranspileOption } from "./transform/gts";
2
-
3
- export interface PathPolyfill {
4
- isAbsolute(filePath: string): boolean;
5
- dirname(filePath: string): string;
6
- resolve(...segments: string[]): string;
7
- }
8
-
9
- export const path: PathPolyfill = globalThis.require
10
- ? (globalThis.require("node:path") as typeof import("node:path"))
11
- : {
12
- isAbsolute(filePath) {
13
- return filePath.startsWith("/");
14
- },
15
- dirname(filePath) {
16
- if (!filePath) {
17
- return ".";
18
- }
19
- const normalized = normalizeSlashes(filePath);
20
- if (normalized === "/") {
21
- return "/";
22
- }
23
- const trimmed =
24
- normalized.length > 1 && normalized.endsWith("/")
25
- ? normalized.slice(0, -1)
26
- : normalized;
27
- const idx = trimmed.lastIndexOf("/");
28
- if (idx < 0) {
29
- return ".";
30
- }
31
- if (idx === 0) {
32
- return "/";
33
- }
34
- return trimmed.slice(0, idx);
35
- },
36
- resolve(...segments) {
37
- if (segments.length === 0) {
38
- return ".";
39
- }
40
- let resolved = "";
41
- for (let i = segments.length - 1; i >= 0; i--) {
42
- const segment = segments[i];
43
- if (!segment) {
44
- continue;
45
- }
46
- if (resolved) {
47
- resolved = `${segment}/${resolved}`;
48
- } else {
49
- resolved = segment;
50
- }
51
- if (path.isAbsolute(segment)) {
52
- break;
53
- }
54
- }
55
- return normalizeResolvedPath(resolved || ".");
56
- },
57
- };
58
-
59
- function normalizeSlashes(filePath: string): string {
60
- return filePath.replace(/\\+/g, "/");
61
- }
62
-
63
- function normalizeResolvedPath(filePath: string): string {
64
- const normalized = normalizeSlashes(filePath);
65
- const isAbsolute = normalized.startsWith("/");
66
- const parts = normalized.split("/");
67
- const stack: string[] = [];
68
- for (const part of parts) {
69
- if (!part || part === ".") {
70
- continue;
71
- }
72
- if (part === "..") {
73
- if (stack.length > 0 && stack[stack.length - 1] !== "..") {
74
- stack.pop();
75
- } else if (!isAbsolute) {
76
- stack.push("..");
77
- }
78
- continue;
79
- }
80
- stack.push(part);
81
- }
82
- if (isAbsolute) {
83
- return `/${stack.join("/")}` || "/";
84
- }
85
- return stack.join("/") || ".";
86
- }
1
+ import type { TranspileOption } from "./transform/gts.ts";
2
+ import path from "path-browserify-esm";
87
3
 
88
4
  export interface GtsConfig extends TranspileOption {}
89
5
 
package/src/error.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import type { SourceLocation } from "estree";
2
2
 
3
3
  export class GtsTranspilerError extends Error {
4
- constructor(
5
- message: string,
6
- public readonly position: SourceLocation | null,
7
- ) {
4
+ public readonly position: SourceLocation | null;
5
+ constructor(message: string, position: SourceLocation | null) {
8
6
  super(message);
9
7
  this.name = "GtsTranspilerError";
8
+ this.position = position;
10
9
  }
11
10
  }
package/src/index.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { parse, parseLoose } from "./parse";
1
+ import { parse, parseLoose } from "./parse/index.ts";
2
2
  import {
3
3
  transform,
4
4
  transformForVolar,
5
5
  type TranspileResult,
6
- } from "./transform";
7
- import type { TranspileOption } from "./transform/gts";
8
- import type { VolarMappingResult } from "./transform/volar";
9
- export { GtsTranspilerError } from "./error";
6
+ } from "./transform/index.ts";
7
+ import type { TranspileOption } from "./transform/gts.ts";
8
+ import type { VolarMappingResult } from "./transform/volar/index.ts";
9
+ export { GtsTranspilerError } from "./error.ts";
10
10
 
11
11
  export function transpile(
12
12
  source: string,
@@ -39,6 +39,4 @@ export {
39
39
  resolveGtsConfig,
40
40
  resolveGtsConfigSync,
41
41
  type GtsConfig,
42
- path,
43
- type PathPolyfill,
44
- } from "./config";
42
+ } from "./config.ts";
@@ -1,7 +1,7 @@
1
1
  import { tokTypes, type Parser as ParserClass } from "acorn";
2
- import type { Parse, AST } from "../types";
3
- import { specialIdentifiers } from "../keywords";
4
- import { DUMMY_PLACEHOLDER } from "./loose_plugin";
2
+ import type { Parse, AST } from "../types.ts";
3
+ import { specialIdentifiers } from "../keywords.ts";
4
+ import { DUMMY_PLACEHOLDER } from "./loose_plugin.ts";
5
5
 
6
6
  /*
7
7
 
@@ -316,7 +316,7 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
316
316
  incDec?: boolean,
317
317
  forInit?: boolean | "await",
318
318
  ): AST.Expression {
319
- if (this.isContextual("query")) {
319
+ if (this.isShortcutContext && this.isContextual("query")) {
320
320
  const expr = this.gts_parseQueryExpression();
321
321
  if (!incDec && this.eat(tokTypes.starstar)) {
322
322
  this.unexpected(this.lastTokStart);
@@ -1,11 +1,11 @@
1
1
  import { Parser } from "acorn";
2
2
  import type { Position, Program } from "estree";
3
3
  import { tsPlugin } from "@sveltejs/acorn-typescript";
4
- import { gtsPlugin, type GtsPluginOption } from "./gts_plugin.js";
5
- import { loosePlugin } from "./loose_plugin.js";
4
+ import { gtsPlugin, type GtsPluginOption } from "./gts_plugin.ts";
5
+ import { loosePlugin } from "./loose_plugin.ts";
6
6
  import { getCommentHandlers } from "./comment.js";
7
- import { GtsTranspilerError } from "../error.js";
8
- import { recordCallLParenPlugin } from "./record_call_lparen_plugin.js";
7
+ import { GtsTranspilerError } from "../error.ts";
8
+ import { recordCallLParenPlugin } from "./record_call_lparen_plugin.ts";
9
9
 
10
10
  const TsParser = Parser.extend(tsPlugin());
11
11
 
@@ -1,5 +1,5 @@
1
1
  import { tokTypes, type Parser } from "acorn";
2
- import type { AST, Parse } from "../types.js";
2
+ import type { AST, Parse } from "../types.ts";
3
3
 
4
4
  /**
5
5
  * A plugin that records the location of the left parenthesis in CallExpression and
@@ -7,7 +7,7 @@ import type {
7
7
  Node,
8
8
  EmptyStatement,
9
9
  } from "estree";
10
- import { GtsTranspilerError } from "../error";
10
+ import { GtsTranspilerError } from "../error.ts";
11
11
 
12
12
  const empty: EmptyStatement = {
13
13
  type: "EmptyStatement",
@@ -201,4 +201,4 @@ const eraseTsVisitor: Visitors<any, null> = {
201
201
 
202
202
  export const eraseTs = <T>(ast: T): T => {
203
203
  return walk(ast, null, eraseTsVisitor);
204
- }
204
+ }
@@ -16,11 +16,11 @@ import type {
16
16
  Statement,
17
17
  } from "estree";
18
18
  import { walk, type Visitors } from "zimmerframe";
19
- import { GtsTranspilerError } from "../error";
19
+ import { GtsTranspilerError } from "../error.ts";
20
20
  import {
21
21
  DEFAULT_QUERY_BINDINGS,
22
22
  DEFAULT_SHORTCUT_FUNCTION_PRELUDES,
23
- } from "./constants";
23
+ } from "./constants.ts";
24
24
 
25
25
  export interface ExternalizedBinding {
26
26
  bindingName: Identifier;
@@ -162,6 +162,15 @@ export const commonGtsVisitor: Visitors<Node, TranspileState> = {
162
162
  value: !!node.star,
163
163
  },
164
164
  },
165
+ {
166
+ type: "Property",
167
+ key: { type: "Identifier", name: "context" },
168
+ computed: false,
169
+ kind: "init",
170
+ method: false,
171
+ shorthand: false,
172
+ value: state.fnArgId,
173
+ }
165
174
  ],
166
175
  },
167
176
  ],
@@ -1,9 +1,9 @@
1
1
  import type { Program } from "estree";
2
- import { eraseTs } from "./erase_ts";
2
+ import { eraseTs } from "./erase_ts.ts";
3
3
  import { print } from "esrap";
4
4
  import jsPrinter from "esrap/languages/ts";
5
5
  import type { SourceMap } from "magic-string";
6
- import { gtsToTs, type TranspileOption } from "./gts";
6
+ import { gtsToTs, type TranspileOption } from "./gts.ts";
7
7
 
8
8
  export interface TranspileResult {
9
9
  code: string;
@@ -33,4 +33,4 @@ export function transform(
33
33
  };
34
34
  }
35
35
 
36
- export { transformForVolar } from "./volar";
36
+ export { transformForVolar } from "./volar/index.ts";
@@ -1,18 +1,18 @@
1
- import type { AST } from "../../types";
1
+ import type { AST } from "../../types.ts";
2
2
  import { walk } from "zimmerframe";
3
- import type { SourceInfo, TranspileResult } from "..";
3
+ import type { SourceInfo, TranspileResult } from "../index.ts";
4
4
  import { print } from "esrap";
5
5
  import {
6
6
  initialTranspileState,
7
7
  type TranspileOption,
8
8
  type TranspileState,
9
- } from "../gts";
10
- import { gtsToTypingsWalker, type TypingTranspileState } from "./walker";
11
- import { applyReplacements } from "./replacements";
9
+ } from "../gts.ts";
10
+ import { gtsToTypingsWalker, type TypingTranspileState } from "./walker.ts";
11
+ import { applyReplacements } from "./replacements.ts";
12
12
  import type { Program } from "estree";
13
- import { convertToVolarMappings, type VolarMappingResult } from "./mappings";
14
- import { collectLeafTokens, type LeafToken } from "./collect_tokens";
15
- import { patchedPrinter } from "./printer";
13
+ import { convertToVolarMappings, type VolarMappingResult } from "./mappings.ts";
14
+ import { collectLeafTokens, type LeafToken } from "./collect_tokens.ts";
15
+ import { patchedPrinter } from "./printer.ts";
16
16
 
17
17
  interface TypingTranspileOption extends TranspileOption {
18
18
  leafTokens: LeafToken[];
@@ -81,4 +81,4 @@ export function transformForVolar(
81
81
  };
82
82
  }
83
83
 
84
- export type { VolarMappingResult } from "./mappings";
84
+ export type { VolarMappingResult } from "./mappings.ts";
@@ -1,6 +1,6 @@
1
1
  import { decode } from "@jridgewell/sourcemap-codec";
2
2
  import type { CodeInformation, CodeMapping } from "@volar/language-core";
3
- import type { LeafToken } from "./collect_tokens";
3
+ import type { LeafToken } from "./collect_tokens.ts";
4
4
 
5
5
  export interface VolarMappingResult {
6
6
  code: string;
@@ -1,5 +1,5 @@
1
1
  import tsPrinter from "esrap/languages/ts";
2
- import type { AST } from "../../types";
2
+ import type { AST } from "../../types.ts";
3
3
  import type { Context, Visitors } from "esrap";
4
4
  import type { Node, SimpleCallExpression } from "estree";
5
5
 
@@ -1,5 +1,5 @@
1
1
  import type { ExpressionStatement } from "estree";
2
- import type { TypingTranspileState } from "./walker";
2
+ import type { TypingTranspileState } from "./walker.ts";
3
3
 
4
4
  type ReplacementPayload =
5
5
  | {
@@ -24,9 +24,9 @@ import {
24
24
  commonGtsVisitor,
25
25
  type ExternalizedBinding,
26
26
  type TranspileState,
27
- } from "../gts";
28
- import type { LeafToken } from "./collect_tokens";
29
- import { createReplacementHolder } from "./replacements";
27
+ } from "../gts.ts";
28
+ import type { LeafToken } from "./collect_tokens.ts";
29
+ import { createReplacementHolder } from "./replacements.ts";
30
30
 
31
31
  interface ExternalizedTypedBinding extends ExternalizedBinding {
32
32
  typingId: Identifier;
package/src/types.ts CHANGED
@@ -107,7 +107,7 @@ declare module "acorn" {
107
107
  }
108
108
  }
109
109
 
110
- export namespace Parse {
110
+ export declare namespace Parse {
111
111
  /**
112
112
  * Destructuring errors object used during expression parsing
113
113
  * See: https://github.com/acornjs/acorn/blob/main/acorn/src/parseutil.js
@@ -365,7 +365,7 @@ export namespace Parse {
365
365
  * They are accessed by Ripple's custom parser plugin for whitespace handling,
366
366
  * JSX parsing, and other advanced features.
367
367
  */
368
- export declare class Parser extends acorn.Parser {
368
+ export class Parser extends acorn.Parser {
369
369
  // ============================================================
370
370
  // Position and Location Tracking
371
371
  // ============================================================