esrap 1.4.5 → 1.4.7
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 +2 -2
- package/src/handlers.js +38 -20
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "esrap",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.7",
|
|
4
4
|
"description": "Parse in reverse",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -20,10 +20,10 @@
|
|
|
20
20
|
"types": "./types/index.d.ts",
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@changesets/cli": "^2.27.11",
|
|
23
|
+
"@sveltejs/acorn-typescript": "^1.0.5",
|
|
23
24
|
"@typescript-eslint/types": "^8.2.0",
|
|
24
25
|
"@vitest/ui": "^2.1.1",
|
|
25
26
|
"acorn": "^8.11.3",
|
|
26
|
-
"acorn-typescript": "^1.4.13",
|
|
27
27
|
"dts-buddy": "^0.5.4",
|
|
28
28
|
"prettier": "^3.0.3",
|
|
29
29
|
"typescript": "^5.7.2",
|
package/src/handlers.js
CHANGED
|
@@ -116,15 +116,10 @@ function prepend_comments(comments, state, newlines) {
|
|
|
116
116
|
*/
|
|
117
117
|
function quote(string, char) {
|
|
118
118
|
let out = char;
|
|
119
|
-
let escaped = false;
|
|
120
119
|
|
|
121
120
|
for (const c of string) {
|
|
122
|
-
if (
|
|
123
|
-
out += c;
|
|
124
|
-
escaped = false;
|
|
125
|
-
} else if (c === '\\') {
|
|
121
|
+
if (c === '\\') {
|
|
126
122
|
out += '\\\\';
|
|
127
|
-
escaped = true;
|
|
128
123
|
} else if (c === char) {
|
|
129
124
|
out += '\\' + c;
|
|
130
125
|
} else if (c === '\n') {
|
|
@@ -501,8 +496,9 @@ function handle_type_annotation(node, state) {
|
|
|
501
496
|
case 'TSTypeReference':
|
|
502
497
|
handle(node.typeName, state);
|
|
503
498
|
|
|
504
|
-
|
|
505
|
-
|
|
499
|
+
if (node.typeArguments) {
|
|
500
|
+
handle_type_annotation(node.typeArguments, state);
|
|
501
|
+
}
|
|
506
502
|
break;
|
|
507
503
|
case 'TSTypeParameterInstantiation':
|
|
508
504
|
case 'TSTypeParameterDeclaration':
|
|
@@ -514,7 +510,7 @@ function handle_type_annotation(node, state) {
|
|
|
514
510
|
state.commands.push('>');
|
|
515
511
|
break;
|
|
516
512
|
case 'TSTypeParameter':
|
|
517
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
513
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
518
514
|
state.commands.push(node.name);
|
|
519
515
|
|
|
520
516
|
if (node.constraint) {
|
|
@@ -536,14 +532,14 @@ function handle_type_annotation(node, state) {
|
|
|
536
532
|
case 'TSFunctionType':
|
|
537
533
|
if (node.typeParameters) handle_type_annotation(node.typeParameters, state);
|
|
538
534
|
|
|
539
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
535
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
540
536
|
const parameters = node.parameters;
|
|
541
537
|
state.commands.push('(');
|
|
542
538
|
sequence(parameters, state, false, handle);
|
|
543
539
|
|
|
544
540
|
state.commands.push(') => ');
|
|
545
541
|
|
|
546
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
542
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
547
543
|
handle_type_annotation(node.typeAnnotation.typeAnnotation, state);
|
|
548
544
|
break;
|
|
549
545
|
case 'TSIndexSignature':
|
|
@@ -552,19 +548,19 @@ function handle_type_annotation(node, state) {
|
|
|
552
548
|
sequence(indexParameters, state, false, handle);
|
|
553
549
|
state.commands.push(']');
|
|
554
550
|
|
|
555
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
551
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
556
552
|
handle_type_annotation(node.typeAnnotation, state);
|
|
557
553
|
break;
|
|
558
554
|
case 'TSMethodSignature':
|
|
559
555
|
handle(node.key, state);
|
|
560
556
|
|
|
561
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
557
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
562
558
|
const parametersSignature = node.parameters;
|
|
563
559
|
state.commands.push('(');
|
|
564
560
|
sequence(parametersSignature, state, false, handle);
|
|
565
561
|
state.commands.push(')');
|
|
566
562
|
|
|
567
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
563
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
568
564
|
handle_type_annotation(node.typeAnnotation, state);
|
|
569
565
|
break;
|
|
570
566
|
case 'TSExpressionWithTypeArguments':
|
|
@@ -715,8 +711,7 @@ const shared = {
|
|
|
715
711
|
state.commands.push('?.');
|
|
716
712
|
}
|
|
717
713
|
|
|
718
|
-
|
|
719
|
-
if (node.typeParameters) handle_type_annotation(node.typeParameters, state);
|
|
714
|
+
if (node.typeArguments) handle_type_annotation(node.typeArguments, state);
|
|
720
715
|
|
|
721
716
|
const open = create_sequence();
|
|
722
717
|
const join = create_sequence();
|
|
@@ -852,7 +847,7 @@ const shared = {
|
|
|
852
847
|
state.commands.push('...');
|
|
853
848
|
handle(node.argument, state);
|
|
854
849
|
|
|
855
|
-
// @ts-expect-error `acorn-typescript` and `@typescript-
|
|
850
|
+
// @ts-expect-error `acorn-typescript` and `@typescript-eslint/types` have slightly different type definitions
|
|
856
851
|
if (node.typeAnnotation) handle_type_annotation(node.typeAnnotation, state);
|
|
857
852
|
}
|
|
858
853
|
};
|
|
@@ -1054,7 +1049,8 @@ const handlers = {
|
|
|
1054
1049
|
if (
|
|
1055
1050
|
node.expression.type === 'ObjectExpression' ||
|
|
1056
1051
|
(node.expression.type === 'AssignmentExpression' &&
|
|
1057
|
-
node.expression.left.type === 'ObjectPattern')
|
|
1052
|
+
node.expression.left.type === 'ObjectPattern') ||
|
|
1053
|
+
node.expression.type === 'FunctionExpression'
|
|
1058
1054
|
) {
|
|
1059
1055
|
// is an AssignmentExpression to an ObjectPattern
|
|
1060
1056
|
state.commands.push('(');
|
|
@@ -1169,12 +1165,34 @@ const handlers = {
|
|
|
1169
1165
|
|
|
1170
1166
|
state.commands.push(' from ');
|
|
1171
1167
|
handle(node.source, state);
|
|
1168
|
+
if (node.attributes) {
|
|
1169
|
+
state.commands.push(' with { ');
|
|
1170
|
+
for (let index = 0; index < node.attributes.length; index++) {
|
|
1171
|
+
const { key, value } = node.attributes[index];
|
|
1172
|
+
handle(key, state);
|
|
1173
|
+
state.commands.push(': ');
|
|
1174
|
+
handle(value, state);
|
|
1175
|
+
if (index + 1 !== node.attributes.length) {
|
|
1176
|
+
state.commands.push(', ');
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
state.commands.push(' }');
|
|
1180
|
+
}
|
|
1172
1181
|
state.commands.push(';');
|
|
1173
1182
|
},
|
|
1174
1183
|
|
|
1175
1184
|
ImportExpression(node, state) {
|
|
1176
1185
|
state.commands.push('import(');
|
|
1177
1186
|
handle(node.source, state);
|
|
1187
|
+
//@ts-expect-error for some reason the types haven't been updated
|
|
1188
|
+
if (node.arguments) {
|
|
1189
|
+
//@ts-expect-error
|
|
1190
|
+
for (let index = 0; index < node.arguments.length; index++) {
|
|
1191
|
+
state.commands.push(', ');
|
|
1192
|
+
//@ts-expect-error
|
|
1193
|
+
handle(node.arguments[index], state);
|
|
1194
|
+
}
|
|
1195
|
+
}
|
|
1178
1196
|
state.commands.push(')');
|
|
1179
1197
|
},
|
|
1180
1198
|
|
|
@@ -1518,7 +1536,7 @@ const handlers = {
|
|
|
1518
1536
|
|
|
1519
1537
|
TSModuleBlock(node, state) {
|
|
1520
1538
|
state.commands.push(' {', indent, newline);
|
|
1521
|
-
|
|
1539
|
+
handle_body(node.body, state);
|
|
1522
1540
|
state.commands.push(dedent, newline, '}');
|
|
1523
1541
|
},
|
|
1524
1542
|
|
|
@@ -1538,7 +1556,7 @@ const handlers = {
|
|
|
1538
1556
|
},
|
|
1539
1557
|
|
|
1540
1558
|
TSInterfaceBody(node, state) {
|
|
1541
|
-
sequence(node.body, state,
|
|
1559
|
+
sequence(node.body, state, true, handle_type_annotation, ';');
|
|
1542
1560
|
},
|
|
1543
1561
|
|
|
1544
1562
|
TSInterfaceDeclaration(node, state) {
|