@gi-tcg/gts-transpiler 0.4.2 → 0.4.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/dist/index.js CHANGED
@@ -203,7 +203,7 @@ function gtsPlugin(options = {}) {
203
203
  dummy.isDummy = true;
204
204
  return this.finishNode(dummy, "Identifier");
205
205
  }
206
- return this.parseExprAtom();
206
+ return this.parseExprSubscripts(void 0, false);
207
207
  }
208
208
  gts_parseShortcutFunction() {
209
209
  const node = this.startNode();
@@ -713,17 +713,12 @@ const eraseTs = (ast) => {
713
713
  };
714
714
  //#endregion
715
715
  //#region src/transform/constants.ts
716
- const DEFAULT_SHORTCUT_FUNCTION_PRELUDES = [
717
- "cryo",
718
- "hydro",
719
- "pyro",
720
- "electro",
721
- "anemo",
722
- "geo",
723
- "dendro",
724
- "omni"
716
+ const DEFAULT_SHORTCUT_FUNCTION_PRELUDES = ["$"];
717
+ const DEFAULT_QUERY_BINDINGS = [
718
+ "my",
719
+ "opp",
720
+ "macros"
725
721
  ];
726
- const DEFAULT_QUERY_BINDINGS = ["my", "opp"];
727
722
  //#endregion
728
723
  //#region src/transform/gts.ts
729
724
  const commonGtsVisitor = {
@@ -1245,10 +1240,11 @@ function applyReplacements(state, code, mappings) {
1245
1240
  const lhs = `${payload.finalMetaType}_lhs`;
1246
1241
  const requiredAttrsNs = `${payload.finalMetaType}_rans`;
1247
1242
  const collectedAttrsExpr = `${payload.collectedAttrs.join(" | ") || "never"}`;
1248
- const needleString = `"${requiredAttrsNs}_NeedleString" as any as "required attributes are missing"`;
1243
+ const length = payload.errorRange ? payload.errorRange[1] - payload.errorRange[0] : 0;
1244
+ const needleString = `"${requiredAttrsNs}_NeedleString${"0".repeat(length)}" as string as ${requiredAttrsNs}.DiagMsg`;
1249
1245
  if (payload.errorRange) state.extraMappings.push({
1250
1246
  sourceOffset: payload.errorRange[0],
1251
- length: payload.errorRange[1] - payload.errorRange[0],
1247
+ length,
1252
1248
  generatedNeedle: needleString
1253
1249
  });
1254
1250
  replacement = dedent`
@@ -1257,7 +1253,13 @@ function applyReplacements(state, code, mappings) {
1257
1253
  type ${lhs} = typeof ${lhs};
1258
1254
  namespace ${requiredAttrsNs} {
1259
1255
  export type Collected = ${collectedAttrsExpr};
1260
- export type Expected = { [K in keyof ${payload.defType}]: ${lhs}[K] extends { required(this: ${lhs}): true } ? K : never }[keyof ${payload.defType}];
1256
+ export type Expected = {
1257
+ [K in keyof ${payload.defType}]: ${lhs}[K] extends { required(this: ${lhs}): true } ? K : never;
1258
+ }[keyof ${payload.defType}];
1259
+ type DiagObj = {
1260
+ [K in Expected]: K extends Collected ? never : \`'\${K}' is a required attribute but not provided\`;
1261
+ }
1262
+ export type DiagMsg = DiagObj[Expected];
1261
1263
  };
1262
1264
  ((_: ${requiredAttrsNs}.Expected extends ${requiredAttrsNs}.Collected ? string : ${requiredAttrsNs}.Expected) => 0)(${needleString});
1263
1265
  `;
@@ -1563,7 +1565,7 @@ const gtsToTypingsWalker = {
1563
1565
  };
1564
1566
  state.literalFromIdentifier.add(lit);
1565
1567
  return lit;
1566
- } else return { ...visit(attr) };
1568
+ } else return visit(attr);
1567
1569
  });
1568
1570
  const returnValue = {
1569
1571
  type: "Identifier",
@@ -1601,11 +1603,11 @@ const gtsToTypingsWalker = {
1601
1603
  }
1602
1604
  }]
1603
1605
  });
1606
+ enterVMFromAttr(state, returnValue);
1604
1607
  if (body.namedAttributes) {
1605
- enterVMFromAttr(state, returnValue);
1606
1608
  visit(body.namedAttributes);
1607
1609
  exitVM(state, body.namedAttributes.range);
1608
- }
1610
+ } else exitVM(state, name.range);
1609
1611
  if (bindingName) {
1610
1612
  const export_ = node.bindingAccessModifier !== "private";
1611
1613
  const typingId = {
@@ -1968,17 +1970,12 @@ function transform(ast, option = {}, sourceInfo = {}) {
1968
1970
  const DEFAULT_GTS_CONFIG = {
1969
1971
  runtimeImportSource: "@gi-tcg/gts-runtime",
1970
1972
  providerImportSource: "@gi-tcg/core/gts",
1971
- shortcutFunctionPreludes: [
1972
- "cryo",
1973
- "hydro",
1974
- "pyro",
1975
- "electro",
1976
- "anemo",
1977
- "geo",
1978
- "dendro",
1979
- "omni"
1980
- ],
1981
- queryBindings: ["my", "opp"]
1973
+ shortcutFunctionPreludes: ["$"],
1974
+ queryBindings: [
1975
+ "my",
1976
+ "opp",
1977
+ "macros"
1978
+ ]
1982
1979
  };
1983
1980
  function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
1984
1981
  const pathModule = options.pathModule || browserPath;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gi-tcg/gts-transpiler",
3
- "version": "0.4.2",
3
+ "version": "0.4.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/piovium/gts.git"
package/src/config.ts CHANGED
@@ -33,17 +33,8 @@ export interface ResolveGtsConfigAsyncOptions extends ResolveGtsConfigBaseOption
33
33
  const DEFAULT_GTS_CONFIG: Required<GtsConfig> = {
34
34
  runtimeImportSource: "@gi-tcg/gts-runtime",
35
35
  providerImportSource: "@gi-tcg/core/gts",
36
- shortcutFunctionPreludes: [
37
- "cryo",
38
- "hydro",
39
- "pyro",
40
- "electro",
41
- "anemo",
42
- "geo",
43
- "dendro",
44
- "omni",
45
- ],
46
- queryBindings: ["my", "opp"],
36
+ shortcutFunctionPreludes: ["$"],
37
+ queryBindings: ["my", "opp", "macros"],
47
38
  };
48
39
 
49
40
  function* resolveGtsConfigImpl(
@@ -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 != "{"] PrimaryExpression
49
+ [lookahead != "{"] CallExpression OptionalChain?
50
+ [lookahead != "{"] MemberExpression OptionalChain?
50
51
 
51
52
  DirectShortcutFunction:
52
53
  [lookahead = one of ":", ReservedWord]
@@ -260,7 +261,7 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
260
261
  dummy.isDummy = true;
261
262
  return this.finishNode(dummy, "Identifier");
262
263
  }
263
- return this.parseExprAtom();
264
+ return this.parseExprSubscripts(void 0, false);
264
265
  }
265
266
 
266
267
  gts_parseShortcutFunction() {
@@ -1,11 +1,2 @@
1
- export const DEFAULT_SHORTCUT_FUNCTION_PRELUDES: string[] = [
2
- "cryo",
3
- "hydro",
4
- "pyro",
5
- "electro",
6
- "anemo",
7
- "geo",
8
- "dendro",
9
- "omni",
10
- ];
11
- export const DEFAULT_QUERY_BINDINGS: string[] = ["my", "opp"];
1
+ export const DEFAULT_SHORTCUT_FUNCTION_PRELUDES: string[] = ["$"];
2
+ export const DEFAULT_QUERY_BINDINGS: string[] = ["my", "opp", "macros"];
@@ -121,11 +121,15 @@ export function applyReplacements(
121
121
  const lhs = `${payload.finalMetaType}_lhs`;
122
122
  const requiredAttrsNs = `${payload.finalMetaType}_rans`;
123
123
  const collectedAttrsExpr = `${payload.collectedAttrs.join(" | ") || "never"}`;
124
- const needleString = `"${requiredAttrsNs}_NeedleString" as any as "required attributes are missing"`;
124
+ const length = payload.errorRange
125
+ ? payload.errorRange[1] - payload.errorRange[0]
126
+ : 0;
127
+ // Ensure that generated needle string is longer than error range so that error squiggle can cover all
128
+ const needleString = `"${requiredAttrsNs}_NeedleString${"0".repeat(length)}" as string as ${requiredAttrsNs}.DiagMsg`;
125
129
  if (payload.errorRange) {
126
130
  state.extraMappings.push({
127
131
  sourceOffset: payload.errorRange[0],
128
- length: payload.errorRange[1] - payload.errorRange[0],
132
+ length,
129
133
  generatedNeedle: needleString,
130
134
  });
131
135
  }
@@ -135,7 +139,13 @@ export function applyReplacements(
135
139
  type ${lhs} = typeof ${lhs};
136
140
  namespace ${requiredAttrsNs} {
137
141
  export type Collected = ${collectedAttrsExpr};
138
- export type Expected = { [K in keyof ${payload.defType}]: ${lhs}[K] extends { required(this: ${lhs}): true } ? K : never }[keyof ${payload.defType}];
142
+ export type Expected = {
143
+ [K in keyof ${payload.defType}]: ${lhs}[K] extends { required(this: ${lhs}): true } ? K : never;
144
+ }[keyof ${payload.defType}];
145
+ type DiagObj = {
146
+ [K in Expected]: K extends Collected ? never : \`'\${K}' is a required attribute but not provided\`;
147
+ }
148
+ export type DiagMsg = DiagObj[Expected];
139
149
  };
140
150
  ((_: ${requiredAttrsNs}.Expected extends ${requiredAttrsNs}.Collected ? string : ${requiredAttrsNs}.Expected) => 0)(${needleString});
141
151
  `;
@@ -389,7 +389,7 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
389
389
  state.literalFromIdentifier.add(lit);
390
390
  return lit;
391
391
  } else {
392
- return { ...(visit(attr) as Expression) };
392
+ return visit(attr) as Expression;
393
393
  }
394
394
  },
395
395
  );
@@ -434,10 +434,12 @@ export const gtsToTypingsWalker: Visitors<Node, TypingTranspileState> = {
434
434
  },
435
435
  ],
436
436
  });
437
+ enterVMFromAttr(state, returnValue);
437
438
  if (body.namedAttributes) {
438
- enterVMFromAttr(state, returnValue);
439
439
  visit(body.namedAttributes);
440
440
  exitVM(state, body.namedAttributes.range);
441
+ } else {
442
+ exitVM(state, name.range);
441
443
  }
442
444
  if (bindingName) {
443
445
  const export_ = node.bindingAccessModifier !== "private";