@gi-tcg/gts-transpiler 0.7.0 → 0.7.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 +5 -1
- package/dist/index.js +23 -6
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/parse/comment.js +15 -3
- package/src/parse/gts_plugin.ts +6 -2
- package/src/utils/index.ts +13 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import * as acorn from "acorn";
|
|
2
2
|
import { Options as Options$1, Parser as Parser$1 } from "acorn";
|
|
3
|
+
import { AST as AST$1 } from "espolar";
|
|
3
4
|
import * as AST from "estree";
|
|
4
5
|
import { Program, SourceLocation } from "estree";
|
|
5
6
|
import { SourceMap } from "magic-string";
|
|
@@ -1528,8 +1529,11 @@ interface ResolveGtsConfigAsyncOptions extends ResolveGtsConfigBaseOptions {
|
|
|
1528
1529
|
declare function resolveGtsConfig(filePath: string, inlineConfig: GtsConfig, options: ResolveGtsConfigAsyncOptions): Promise<Required<GtsConfig>>;
|
|
1529
1530
|
declare function resolveGtsConfigSync(filePath: string, inlineConfig: GtsConfig, options: ResolveGtsConfigSyncOptions): Required<GtsConfig>;
|
|
1530
1531
|
//#endregion
|
|
1532
|
+
//#region src/utils/index.d.ts
|
|
1533
|
+
declare function isGtsShortcutArgumentStartingConciseBody(node: AST$1.Expression | AST$1.BlockStatement | AST$1.PrivateIdentifier): boolean;
|
|
1534
|
+
//#endregion
|
|
1531
1535
|
//#region src/index.d.ts
|
|
1532
1536
|
declare function transpile(source: string, filename: string, option: TranspileOption): TranspileResult;
|
|
1533
1537
|
declare function transpileForVolar(source: string, filename: string, option: TranspileOption): VolarMappingResult;
|
|
1534
1538
|
//#endregion
|
|
1535
|
-
export { type AST, 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 };
|
|
1539
|
+
export { type AST, type GtsConfig, type ParseLooseOptions as GtsParseLooseOptions, type ParseOptions as GtsParseOptions, GtsTranspilerError, type PathModule, type ResolveGtsConfigAsyncOptions, type ResolveGtsConfigSyncOptions, type TranspileOption, type TranspileResult, type VolarMappingResult, isGtsShortcutArgumentStartingConciseBody, parse, parseLoose, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import { tsPlugin } from "@sveltejs/acorn-typescript";
|
|
|
3
3
|
import { walk } from "zimmerframe";
|
|
4
4
|
import { print } from "esrap";
|
|
5
5
|
import jsPrinter from "esrap/languages/ts";
|
|
6
|
-
import { defaultPrinters, print as print$1 } from "espolar";
|
|
6
|
+
import { canStartConciseBody, defaultPrinters, print as print$1 } from "espolar";
|
|
7
7
|
import dedent from "dedent";
|
|
8
8
|
import browserPath from "path-browserify-esm";
|
|
9
9
|
//#region src/keywords.ts
|
|
@@ -185,8 +185,10 @@ function gtsPlugin(options = {}) {
|
|
|
185
185
|
if (this.type === tokTypes.braceL) break;
|
|
186
186
|
if (this.type === tokTypes.semi || this.canInsertSemicolon()) break;
|
|
187
187
|
if (this.isContextual("as")) break;
|
|
188
|
-
if (!first)
|
|
189
|
-
|
|
188
|
+
if (!first) {
|
|
189
|
+
this.expect(tokTypes.comma);
|
|
190
|
+
if (this.type === tokTypes.braceL) break;
|
|
191
|
+
} else first = false;
|
|
190
192
|
node.attributes.push(this.gts_parseAttributeExpression());
|
|
191
193
|
}
|
|
192
194
|
return this.finishNode(node, "GTSPositionalAttributeList");
|
|
@@ -227,7 +229,7 @@ function gtsPlugin(options = {}) {
|
|
|
227
229
|
this.next();
|
|
228
230
|
return this.gts_parseShortcutFunction();
|
|
229
231
|
}
|
|
230
|
-
if (this.gtsOptions.allowEmptyPositionalAttribute && (this.type === tokTypes.comma || this.type === tokTypes.braceR || this.type === tokTypes.semi || this.
|
|
232
|
+
if (this.gtsOptions.allowEmptyPositionalAttribute && (this.type === tokTypes.comma || this.type === tokTypes.braceR || this.type === tokTypes.semi || this.isContextual("as"))) {
|
|
231
233
|
const dummy = this.startNodeAt(this.lastTokEnd, this.lastTokEndLoc);
|
|
232
234
|
dummy.name = "✖";
|
|
233
235
|
dummy.isDummy = true;
|
|
@@ -388,7 +390,11 @@ function getCommentHandlers(source, comments, index = 0) {
|
|
|
388
390
|
if (node.innerComments && node.innerComments.length > 0) return;
|
|
389
391
|
}
|
|
390
392
|
const parent = path.at(-1);
|
|
391
|
-
if (parent === void 0
|
|
393
|
+
if (parent === void 0) {
|
|
394
|
+
while (comments.length) (node.leadingComments ||= []).push(comments.shift());
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (node.end !== parent.end) {
|
|
392
398
|
const slice = source.slice(node.end, comments[0].start);
|
|
393
399
|
let is_last_in_array = false;
|
|
394
400
|
/** @type {(AST.Node | null)[] | null} */
|
|
@@ -2031,6 +2037,17 @@ function* readPackageConfig(readFileFn, pkgPath) {
|
|
|
2031
2037
|
}
|
|
2032
2038
|
}
|
|
2033
2039
|
//#endregion
|
|
2040
|
+
//#region src/utils/index.ts
|
|
2041
|
+
function isGtsShortcutArgument(node) {
|
|
2042
|
+
return node.type === "GTSShortcutArgumentExpression";
|
|
2043
|
+
}
|
|
2044
|
+
function canStartConciseBodyGts(node) {
|
|
2045
|
+
return canStartConciseBody(node, isGtsShortcutArgument);
|
|
2046
|
+
}
|
|
2047
|
+
function isGtsShortcutArgumentStartingConciseBody(node) {
|
|
2048
|
+
return !canStartConciseBodyGts(node) && canStartConciseBody(node);
|
|
2049
|
+
}
|
|
2050
|
+
//#endregion
|
|
2034
2051
|
//#region src/index.ts
|
|
2035
2052
|
function transpile(source, filename, option) {
|
|
2036
2053
|
return transform(parse(source), option, {
|
|
@@ -2045,4 +2062,4 @@ function transpileForVolar(source, filename, option) {
|
|
|
2045
2062
|
});
|
|
2046
2063
|
}
|
|
2047
2064
|
//#endregion
|
|
2048
|
-
export { GtsTranspilerError, parse, parseLoose, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|
|
2065
|
+
export { GtsTranspilerError, isGtsShortcutArgumentStartingConciseBody, parse, parseLoose, resolveGtsConfig, resolveGtsConfigSync, transpile, transpileForVolar };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gi-tcg/gts-transpiler",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "https://github.com/piovium/gts.git"
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"@sveltejs/acorn-typescript": "^1.0.8",
|
|
23
23
|
"acorn": "^8.15.0",
|
|
24
24
|
"dedent": "^1.7.2",
|
|
25
|
-
"espolar": "^0.6.
|
|
25
|
+
"espolar": "^0.6.5",
|
|
26
26
|
"esrap": "2.2.1",
|
|
27
27
|
"magic-string": "^0.30.21",
|
|
28
28
|
"path-browserify-esm": "^1.0.6",
|
package/src/index.ts
CHANGED
package/src/parse/comment.js
CHANGED
|
@@ -184,9 +184,21 @@ export function getCommentHandlers(source, comments, index = 0) {
|
|
|
184
184
|
}
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
const parent = /** @type {AST.Node & AST.NodeWithLocation} */ (path.at(-1));
|
|
187
|
+
const parent = /** @type {AST.Node & AST.NodeWithLocation | undefined} */ (path.at(-1));
|
|
188
|
+
|
|
189
|
+
// A comment-only file leaves Program as the sole visited node. There is
|
|
190
|
+
// no parent to use for sibling-based attachment, so retain the remaining
|
|
191
|
+
// comments on Program itself.
|
|
192
|
+
if (parent === undefined) {
|
|
193
|
+
while (comments.length) {
|
|
194
|
+
(node.leadingComments ||= []).push(
|
|
195
|
+
/** @type {AST.CommentWithLocation} */ (comments.shift()),
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
return;
|
|
199
|
+
}
|
|
188
200
|
|
|
189
|
-
if (
|
|
201
|
+
if (node.end !== parent.end) {
|
|
190
202
|
const slice = source.slice(node.end, comments[0].start);
|
|
191
203
|
|
|
192
204
|
// Check if this node is the last item in an array-like structure
|
|
@@ -408,4 +420,4 @@ export function getCommentHandlers(source, comments, index = 0) {
|
|
|
408
420
|
});
|
|
409
421
|
},
|
|
410
422
|
};
|
|
411
|
-
}
|
|
423
|
+
}
|
package/src/parse/gts_plugin.ts
CHANGED
|
@@ -20,6 +20,7 @@ AttributeName:
|
|
|
20
20
|
|
|
21
21
|
AttributeBody:
|
|
22
22
|
PositionalAttributeList? NamedAttributeBlock?
|
|
23
|
+
PositionalAttributeList "," NamedAttributeBlock
|
|
23
24
|
|
|
24
25
|
AttributeBindingClause:
|
|
25
26
|
"as" BindingAccessModifier? Identifier
|
|
@@ -44,7 +45,7 @@ AttributeExpression:
|
|
|
44
45
|
":" ShortcutFunction
|
|
45
46
|
# Intentionally disable object literal, for distinction to named attribute block
|
|
46
47
|
# foo bar { baz = 1 };
|
|
47
|
-
# foo bar, { baz: 1 }; //
|
|
48
|
+
# foo bar, { baz: 1 }; // Named attribute block
|
|
48
49
|
# foo bar, ({ baz: 1 }); // OK
|
|
49
50
|
[lookahead != "{"] CallExpression OptionalChain?
|
|
50
51
|
[lookahead != "{"] MemberExpression OptionalChain?
|
|
@@ -181,6 +182,10 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
|
|
|
181
182
|
}
|
|
182
183
|
if (!first) {
|
|
183
184
|
this.expect(tokTypes.comma);
|
|
185
|
+
// A trailing comma is only valid before a named attribute block.
|
|
186
|
+
if (this.type === tokTypes.braceL) {
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
184
189
|
} else {
|
|
185
190
|
first = false;
|
|
186
191
|
}
|
|
@@ -247,7 +252,6 @@ export function gtsPlugin(options: GtsPluginOption = {}) {
|
|
|
247
252
|
(this.type === tokTypes.comma ||
|
|
248
253
|
this.type === tokTypes.braceR ||
|
|
249
254
|
this.type === tokTypes.semi ||
|
|
250
|
-
this.canInsertSemicolon() ||
|
|
251
255
|
this.isContextual("as"))
|
|
252
256
|
) {
|
|
253
257
|
// Allow omitting the attribute expression for language tooling
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { canStartConciseBody, type AST } from "espolar";
|
|
2
|
+
|
|
3
|
+
function isGtsShortcutArgument(node: AST.Node) {
|
|
4
|
+
return (node.type as string) === "GTSShortcutArgumentExpression";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function canStartConciseBodyGts(node: AST.Expression | AST.BlockStatement | AST.PrivateIdentifier): boolean {
|
|
8
|
+
return canStartConciseBody(node, isGtsShortcutArgument);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function isGtsShortcutArgumentStartingConciseBody(node: AST.Expression | AST.BlockStatement | AST.PrivateIdentifier): boolean {
|
|
12
|
+
return !canStartConciseBodyGts(node) && canStartConciseBody(node);
|
|
13
|
+
}
|