@tsrx/prettier-plugin 0.4.7 → 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 +179 -53
- package/src/index.test.js +225 -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
|
|
|
@@ -2883,7 +2927,7 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2883
2927
|
} else if (spec.type === 'ImportSpecifier') {
|
|
2884
2928
|
// Handle inline type imports: import { type Component } from '@example/runtime'
|
|
2885
2929
|
const typePrefix = spec.importKind === 'type' ? 'type ' : '';
|
|
2886
|
-
const importedName =
|
|
2930
|
+
const importedName = printModuleExportName(spec.imported, options);
|
|
2887
2931
|
const localName = spec.local.name;
|
|
2888
2932
|
const importName =
|
|
2889
2933
|
importedName === localName
|
|
@@ -2927,13 +2971,40 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2927
2971
|
parts.push(' from');
|
|
2928
2972
|
}
|
|
2929
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) {
|
|
2930
3001
|
const source = /** @type {AST.Literal | AST.Identifier} */ (/** @type {unknown} */ (node.source));
|
|
2931
|
-
|
|
3002
|
+
/** @type {Doc[]} */
|
|
3003
|
+
const parts = [
|
|
2932
3004
|
source.type === 'Identifier'
|
|
2933
3005
|
? source.name
|
|
2934
|
-
: formatStringLiteral(/** @type {string} */ (source.value), options)
|
|
2935
|
-
|
|
2936
|
-
parts.push(' ', sourceDoc);
|
|
3006
|
+
: formatStringLiteral(/** @type {string} */ (source.value), options),
|
|
3007
|
+
];
|
|
2937
3008
|
|
|
2938
3009
|
const attributes =
|
|
2939
3010
|
/** @type {Array<{ key: AST.Identifier | AST.Literal, value: AST.Literal }>} */ (
|
|
@@ -2951,18 +3022,39 @@ function printImportDeclaration(node, path, options, _print) {
|
|
|
2951
3022
|
parts.push(' with { ', join(', ', attributeDocs), ' }');
|
|
2952
3023
|
}
|
|
2953
3024
|
|
|
2954
|
-
parts.push(semi(options));
|
|
2955
|
-
|
|
2956
3025
|
return parts;
|
|
2957
3026
|
}
|
|
2958
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
|
+
|
|
2959
3051
|
/**
|
|
2960
3052
|
* Print an export named declaration
|
|
2961
3053
|
* @param {AST.ExportNamedDeclaration} node - The export declaration node
|
|
2962
3054
|
* @param {AstPath<AST.ExportNamedDeclaration>} path - The AST path
|
|
2963
3055
|
* @param {TsrxFormatOptions} options - Prettier options
|
|
2964
3056
|
* @param {PrintFn} print - Print callback
|
|
2965
|
-
* @returns {Doc[]
|
|
3057
|
+
* @returns {Doc[]}
|
|
2966
3058
|
*/
|
|
2967
3059
|
function printExportNamedDeclaration(node, path, options, print) {
|
|
2968
3060
|
if (node.declaration) {
|
|
@@ -2972,42 +3064,52 @@ function printExportNamedDeclaration(node, path, options, print) {
|
|
|
2972
3064
|
parts.push('export ');
|
|
2973
3065
|
parts.push(path.call(print, 'declaration'));
|
|
2974
3066
|
return parts;
|
|
2975
|
-
}
|
|
2976
|
-
const specifiers = node.specifiers.map((spec) => {
|
|
2977
|
-
const typePrefix = spec.exportKind === 'type' ? 'type ' : '';
|
|
2978
|
-
const exportedName = /** @type {AST.Identifier} */ (spec.exported).name;
|
|
2979
|
-
const localName = /** @type {AST.Identifier} */ (spec.local).name;
|
|
2980
|
-
if (exportedName === localName) {
|
|
2981
|
-
return typePrefix + localName;
|
|
2982
|
-
} else {
|
|
2983
|
-
return typePrefix + localName + ' as ' + exportedName;
|
|
2984
|
-
}
|
|
2985
|
-
});
|
|
3067
|
+
}
|
|
2986
3068
|
|
|
2987
|
-
|
|
2988
|
-
|
|
2989
|
-
|
|
2990
|
-
|
|
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;
|
|
2991
3077
|
}
|
|
2992
|
-
|
|
3078
|
+
});
|
|
2993
3079
|
|
|
2994
|
-
|
|
2995
|
-
|
|
2996
|
-
|
|
2997
|
-
|
|
2998
|
-
|
|
2999
|
-
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
: formatStringLiteral(/** @type {string} */ (source.value), options),
|
|
3003
|
-
);
|
|
3004
|
-
}
|
|
3005
|
-
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
|
+
];
|
|
3006
3088
|
|
|
3007
|
-
|
|
3089
|
+
if (node.source) {
|
|
3090
|
+
parts.push(' from ', printModuleSource(node, options));
|
|
3008
3091
|
}
|
|
3092
|
+
parts.push(semi(options));
|
|
3093
|
+
|
|
3094
|
+
return parts;
|
|
3095
|
+
}
|
|
3009
3096
|
|
|
3010
|
-
|
|
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
|
+
];
|
|
3011
3113
|
}
|
|
3012
3114
|
|
|
3013
3115
|
/**
|
|
@@ -4420,6 +4522,16 @@ function printClassDeclaration(node, path, options, print) {
|
|
|
4420
4522
|
if (node.superClass) {
|
|
4421
4523
|
parts.push(' extends ');
|
|
4422
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')));
|
|
4423
4535
|
}
|
|
4424
4536
|
|
|
4425
4537
|
parts.push(' ');
|
|
@@ -4573,6 +4685,12 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4573
4685
|
parts.push('?');
|
|
4574
4686
|
}
|
|
4575
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
|
+
|
|
4576
4694
|
// Type annotation
|
|
4577
4695
|
if (node.typeAnnotation) {
|
|
4578
4696
|
parts.push(': ');
|
|
@@ -4643,6 +4761,11 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4643
4761
|
// the key is 'constructor' and we already handled that above
|
|
4644
4762
|
parts.push(...printKey(node, path, options, print));
|
|
4645
4763
|
|
|
4764
|
+
// Optional marker — dropping it turns `onMount?()` into a required member
|
|
4765
|
+
if (node.optional) {
|
|
4766
|
+
parts.push('?');
|
|
4767
|
+
}
|
|
4768
|
+
|
|
4646
4769
|
// Add TypeScript generics if present (always on the method node, not on value)
|
|
4647
4770
|
if (node.typeParameters) {
|
|
4648
4771
|
const typeParams = path.call(print, 'typeParameters');
|
|
@@ -6407,7 +6530,10 @@ function printTSImportType(node, path, options, print) {
|
|
|
6407
6530
|
parts.push('.', path.call(print, 'qualifier'));
|
|
6408
6531
|
}
|
|
6409
6532
|
|
|
6410
|
-
|
|
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) {
|
|
6411
6537
|
parts.push(path.call(print, 'typeParameters'));
|
|
6412
6538
|
}
|
|
6413
6539
|
|
package/src/index.test.js
CHANGED
|
@@ -7092,6 +7092,33 @@ function g() {
|
|
|
7092
7092
|
}`);
|
|
7093
7093
|
});
|
|
7094
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
|
+
|
|
7095
7122
|
it('keeps override on class members', async () => {
|
|
7096
7123
|
await expectUnchanged(`class Derived extends Base {
|
|
7097
7124
|
override toString(): string {
|
|
@@ -7187,6 +7214,204 @@ declare enum Level {
|
|
|
7187
7214
|
});
|
|
7188
7215
|
});
|
|
7189
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
|
+
|
|
7190
7415
|
// `export default (class Named {})` is an expression: `Named` is bound only
|
|
7191
7416
|
// inside the class body. `export default class Named {}` is a declaration:
|
|
7192
7417
|
// `Named` becomes a module-scoped binding. Dropping the parens swaps one for
|