@tsrx/prettier-plugin 0.4.6 → 0.4.8
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/index.js +195 -53
- package/src/index.test.js +265 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/prettier-plugin",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.8",
|
|
4
4
|
"description": "TSRX plugin for Prettier",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"prettier": "^3.9.6"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@tsrx/core": "0.
|
|
30
|
+
"@tsrx/core": "0.3.0"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"src/"
|
package/src/index.js
CHANGED
|
@@ -1228,10 +1228,30 @@ function printTsrxNode(node, path, options, print, args) {
|
|
|
1228
1228
|
nodeContent = printImportDeclaration(node, path, options, print);
|
|
1229
1229
|
break;
|
|
1230
1230
|
|
|
1231
|
+
case 'TSImportEqualsDeclaration':
|
|
1232
|
+
nodeContent = printTSImportEqualsDeclaration(node, path, options, print);
|
|
1233
|
+
break;
|
|
1234
|
+
|
|
1235
|
+
case 'TSExternalModuleReference':
|
|
1236
|
+
nodeContent = ['require(', path.call(print, 'expression'), ')'];
|
|
1237
|
+
break;
|
|
1238
|
+
|
|
1239
|
+
case 'TSExportAssignment':
|
|
1240
|
+
nodeContent = ['export = ', path.call(print, 'expression'), semi(options)];
|
|
1241
|
+
break;
|
|
1242
|
+
|
|
1243
|
+
case 'TSNamespaceExportDeclaration':
|
|
1244
|
+
nodeContent = ['export as namespace ', path.call(print, 'id'), semi(options)];
|
|
1245
|
+
break;
|
|
1246
|
+
|
|
1231
1247
|
case 'ExportNamedDeclaration':
|
|
1232
1248
|
nodeContent = printExportNamedDeclaration(node, path, options, print);
|
|
1233
1249
|
break;
|
|
1234
1250
|
|
|
1251
|
+
case 'ExportAllDeclaration':
|
|
1252
|
+
nodeContent = printExportAllDeclaration(node, options);
|
|
1253
|
+
break;
|
|
1254
|
+
|
|
1235
1255
|
case 'ExportDefaultDeclaration':
|
|
1236
1256
|
nodeContent = printExportDefaultDeclaration(node, path, options, print);
|
|
1237
1257
|
break;
|
|
@@ -2043,6 +2063,10 @@ function printTsrxNode(node, path, options, print, args) {
|
|
|
2043
2063
|
nodeContent = 'string';
|
|
2044
2064
|
break;
|
|
2045
2065
|
|
|
2066
|
+
case 'TSThisType':
|
|
2067
|
+
nodeContent = 'this';
|
|
2068
|
+
break;
|
|
2069
|
+
|
|
2046
2070
|
case 'EmptyStatement':
|
|
2047
2071
|
nodeContent = '';
|
|
2048
2072
|
break;
|
|
@@ -2303,19 +2327,35 @@ function printTsrxNode(node, path, options, print, args) {
|
|
|
2303
2327
|
}
|
|
2304
2328
|
|
|
2305
2329
|
case 'TSModuleDeclaration': {
|
|
2306
|
-
|
|
2307
|
-
|
|
2308
|
-
|
|
2309
|
-
|
|
2310
|
-
|
|
2311
|
-
|
|
2312
|
-
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2330
|
+
const parent = path.getParentNode();
|
|
2331
|
+
/** @type {Doc[]} */
|
|
2332
|
+
const parts = [];
|
|
2333
|
+
// A dotted name (`namespace A.B { … }`) parses as nested declarations
|
|
2334
|
+
// whose body is the next name part, so only the outermost one prints
|
|
2335
|
+
// the keyword; repeating it gives the invalid `namespace A namespace B`.
|
|
2336
|
+
if (parent?.type !== 'TSModuleDeclaration' || parent.body !== node) {
|
|
2337
|
+
if (node.declare) {
|
|
2338
|
+
parts.push('declare ');
|
|
2339
|
+
}
|
|
2340
|
+
// `declare global` augments the global scope; printing it as
|
|
2341
|
+
// `declare module global` declares an unrelated module named `global`.
|
|
2342
|
+
if (node.kind !== 'global') {
|
|
2343
|
+
parts.push(node.kind, ' ');
|
|
2344
|
+
}
|
|
2345
|
+
}
|
|
2346
|
+
parts.push(path.call(print, 'id'));
|
|
2347
|
+
const body = /** @type {typeof node | typeof node.body} */ (
|
|
2348
|
+
/** @type {unknown} */ (node.body)
|
|
2349
|
+
);
|
|
2350
|
+
if (body?.type === 'TSModuleDeclaration') {
|
|
2351
|
+
parts.push('.', path.call(print, 'body'));
|
|
2352
|
+
} else if (body) {
|
|
2353
|
+
parts.push(' ', path.call(print, 'body'));
|
|
2354
|
+
} else {
|
|
2355
|
+
// Shorthand ambient module: `declare module 'name';`
|
|
2356
|
+
parts.push(semi(options));
|
|
2357
|
+
}
|
|
2358
|
+
nodeContent = parts;
|
|
2319
2359
|
break;
|
|
2320
2360
|
}
|
|
2321
2361
|
|
|
@@ -2637,7 +2677,11 @@ function printTsrxNode(node, path, options, print, args) {
|
|
|
2637
2677
|
|
|
2638
2678
|
case 'TSTypeQuery': {
|
|
2639
2679
|
const expr = path.call(print, 'exprName');
|
|
2640
|
-
|
|
2680
|
+
// `typeof fn<string>` is an instantiation expression: dropping the
|
|
2681
|
+
// arguments turns a specialized type back into the generic one.
|
|
2682
|
+
nodeContent = node.typeArguments
|
|
2683
|
+
? ['typeof ', expr, path.call(print, 'typeArguments')]
|
|
2684
|
+
: ['typeof ', expr];
|
|
2641
2685
|
break;
|
|
2642
2686
|
}
|
|
2643
2687
|
|
|
@@ -2645,6 +2689,10 @@ function printTsrxNode(node, path, options, print, args) {
|
|
|
2645
2689
|
/** @type {Doc[]} */
|
|
2646
2690
|
const parts = [];
|
|
2647
2691
|
|
|
2692
|
+
if (node.typeParameters) {
|
|
2693
|
+
parts.push(path.call(print, 'typeParameters'));
|
|
2694
|
+
}
|
|
2695
|
+
|
|
2648
2696
|
// Handle parameters
|
|
2649
2697
|
parts.push('(');
|
|
2650
2698
|
if (node.parameters && node.parameters.length > 0) {
|
|
@@ -2879,7 +2927,7 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2879
2927
|
} else if (spec.type === 'ImportSpecifier') {
|
|
2880
2928
|
// Handle inline type imports: import { type Component } from '@example/runtime'
|
|
2881
2929
|
const typePrefix = spec.importKind === 'type' ? 'type ' : '';
|
|
2882
|
-
const importedName =
|
|
2930
|
+
const importedName = printModuleExportName(spec.imported, options);
|
|
2883
2931
|
const localName = spec.local.name;
|
|
2884
2932
|
const importName =
|
|
2885
2933
|
importedName === localName
|
|
@@ -2923,13 +2971,40 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2923
2971
|
parts.push(' from');
|
|
2924
2972
|
}
|
|
2925
2973
|
|
|
2974
|
+
parts.push(' ', printModuleSource(node, options), semi(options));
|
|
2975
|
+
|
|
2976
|
+
return parts;
|
|
2977
|
+
}
|
|
2978
|
+
|
|
2979
|
+
/**
|
|
2980
|
+
* Print a name in an import or export clause. A name that is not a valid
|
|
2981
|
+
* identifier is written as a string literal: `export { a as "a-b" }`.
|
|
2982
|
+
* @param {AST.Identifier | AST.Literal} node
|
|
2983
|
+
* @param {TsrxFormatOptions} options
|
|
2984
|
+
* @returns {string}
|
|
2985
|
+
*/
|
|
2986
|
+
function printModuleExportName(node, options) {
|
|
2987
|
+
return node.type === 'Identifier'
|
|
2988
|
+
? node.name
|
|
2989
|
+
: formatStringLiteral(/** @type {string} */ (node.value), options);
|
|
2990
|
+
}
|
|
2991
|
+
|
|
2992
|
+
/**
|
|
2993
|
+
* Print the module an import or re-export loads, with its import attributes:
|
|
2994
|
+
* `"./data.json" with { type: "json" }`. Dropping the attributes changes how
|
|
2995
|
+
* the module loads, so every declaration with a source prints them.
|
|
2996
|
+
* @param {AST.TSRXImportDeclaration | AST.ExportNamedDeclaration | AST.ExportAllDeclaration} node
|
|
2997
|
+
* @param {TsrxFormatOptions} options
|
|
2998
|
+
* @returns {Doc[]}
|
|
2999
|
+
*/
|
|
3000
|
+
function printModuleSource(node, options) {
|
|
2926
3001
|
const source = /** @type {AST.Literal | AST.Identifier} */ (/** @type {unknown} */ (node.source));
|
|
2927
|
-
|
|
3002
|
+
/** @type {Doc[]} */
|
|
3003
|
+
const parts = [
|
|
2928
3004
|
source.type === 'Identifier'
|
|
2929
3005
|
? source.name
|
|
2930
|
-
: formatStringLiteral(/** @type {string} */ (source.value), options)
|
|
2931
|
-
|
|
2932
|
-
parts.push(' ', sourceDoc);
|
|
3006
|
+
: formatStringLiteral(/** @type {string} */ (source.value), options),
|
|
3007
|
+
];
|
|
2933
3008
|
|
|
2934
3009
|
const attributes =
|
|
2935
3010
|
/** @type {Array<{ key: AST.Identifier | AST.Literal, value: AST.Literal }>} */ (
|
|
@@ -2947,18 +3022,39 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2947
3022
|
parts.push(' with { ', join(', ', attributeDocs), ' }');
|
|
2948
3023
|
}
|
|
2949
3024
|
|
|
2950
|
-
parts.push(semi(options));
|
|
2951
|
-
|
|
2952
3025
|
return parts;
|
|
2953
3026
|
}
|
|
2954
3027
|
|
|
3028
|
+
/**
|
|
3029
|
+
* Print an import alias: `import A = Foo.Bar;` or `import fs = require("fs");`.
|
|
3030
|
+
* The alias is a runtime binding, so it must never fall through to the
|
|
3031
|
+
* unknown-node fallback. acorn-typescript flags `export import` with `isExport`
|
|
3032
|
+
* rather than wrapping it in an `ExportNamedDeclaration`.
|
|
3033
|
+
* @param {AST.TSImportEqualsDeclaration} node
|
|
3034
|
+
* @param {AstPath<AST.TSImportEqualsDeclaration>} path
|
|
3035
|
+
* @param {TsrxFormatOptions} options
|
|
3036
|
+
* @param {PrintFn} print
|
|
3037
|
+
* @returns {Doc[]}
|
|
3038
|
+
*/
|
|
3039
|
+
function printTSImportEqualsDeclaration(node, path, options, print) {
|
|
3040
|
+
return [
|
|
3041
|
+
/** @type {{ isExport?: boolean }} */ (node).isExport ? 'export ' : '',
|
|
3042
|
+
'import ',
|
|
3043
|
+
node.importKind === 'type' ? 'type ' : '',
|
|
3044
|
+
path.call(print, 'id'),
|
|
3045
|
+
' = ',
|
|
3046
|
+
path.call(print, 'moduleReference'),
|
|
3047
|
+
semi(options),
|
|
3048
|
+
];
|
|
3049
|
+
}
|
|
3050
|
+
|
|
2955
3051
|
/**
|
|
2956
3052
|
* Print an export named declaration
|
|
2957
3053
|
* @param {AST.ExportNamedDeclaration} node - The export declaration node
|
|
2958
3054
|
* @param {AstPath<AST.ExportNamedDeclaration>} path - The AST path
|
|
2959
3055
|
* @param {TsrxFormatOptions} options - Prettier options
|
|
2960
3056
|
* @param {PrintFn} print - Print callback
|
|
2961
|
-
* @returns {Doc[]
|
|
3057
|
+
* @returns {Doc[]}
|
|
2962
3058
|
*/
|
|
2963
3059
|
function printExportNamedDeclaration(node, path, options, print) {
|
|
2964
3060
|
if (node.declaration) {
|
|
@@ -2968,42 +3064,52 @@ function printExportNamedDeclaration(node, path, options, print) {
|
|
|
2968
3064
|
parts.push('export ');
|
|
2969
3065
|
parts.push(path.call(print, 'declaration'));
|
|
2970
3066
|
return parts;
|
|
2971
|
-
}
|
|
2972
|
-
const specifiers = node.specifiers.map((spec) => {
|
|
2973
|
-
const typePrefix = spec.exportKind === 'type' ? 'type ' : '';
|
|
2974
|
-
const exportedName = /** @type {AST.Identifier} */ (spec.exported).name;
|
|
2975
|
-
const localName = /** @type {AST.Identifier} */ (spec.local).name;
|
|
2976
|
-
if (exportedName === localName) {
|
|
2977
|
-
return typePrefix + localName;
|
|
2978
|
-
} else {
|
|
2979
|
-
return typePrefix + localName + ' as ' + exportedName;
|
|
2980
|
-
}
|
|
2981
|
-
});
|
|
3067
|
+
}
|
|
2982
3068
|
|
|
2983
|
-
|
|
2984
|
-
|
|
2985
|
-
|
|
2986
|
-
|
|
3069
|
+
const specifiers = node.specifiers.map((spec) => {
|
|
3070
|
+
const typePrefix = spec.exportKind === 'type' ? 'type ' : '';
|
|
3071
|
+
const exportedName = printModuleExportName(spec.exported, options);
|
|
3072
|
+
const localName = printModuleExportName(spec.local, options);
|
|
3073
|
+
if (exportedName === localName) {
|
|
3074
|
+
return typePrefix + localName;
|
|
3075
|
+
} else {
|
|
3076
|
+
return typePrefix + localName + ' as ' + exportedName;
|
|
2987
3077
|
}
|
|
2988
|
-
|
|
3078
|
+
});
|
|
2989
3079
|
|
|
2990
|
-
|
|
2991
|
-
|
|
2992
|
-
|
|
2993
|
-
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
: formatStringLiteral(/** @type {string} */ (source.value), options),
|
|
2999
|
-
);
|
|
3000
|
-
}
|
|
3001
|
-
parts.push(semi(options));
|
|
3080
|
+
// `export {};` still marks the file as a module, and `export {} from "x"`
|
|
3081
|
+
// still loads `x`, so an empty list keeps its braces. A bare `export` would
|
|
3082
|
+
// export the next declaration or fail to parse at the end of the file.
|
|
3083
|
+
/** @type {Doc[]} */
|
|
3084
|
+
const parts = [
|
|
3085
|
+
node.exportKind === 'type' ? 'export type ' : 'export ',
|
|
3086
|
+
specifiers.length > 0 ? ['{ ', join(', ', specifiers), ' }'] : '{}',
|
|
3087
|
+
];
|
|
3002
3088
|
|
|
3003
|
-
|
|
3089
|
+
if (node.source) {
|
|
3090
|
+
parts.push(' from ', printModuleSource(node, options));
|
|
3004
3091
|
}
|
|
3092
|
+
parts.push(semi(options));
|
|
3093
|
+
|
|
3094
|
+
return parts;
|
|
3095
|
+
}
|
|
3005
3096
|
|
|
3006
|
-
|
|
3097
|
+
/**
|
|
3098
|
+
* Print a star re-export: `export * from "x";` or `export type * as ns from "x";`.
|
|
3099
|
+
* @param {AST.ExportAllDeclaration} node
|
|
3100
|
+
* @param {TsrxFormatOptions} options
|
|
3101
|
+
* @returns {Doc[]}
|
|
3102
|
+
*/
|
|
3103
|
+
function printExportAllDeclaration(node, options) {
|
|
3104
|
+
return [
|
|
3105
|
+
/** @type {{ exportKind?: string }} */ (node).exportKind === 'type'
|
|
3106
|
+
? 'export type * '
|
|
3107
|
+
: 'export * ',
|
|
3108
|
+
node.exported ? ['as ', printModuleExportName(node.exported, options), ' '] : '',
|
|
3109
|
+
'from ',
|
|
3110
|
+
printModuleSource(node, options),
|
|
3111
|
+
semi(options),
|
|
3112
|
+
];
|
|
3007
3113
|
}
|
|
3008
3114
|
|
|
3009
3115
|
/**
|
|
@@ -4416,6 +4522,16 @@ function printClassDeclaration(node, path, options, print) {
|
|
|
4416
4522
|
if (node.superClass) {
|
|
4417
4523
|
parts.push(' extends ');
|
|
4418
4524
|
parts.push(path.call(print, 'superClass'));
|
|
4525
|
+
if (node.superTypeParameters) {
|
|
4526
|
+
parts.push(path.call(print, 'superTypeParameters'));
|
|
4527
|
+
}
|
|
4528
|
+
}
|
|
4529
|
+
|
|
4530
|
+
// Heritage type arguments and implements clauses are what TypeScript
|
|
4531
|
+
// checks the class against, so dropping them silently loses those checks
|
|
4532
|
+
if (node.implements && node.implements.length > 0) {
|
|
4533
|
+
parts.push(' implements ');
|
|
4534
|
+
parts.push(join(', ', path.map(print, 'implements')));
|
|
4419
4535
|
}
|
|
4420
4536
|
|
|
4421
4537
|
parts.push(' ');
|
|
@@ -4569,6 +4685,12 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4569
4685
|
parts.push('?');
|
|
4570
4686
|
}
|
|
4571
4687
|
|
|
4688
|
+
// Definite-assignment assertion (`value!: T`) — dropping it makes TS
|
|
4689
|
+
// report the field as unassigned under strictPropertyInitialization
|
|
4690
|
+
if (node.definite) {
|
|
4691
|
+
parts.push('!');
|
|
4692
|
+
}
|
|
4693
|
+
|
|
4572
4694
|
// Type annotation
|
|
4573
4695
|
if (node.typeAnnotation) {
|
|
4574
4696
|
parts.push(': ');
|
|
@@ -4639,6 +4761,11 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4639
4761
|
// the key is 'constructor' and we already handled that above
|
|
4640
4762
|
parts.push(...printKey(node, path, options, print));
|
|
4641
4763
|
|
|
4764
|
+
// Optional marker — dropping it turns `onMount?()` into a required member
|
|
4765
|
+
if (node.optional) {
|
|
4766
|
+
parts.push('?');
|
|
4767
|
+
}
|
|
4768
|
+
|
|
4642
4769
|
// Add TypeScript generics if present (always on the method node, not on value)
|
|
4643
4770
|
if (node.typeParameters) {
|
|
4644
4771
|
const typeParams = path.call(print, 'typeParameters');
|
|
@@ -5167,6 +5294,15 @@ function printTSTypeParameterDeclaration(node, path, options, print) {
|
|
|
5167
5294
|
function printTSTypeParameter(node, path, options, print) {
|
|
5168
5295
|
/** @type {Doc[]} */
|
|
5169
5296
|
const parts = [];
|
|
5297
|
+
if (node.const) {
|
|
5298
|
+
parts.push('const ');
|
|
5299
|
+
}
|
|
5300
|
+
if (node.in) {
|
|
5301
|
+
parts.push('in ');
|
|
5302
|
+
}
|
|
5303
|
+
if (node.out) {
|
|
5304
|
+
parts.push('out ');
|
|
5305
|
+
}
|
|
5170
5306
|
parts.push(node.name);
|
|
5171
5307
|
|
|
5172
5308
|
if (node.constraint) {
|
|
@@ -6273,6 +6409,9 @@ function printTSConstructorType(node, path, options, print) {
|
|
|
6273
6409
|
parts.push('abstract ');
|
|
6274
6410
|
}
|
|
6275
6411
|
parts.push('new ');
|
|
6412
|
+
if (node.typeParameters) {
|
|
6413
|
+
parts.push(path.call(print, 'typeParameters'));
|
|
6414
|
+
}
|
|
6276
6415
|
parts.push('(');
|
|
6277
6416
|
const hasParameters = Array.isArray(node.parameters) && node.parameters.length > 0;
|
|
6278
6417
|
if (hasParameters) {
|
|
@@ -6391,7 +6530,10 @@ function printTSImportType(node, path, options, print) {
|
|
|
6391
6530
|
parts.push('.', path.call(print, 'qualifier'));
|
|
6392
6531
|
}
|
|
6393
6532
|
|
|
6394
|
-
|
|
6533
|
+
// acorn-typescript stores import type arguments on typeArguments
|
|
6534
|
+
if (node.typeArguments) {
|
|
6535
|
+
parts.push(path.call(print, 'typeArguments'));
|
|
6536
|
+
} else if (node.typeParameters) {
|
|
6395
6537
|
parts.push(path.call(print, 'typeParameters'));
|
|
6396
6538
|
}
|
|
6397
6539
|
|
package/src/index.test.js
CHANGED
|
@@ -6846,6 +6846,24 @@ function g() {
|
|
|
6846
6846
|
});
|
|
6847
6847
|
|
|
6848
6848
|
describe('type parameter declarations', () => {
|
|
6849
|
+
it.each([
|
|
6850
|
+
'type Select<T> = <K extends keyof T>(value: T[K]) => T[K];',
|
|
6851
|
+
'type Mapper = <T, U = T>(value: T) => U;',
|
|
6852
|
+
'type Factory = <T = unknown>() => <U extends T>(value: U) => U;',
|
|
6853
|
+
`interface Api {
|
|
6854
|
+
select: <T>(value: T) => T;
|
|
6855
|
+
}`,
|
|
6856
|
+
])('preserves generic function type declarations: %s', async (source) => {
|
|
6857
|
+
expect(await format(source)).toBeWithNewline(source);
|
|
6858
|
+
});
|
|
6859
|
+
|
|
6860
|
+
it.each([
|
|
6861
|
+
'type Constructor = new <T>(value: T) => Box<T>;',
|
|
6862
|
+
'type Constructor = abstract new <T, U = T>(value: U) => Box<T>;',
|
|
6863
|
+
])('preserves generic constructor type declarations: %s', async (source) => {
|
|
6864
|
+
expect(await format(source)).toBeWithNewline(source);
|
|
6865
|
+
});
|
|
6866
|
+
|
|
6849
6867
|
/**
|
|
6850
6868
|
* Formats twice and asserts the output is stable (idempotent).
|
|
6851
6869
|
* @param {string} code
|
|
@@ -6987,6 +7005,28 @@ function g() {
|
|
|
6987
7005
|
expect(result).toBeWithNewline(source);
|
|
6988
7006
|
};
|
|
6989
7007
|
|
|
7008
|
+
it.each([
|
|
7009
|
+
'type Setter<in T> = (value: T) => void;',
|
|
7010
|
+
'type Getter<out T> = () => T;',
|
|
7011
|
+
'type Cell<in out T> = { value: T };',
|
|
7012
|
+
`interface Cell<in out T extends object = object> {
|
|
7013
|
+
value: T;
|
|
7014
|
+
}`,
|
|
7015
|
+
])('keeps type parameter variance: %s', async (source) => {
|
|
7016
|
+
await expectUnchanged(source);
|
|
7017
|
+
});
|
|
7018
|
+
|
|
7019
|
+
it.each([
|
|
7020
|
+
`function identity<const T>(value: T): T {
|
|
7021
|
+
return value;
|
|
7022
|
+
}`,
|
|
7023
|
+
'const identity = <const T,>(value: T): T => value;',
|
|
7024
|
+
'class Box<const T> {}',
|
|
7025
|
+
'class Box<const in out T> {}',
|
|
7026
|
+
])('keeps const type parameters: %s', async (source) => {
|
|
7027
|
+
await expectUnchanged(source);
|
|
7028
|
+
});
|
|
7029
|
+
|
|
6990
7030
|
it('keeps readonly on interface members', async () => {
|
|
6991
7031
|
await expectUnchanged(`export interface BenchRow {
|
|
6992
7032
|
readonly id: number;
|
|
@@ -7052,6 +7092,33 @@ function g() {
|
|
|
7052
7092
|
}`);
|
|
7053
7093
|
});
|
|
7054
7094
|
|
|
7095
|
+
it('keeps the optional marker on class methods', async () => {
|
|
7096
|
+
await expectUnchanged(`declare class Hook {
|
|
7097
|
+
onMount?(): void;
|
|
7098
|
+
onUpdate?<T>(value: T): T;
|
|
7099
|
+
[Symbol.dispose]?(): void;
|
|
7100
|
+
}`);
|
|
7101
|
+
await expectUnchanged(`abstract class Lifecycle {
|
|
7102
|
+
abstract onMount?(): void;
|
|
7103
|
+
static async *stream?() {}
|
|
7104
|
+
onUnmount?() {
|
|
7105
|
+
return;
|
|
7106
|
+
}
|
|
7107
|
+
}`);
|
|
7108
|
+
});
|
|
7109
|
+
|
|
7110
|
+
it('keeps the definite-assignment assertion on class fields', async () => {
|
|
7111
|
+
await expectUnchanged(`export class Model { value!: string; }`);
|
|
7112
|
+
await expectUnchanged(`class Store extends Base {
|
|
7113
|
+
#id!: number;
|
|
7114
|
+
static instance!: Store;
|
|
7115
|
+
private readonly items!: Item[];
|
|
7116
|
+
[key]!: string;
|
|
7117
|
+
override name!: string;
|
|
7118
|
+
accessor state!: State;
|
|
7119
|
+
}`);
|
|
7120
|
+
});
|
|
7121
|
+
|
|
7055
7122
|
it('keeps override on class members', async () => {
|
|
7056
7123
|
await expectUnchanged(`class Derived extends Base {
|
|
7057
7124
|
override toString(): string {
|
|
@@ -7147,6 +7214,204 @@ declare enum Level {
|
|
|
7147
7214
|
});
|
|
7148
7215
|
});
|
|
7149
7216
|
|
|
7217
|
+
// Type arguments, `this` types, and heritage clauses decide what a
|
|
7218
|
+
// declaration means. Dropping one either breaks the file or quietly
|
|
7219
|
+
// widens a type, so each must come back exactly as written.
|
|
7220
|
+
describe('TypeScript types survive formatting', () => {
|
|
7221
|
+
/**
|
|
7222
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7223
|
+
* @param {string} source
|
|
7224
|
+
*/
|
|
7225
|
+
const expectUnchanged = async (source) => {
|
|
7226
|
+
const result = await format(source);
|
|
7227
|
+
expect(result).toBeWithNewline(source);
|
|
7228
|
+
};
|
|
7229
|
+
|
|
7230
|
+
it.each([
|
|
7231
|
+
'const upper: typeof identity<string> = (value) => value.toUpperCase();',
|
|
7232
|
+
'type Pair = typeof ns.pair<number, string>;',
|
|
7233
|
+
'type Loaded = typeof import("./module").load<string>;',
|
|
7234
|
+
])('keeps type arguments on typeof type queries: %s', async (source) => {
|
|
7235
|
+
await expectUnchanged(source);
|
|
7236
|
+
});
|
|
7237
|
+
|
|
7238
|
+
it('keeps type arguments on import types', async () => {
|
|
7239
|
+
await expectUnchanged('type Entry = import("./module").Entry<string>;');
|
|
7240
|
+
});
|
|
7241
|
+
|
|
7242
|
+
it('keeps polymorphic this types', async () => {
|
|
7243
|
+
await expectUnchanged(`interface Builder {
|
|
7244
|
+
self: this;
|
|
7245
|
+
next(): this;
|
|
7246
|
+
all: this[];
|
|
7247
|
+
}`);
|
|
7248
|
+
await expectUnchanged(`class Chain {
|
|
7249
|
+
clone(): this {
|
|
7250
|
+
return this;
|
|
7251
|
+
}
|
|
7252
|
+
}`);
|
|
7253
|
+
});
|
|
7254
|
+
|
|
7255
|
+
it.each([
|
|
7256
|
+
'class Derived extends Base<string> implements Contract<string> {}',
|
|
7257
|
+
'class Derived extends ns.Base<Map<string, number>> {}',
|
|
7258
|
+
'class Derived implements Contract<string>, ns.Other {}',
|
|
7259
|
+
'const Derived = class extends Base<number> implements Contract<number> {};',
|
|
7260
|
+
'abstract class Derived<T> extends Base<T> implements Contract<T> {}',
|
|
7261
|
+
'export default class Derived extends Base<string> implements Contract<string> {}',
|
|
7262
|
+
])('keeps superclass type arguments and implements clauses: %s', async (source) => {
|
|
7263
|
+
await expectUnchanged(source);
|
|
7264
|
+
});
|
|
7265
|
+
});
|
|
7266
|
+
|
|
7267
|
+
// Import aliases and export assignments are runtime bindings. Dropping one
|
|
7268
|
+
// leaves every later reference dangling, and the file still compiles, so the
|
|
7269
|
+
// break only surfaces when the module runs.
|
|
7270
|
+
describe('TypeScript module declarations survive formatting', () => {
|
|
7271
|
+
/**
|
|
7272
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7273
|
+
* @param {string} source
|
|
7274
|
+
*/
|
|
7275
|
+
const expectUnchanged = async (source) => {
|
|
7276
|
+
const result = await format(source);
|
|
7277
|
+
expect(result).toBeWithNewline(source);
|
|
7278
|
+
};
|
|
7279
|
+
|
|
7280
|
+
it.each([
|
|
7281
|
+
'import Alias = Foo;',
|
|
7282
|
+
'import Alias = Foo.Bar.Baz;',
|
|
7283
|
+
'import fs = require("fs");',
|
|
7284
|
+
'import type Types = require("./types");',
|
|
7285
|
+
'export import Alias = Foo.Bar;',
|
|
7286
|
+
'export import type Types = require("./types");',
|
|
7287
|
+
'export = value;',
|
|
7288
|
+
'export as namespace Library;',
|
|
7289
|
+
])('keeps %s', async (source) => {
|
|
7290
|
+
await expectUnchanged(source);
|
|
7291
|
+
});
|
|
7292
|
+
|
|
7293
|
+
it('keeps import aliases and export assignments inside namespaces and modules', async () => {
|
|
7294
|
+
await expectUnchanged(`namespace Outer {
|
|
7295
|
+
export import Alias = Foo;
|
|
7296
|
+
import fs = require("fs");
|
|
7297
|
+
}`);
|
|
7298
|
+
await expectUnchanged(`declare module "library" {
|
|
7299
|
+
const value: number;
|
|
7300
|
+
export = value;
|
|
7301
|
+
}`);
|
|
7302
|
+
});
|
|
7303
|
+
|
|
7304
|
+
it('keeps the import alias a module reads from', async () => {
|
|
7305
|
+
const input = `namespace Foo { export const answer = 1; }
|
|
7306
|
+
import Alias = Foo;
|
|
7307
|
+
export const x = Alias.answer;`;
|
|
7308
|
+
const expected = `namespace Foo {
|
|
7309
|
+
export const answer = 1;
|
|
7310
|
+
}
|
|
7311
|
+
import Alias = Foo;
|
|
7312
|
+
export const x = Alias.answer;`;
|
|
7313
|
+
|
|
7314
|
+
const result = await format(input);
|
|
7315
|
+
expect(result).toBeWithNewline(expected);
|
|
7316
|
+
});
|
|
7317
|
+
|
|
7318
|
+
it('keeps comments inside import aliases', async () => {
|
|
7319
|
+
await expectUnchanged('import /* a */ Alias /* b */ = /* c */ Foo /* d */;');
|
|
7320
|
+
await expectUnchanged('import fs = require(/* why */ "fs");');
|
|
7321
|
+
});
|
|
7322
|
+
|
|
7323
|
+
it('follows quote and semicolon options', async () => {
|
|
7324
|
+
const result = await format(`import fs = require("fs");\nexport = fs;`, {
|
|
7325
|
+
singleQuote: true,
|
|
7326
|
+
semi: false,
|
|
7327
|
+
});
|
|
7328
|
+
expect(result).toBeWithNewline(`import fs = require('fs')\nexport = fs`);
|
|
7329
|
+
});
|
|
7330
|
+
|
|
7331
|
+
// A dotted name parses as nested declarations. Printing the keyword for
|
|
7332
|
+
// each part gave `namespace A namespace B { … }`, which no longer parses.
|
|
7333
|
+
it.each([
|
|
7334
|
+
`namespace A.B {
|
|
7335
|
+
export const value = 1;
|
|
7336
|
+
}`,
|
|
7337
|
+
`declare namespace A.B.C {
|
|
7338
|
+
const value: number;
|
|
7339
|
+
}`,
|
|
7340
|
+
'export namespace A.B {}',
|
|
7341
|
+
'export declare namespace A.B {}',
|
|
7342
|
+
'module A.B {}',
|
|
7343
|
+
'namespace A./* between */ B {}',
|
|
7344
|
+
])('keeps dotted namespace names: %s', async (source) => {
|
|
7345
|
+
await expectUnchanged(source);
|
|
7346
|
+
});
|
|
7347
|
+
|
|
7348
|
+
it('formats the body of a dotted namespace', async () => {
|
|
7349
|
+
const result = await format('namespace A.B { export const value = 1; }');
|
|
7350
|
+
expect(result).toBeWithNewline(`namespace A.B {
|
|
7351
|
+
export const value = 1;
|
|
7352
|
+
}`);
|
|
7353
|
+
});
|
|
7354
|
+
|
|
7355
|
+
it('keeps the semicolon on shorthand ambient modules', async () => {
|
|
7356
|
+
await expectUnchanged(`declare module "untyped-a";
|
|
7357
|
+
declare module "untyped-b";`);
|
|
7358
|
+
const result = await format(`declare module "untyped";`, { semi: false });
|
|
7359
|
+
expect(result).toBeWithNewline(`declare module "untyped"`);
|
|
7360
|
+
});
|
|
7361
|
+
});
|
|
7362
|
+
|
|
7363
|
+
// Export clauses decide what a module exposes. Printing `export {};` as a
|
|
7364
|
+
// bare `export` made the next declaration public, and a dropped
|
|
7365
|
+
// `export * from` or import attribute changed what the module loads.
|
|
7366
|
+
describe('export clauses survive formatting', () => {
|
|
7367
|
+
/**
|
|
7368
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7369
|
+
* @param {string} source
|
|
7370
|
+
*/
|
|
7371
|
+
const expectUnchanged = async (source) => {
|
|
7372
|
+
const result = await format(source);
|
|
7373
|
+
expect(result).toBeWithNewline(source);
|
|
7374
|
+
};
|
|
7375
|
+
|
|
7376
|
+
it.each([
|
|
7377
|
+
'export {};',
|
|
7378
|
+
'export type {};',
|
|
7379
|
+
'export {} from "./side-effect";',
|
|
7380
|
+
'export type {} from "./types";',
|
|
7381
|
+
'export * from "./module";',
|
|
7382
|
+
'export * as ns from "./module";',
|
|
7383
|
+
'export type * from "./types";',
|
|
7384
|
+
'export type * as Types from "./types";',
|
|
7385
|
+
'export { a } from "./data.json" with { type: "json" };',
|
|
7386
|
+
'export * from "./data.json" with { type: "json" };',
|
|
7387
|
+
'export { "a-b" as ab, c as "c-d" } from "./module";',
|
|
7388
|
+
'export { "a-b" } from "./module";',
|
|
7389
|
+
'export * as "a-b" from "./module";',
|
|
7390
|
+
'import { "a-b" as ab } from "./module";',
|
|
7391
|
+
])('keeps %s', async (source) => {
|
|
7392
|
+
await expectUnchanged(source);
|
|
7393
|
+
});
|
|
7394
|
+
|
|
7395
|
+
it('keeps the declaration after an empty export local', async () => {
|
|
7396
|
+
await expectUnchanged(`export {};
|
|
7397
|
+
const internal = 42;`);
|
|
7398
|
+
await expectUnchanged(`export {};
|
|
7399
|
+
declare global {
|
|
7400
|
+
interface Window {
|
|
7401
|
+
value: number;
|
|
7402
|
+
}
|
|
7403
|
+
}`);
|
|
7404
|
+
});
|
|
7405
|
+
|
|
7406
|
+
it('follows quote and semicolon options', async () => {
|
|
7407
|
+
const result = await format(`export {};\nexport {} from "a";\nexport * as ns from "b";`, {
|
|
7408
|
+
singleQuote: true,
|
|
7409
|
+
semi: false,
|
|
7410
|
+
});
|
|
7411
|
+
expect(result).toBeWithNewline(`export {}\nexport {} from 'a'\nexport * as ns from 'b'`);
|
|
7412
|
+
});
|
|
7413
|
+
});
|
|
7414
|
+
|
|
7150
7415
|
// `export default (class Named {})` is an expression: `Named` is bound only
|
|
7151
7416
|
// inside the class body. `export default class Named {}` is a declaration:
|
|
7152
7417
|
// `Named` becomes a module-scoped binding. Dropping the parens swaps one for
|