@gi-tcg/gts-transpiler 0.4.3 → 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
@@ -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
  `;
@@ -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,18 +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
- ],
1982
- queryBindings: ["my", "opp"]
1973
+ shortcutFunctionPreludes: ["$"],
1974
+ queryBindings: [
1975
+ "my",
1976
+ "opp",
1977
+ "macros"
1978
+ ]
1983
1979
  };
1984
1980
  function* resolveGtsConfigImpl(filePath, inlineConfig = {}, options) {
1985
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.3",
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,18 +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
- ],
47
- queryBindings: ["my", "opp"],
36
+ shortcutFunctionPreludes: ["$"],
37
+ queryBindings: ["my", "opp", "macros"],
48
38
  };
49
39
 
50
40
  function* resolveGtsConfigImpl(
@@ -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
  `;
@@ -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";