@tsrx/prettier-plugin 0.3.115 → 0.3.117
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 +467 -22
- package/src/index.test.js +590 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/prettier-plugin",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.117",
|
|
4
4
|
"description": "Ripple 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.1.
|
|
30
|
+
"@tsrx/core": "0.1.55"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"src/"
|
package/src/index.js
CHANGED
|
@@ -16,6 +16,12 @@
|
|
|
16
16
|
|
|
17
17
|
/** @typedef {Partial<Pick<ParserOptions, 'singleQuote' | 'jsxSingleQuote' | 'semi' | 'trailingComma' | 'useTabs' | 'tabWidth' | 'singleAttributePerLine' | 'bracketSameLine' | 'bracketSpacing' | 'arrowParens' | 'originalText' | 'printWidth'>> & { locStart: (node: AST.NodeWithLocation) => number, locEnd: (node: AST.NodeWithLocation) => number }} RippleFormatOptions */
|
|
18
18
|
|
|
19
|
+
/**
|
|
20
|
+
* Any node the parser may hang decorators off. The individual node types do not
|
|
21
|
+
* declare the property, so reading it needs a cast.
|
|
22
|
+
* @typedef {AST.Node & { decorators?: AST.Decorator[] }} MaybeDecoratedNode
|
|
23
|
+
*/
|
|
24
|
+
|
|
19
25
|
/** @typedef {{ isInAttribute?: boolean, isInArray?: boolean, allowInlineObject?: boolean, isConditionalTest?: boolean, isNestedConditional?: boolean, suppressLeadingComments?: boolean, suppressExpressionLeadingComments?: boolean, suppressOwnParens?: boolean, isInlineContext?: boolean, isStatement?: boolean, isLogicalAndOr?: boolean, allowShorthandProperty?: boolean, isFirstChild?: boolean, noBreakInside?: boolean, expandLastArg?: boolean, preferInlineSimpleUnionType?: boolean }} PrintArgs */
|
|
20
26
|
|
|
21
27
|
import { parseModule } from '@tsrx/core';
|
|
@@ -821,9 +827,9 @@ function buildInlineArrayCommentDoc(comments) {
|
|
|
821
827
|
}
|
|
822
828
|
|
|
823
829
|
/**
|
|
824
|
-
* Print an object or
|
|
825
|
-
* @param {AST.Property | AST.MethodDefinition} node - The property or method node
|
|
826
|
-
* @param {AstPath<AST.Property | AST.MethodDefinition>} path - The AST path
|
|
830
|
+
* Print an object, method, or class field key
|
|
831
|
+
* @param {AST.Property | AST.MethodDefinition | AST.PropertyDefinition} node - The property or method node
|
|
832
|
+
* @param {AstPath<AST.Property | AST.MethodDefinition | AST.PropertyDefinition>} path - The AST path
|
|
827
833
|
* @param {RippleFormatOptions} options - Prettier options
|
|
828
834
|
* @param {PrintFn} print - Print callback
|
|
829
835
|
* @returns {Doc[]}
|
|
@@ -856,6 +862,152 @@ function printKey(node, path, options, print) {
|
|
|
856
862
|
return parts;
|
|
857
863
|
}
|
|
858
864
|
|
|
865
|
+
/**
|
|
866
|
+
* Read a node's decorator list, if it has one.
|
|
867
|
+
* @param {AST.Node | null | undefined} node - The AST node
|
|
868
|
+
* @returns {AST.Decorator[]} The decorators, or an empty array
|
|
869
|
+
*/
|
|
870
|
+
function getDecorators(node) {
|
|
871
|
+
const decorators = /** @type {MaybeDecoratedNode | null | undefined} */ (node)?.decorators;
|
|
872
|
+
return Array.isArray(decorators) ? decorators : [];
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Whether an ancestor prints this node's decorators, so the node must not print
|
|
877
|
+
* them again. Two positions hoist decorators out of the node that owns them:
|
|
878
|
+
*
|
|
879
|
+
* - `@dec export class A {}` and `export @dec class A {}` produce identical
|
|
880
|
+
* ASTs, so the export printer emits them above the `export` keyword.
|
|
881
|
+
* - A parameter property's decorators live on the inner parameter, but belong
|
|
882
|
+
* before the modifiers: `@inject private readonly x: Foo`.
|
|
883
|
+
*
|
|
884
|
+
* A parenthesized default export is not hoisted: its decorators apply to the
|
|
885
|
+
* expression inside the parens, and lifting them above `export default` would
|
|
886
|
+
* both change what they decorate and strand the parens.
|
|
887
|
+
* @param {AST.Node} node - The AST node
|
|
888
|
+
* @param {AstPath} path - The AST path
|
|
889
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
890
|
+
* @returns {boolean}
|
|
891
|
+
*/
|
|
892
|
+
function decoratorsPrintedByParent(node, path, options) {
|
|
893
|
+
const parent = /** @type {AST.Node | null} */ (path.getParentNode());
|
|
894
|
+
|
|
895
|
+
if (!parent) {
|
|
896
|
+
return false;
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
if (parent.type === 'ExportDefaultDeclaration') {
|
|
900
|
+
return parent.declaration === node && !isParenthesizedDefaultExport(parent, options);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
if (parent.type === 'ExportNamedDeclaration') {
|
|
904
|
+
return parent.declaration === node;
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
if (parent.type === 'TSParameterProperty') {
|
|
908
|
+
return /** @type {AST.Node} */ (/** @type {unknown} */ (parent.parameter)) === node;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
return false;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
/**
|
|
915
|
+
* Whether a node is a class member, whose decorators may sit either on their
|
|
916
|
+
* own line or inline with the member.
|
|
917
|
+
* @param {AST.Node} node - The AST node
|
|
918
|
+
* @returns {boolean}
|
|
919
|
+
*/
|
|
920
|
+
function isClassMember(node) {
|
|
921
|
+
return node.type === 'PropertyDefinition' || node.type === 'MethodDefinition';
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
/**
|
|
925
|
+
* Whether a class member's decorators must each go on their own line, which is
|
|
926
|
+
* how they were written when any of them is followed by a newline.
|
|
927
|
+
* @param {AST.Node} node - The decorated node
|
|
928
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
929
|
+
* @returns {boolean}
|
|
930
|
+
*/
|
|
931
|
+
function shouldBreakDecorators(node, options) {
|
|
932
|
+
const text = options.originalText;
|
|
933
|
+
|
|
934
|
+
if (typeof text !== 'string') {
|
|
935
|
+
return false;
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
return getDecorators(node).some(
|
|
939
|
+
(decorator) => typeof decorator.end === 'number' && hasNewline(text, decorator.end),
|
|
940
|
+
);
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
/**
|
|
944
|
+
* Print a decorator list as a prefix for the node it decorates, including the
|
|
945
|
+
* separator that follows the last decorator.
|
|
946
|
+
*
|
|
947
|
+
* Classes always put each decorator on its own line. A class member keeps the
|
|
948
|
+
* lines it was written with, and when it was written inline the decorators get
|
|
949
|
+
* their own group, so a decorator too long to share the member's line moves to
|
|
950
|
+
* its own line rather than breaking apart. Everywhere else — parameters, most
|
|
951
|
+
* notably — decorators stay inline.
|
|
952
|
+
* @param {AST.Node} node - The decorated node
|
|
953
|
+
* @param {AstPath} path - The AST path, positioned at the decorated node
|
|
954
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
955
|
+
* @param {PrintFn} print - Print callback
|
|
956
|
+
* @returns {Doc[]} The prefix parts, or an empty array when there are none
|
|
957
|
+
*/
|
|
958
|
+
function printDecorators(node, path, options, print) {
|
|
959
|
+
if (getDecorators(node).length === 0) {
|
|
960
|
+
return [];
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
const printed = /** @type {Doc[]} */ (path.map(print, 'decorators'));
|
|
964
|
+
const isClass = node.type === 'ClassDeclaration' || node.type === 'ClassExpression';
|
|
965
|
+
|
|
966
|
+
if (isClass || (isClassMember(node) && shouldBreakDecorators(node, options))) {
|
|
967
|
+
return [join(hardline, printed), hardline];
|
|
968
|
+
}
|
|
969
|
+
|
|
970
|
+
if (isClassMember(node)) {
|
|
971
|
+
return [group([join(line, printed), line])];
|
|
972
|
+
}
|
|
973
|
+
|
|
974
|
+
return [join(' ', printed), ' '];
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* Print the decorators of an exported declaration, which belong above the
|
|
979
|
+
* `export` keyword rather than on the declaration itself. A parenthesized
|
|
980
|
+
* default export keeps its decorators inside the parens, so it prints none
|
|
981
|
+
* here — see {@link decoratorsPrintedByParent}.
|
|
982
|
+
* @param {AST.ExportNamedDeclaration | AST.ExportDefaultDeclaration} node - The export node
|
|
983
|
+
* @param {AstPath} path - The AST path
|
|
984
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
985
|
+
* @param {PrintFn} print - Print callback
|
|
986
|
+
* @returns {Doc[]} The prefix parts, or an empty array when there are none
|
|
987
|
+
*/
|
|
988
|
+
function printDeclarationDecorators(node, path, options, print) {
|
|
989
|
+
const declaration = /** @type {AST.Node | null | undefined} */ (node.declaration);
|
|
990
|
+
|
|
991
|
+
if (getDecorators(declaration).length === 0) {
|
|
992
|
+
return [];
|
|
993
|
+
}
|
|
994
|
+
|
|
995
|
+
if (
|
|
996
|
+
node.type === 'ExportDefaultDeclaration' &&
|
|
997
|
+
isParenthesizedDefaultExport(/** @type {AST.ExportDefaultDeclaration} */ (node), options)
|
|
998
|
+
) {
|
|
999
|
+
return [];
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
return /** @type {Doc[]} */ (
|
|
1003
|
+
path.call(
|
|
1004
|
+
(declarationPath) =>
|
|
1005
|
+
printDecorators(/** @type {AST.Node} */ (declaration), declarationPath, options, print),
|
|
1006
|
+
'declaration',
|
|
1007
|
+
)
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
1010
|
+
|
|
859
1011
|
/**
|
|
860
1012
|
* Combine already-printed leading comment parts, a node's printed body, and its
|
|
861
1013
|
* trailing comments into the final Doc returned by {@link printRippleNode}.
|
|
@@ -2146,14 +2298,18 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2146
2298
|
}
|
|
2147
2299
|
|
|
2148
2300
|
case 'TSModuleDeclaration': {
|
|
2149
|
-
|
|
2150
|
-
|
|
2151
|
-
|
|
2152
|
-
' ',
|
|
2153
|
-
|
|
2154
|
-
|
|
2155
|
-
|
|
2156
|
-
|
|
2301
|
+
// `declare global` augments the global scope; printing it as
|
|
2302
|
+
// `declare module global` declares an unrelated module named `global`.
|
|
2303
|
+
nodeContent = node.global
|
|
2304
|
+
? [node.declare ? 'declare ' : '', path.call(print, 'id'), ' ', path.call(print, 'body')]
|
|
2305
|
+
: [
|
|
2306
|
+
node.declare ? 'declare ' : '',
|
|
2307
|
+
node.metadata?.module_keyword ?? 'module',
|
|
2308
|
+
' ',
|
|
2309
|
+
path.call(print, 'id'),
|
|
2310
|
+
' ',
|
|
2311
|
+
path.call(print, 'body'),
|
|
2312
|
+
];
|
|
2157
2313
|
break;
|
|
2158
2314
|
}
|
|
2159
2315
|
|
|
@@ -2560,6 +2716,58 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2560
2716
|
break;
|
|
2561
2717
|
}
|
|
2562
2718
|
|
|
2719
|
+
case 'TSParameterProperty': {
|
|
2720
|
+
// A constructor parameter property declares a class field. Losing
|
|
2721
|
+
// the modifiers loses the field, so this must not fall through to
|
|
2722
|
+
// the unknown-node path.
|
|
2723
|
+
/** @type {Doc[]} */
|
|
2724
|
+
const parts = [];
|
|
2725
|
+
|
|
2726
|
+
// The parser hangs the decorators off the inner parameter, but they
|
|
2727
|
+
// are written before the modifiers: `@inject private readonly x: T`.
|
|
2728
|
+
const parameter = /** @type {AST.Node} */ (/** @type {unknown} */ (node.parameter));
|
|
2729
|
+
|
|
2730
|
+
if (getDecorators(parameter).length > 0) {
|
|
2731
|
+
parts.push(
|
|
2732
|
+
.../** @type {Doc[]} */ (
|
|
2733
|
+
path.call(
|
|
2734
|
+
(parameterPath) => printDecorators(parameter, parameterPath, options, print),
|
|
2735
|
+
'parameter',
|
|
2736
|
+
)
|
|
2737
|
+
),
|
|
2738
|
+
);
|
|
2739
|
+
}
|
|
2740
|
+
|
|
2741
|
+
if (node.accessibility) {
|
|
2742
|
+
parts.push(node.accessibility, ' ');
|
|
2743
|
+
}
|
|
2744
|
+
|
|
2745
|
+
if (node.override) {
|
|
2746
|
+
parts.push('override ');
|
|
2747
|
+
}
|
|
2748
|
+
|
|
2749
|
+
if (node.readonly) {
|
|
2750
|
+
parts.push('readonly ');
|
|
2751
|
+
}
|
|
2752
|
+
|
|
2753
|
+
parts.push(path.call(print, 'parameter'));
|
|
2754
|
+
nodeContent = parts;
|
|
2755
|
+
break;
|
|
2756
|
+
}
|
|
2757
|
+
|
|
2758
|
+
case 'StaticBlock': {
|
|
2759
|
+
nodeContent =
|
|
2760
|
+
node.body && node.body.length > 0
|
|
2761
|
+
? group([
|
|
2762
|
+
'static {',
|
|
2763
|
+
indent([hardline, join(hardline, path.map(print, 'body'))]),
|
|
2764
|
+
hardline,
|
|
2765
|
+
'}',
|
|
2766
|
+
])
|
|
2767
|
+
: 'static {}';
|
|
2768
|
+
break;
|
|
2769
|
+
}
|
|
2770
|
+
|
|
2563
2771
|
case 'TSExpressionWithTypeArguments': {
|
|
2564
2772
|
/** @type {Doc[]} */
|
|
2565
2773
|
const parts = [];
|
|
@@ -2617,6 +2825,10 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2617
2825
|
break;
|
|
2618
2826
|
}
|
|
2619
2827
|
|
|
2828
|
+
case 'Decorator':
|
|
2829
|
+
nodeContent = ['@', path.call(print, 'expression')];
|
|
2830
|
+
break;
|
|
2831
|
+
|
|
2620
2832
|
default:
|
|
2621
2833
|
// Fallback for unknown node types
|
|
2622
2834
|
console.warn('Unknown node type:', node.type);
|
|
@@ -2624,6 +2836,13 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2624
2836
|
break;
|
|
2625
2837
|
}
|
|
2626
2838
|
|
|
2839
|
+
// Decorators have runtime effects, so every decorated node prints them —
|
|
2840
|
+
// unless an ancestor hoisted them out (see `decoratorsPrintedByParent`).
|
|
2841
|
+
const decorated = /** @type {AST.Node} */ (node);
|
|
2842
|
+
if (getDecorators(decorated).length > 0 && !decoratorsPrintedByParent(decorated, path, options)) {
|
|
2843
|
+
nodeContent = [...printDecorators(decorated, path, options, print), nodeContent];
|
|
2844
|
+
}
|
|
2845
|
+
|
|
2627
2846
|
return finishRippleNode(/** @type {AST.Node} */ (node), parts, nodeContent);
|
|
2628
2847
|
}
|
|
2629
2848
|
|
|
@@ -2743,6 +2962,7 @@ function printExportNamedDeclaration(node, path, options, print) {
|
|
|
2743
2962
|
if (node.declaration) {
|
|
2744
2963
|
/** @type {Doc[]} */
|
|
2745
2964
|
const parts = [];
|
|
2965
|
+
parts.push(...printDeclarationDecorators(node, path, options, print));
|
|
2746
2966
|
parts.push('export ');
|
|
2747
2967
|
parts.push(path.call(print, 'declaration'));
|
|
2748
2968
|
return parts;
|
|
@@ -2812,8 +3032,11 @@ function printVariableDeclaration(node, path, options, print) {
|
|
|
2812
3032
|
const declarations = path.map(print, 'declarations');
|
|
2813
3033
|
const declarationParts = join(', ', declarations);
|
|
2814
3034
|
|
|
3035
|
+
// `declare` makes the binding ambient (no emit) — never a for-loop head
|
|
3036
|
+
const declarePrefix = node.declare ? 'declare ' : '';
|
|
3037
|
+
|
|
2815
3038
|
if (!isForLoopInit) {
|
|
2816
|
-
return [kind, ' ', declarationParts, semi(options)];
|
|
3039
|
+
return [declarePrefix, kind, ' ', declarationParts, semi(options)];
|
|
2817
3040
|
}
|
|
2818
3041
|
|
|
2819
3042
|
return [kind, ' ', declarationParts];
|
|
@@ -2974,6 +3197,122 @@ function isTemplateExpression(node) {
|
|
|
2974
3197
|
return node.type === 'JSXElement' || node.type === 'JSXFragment';
|
|
2975
3198
|
}
|
|
2976
3199
|
|
|
3200
|
+
/**
|
|
3201
|
+
* Remove line and block comments from a span of source text.
|
|
3202
|
+
* @param {string} text - Source text
|
|
3203
|
+
* @returns {string} - The text with every comment replaced by nothing
|
|
3204
|
+
*/
|
|
3205
|
+
function stripComments(text) {
|
|
3206
|
+
let result = '';
|
|
3207
|
+
|
|
3208
|
+
for (let i = 0; i < text.length; i++) {
|
|
3209
|
+
if (text.charAt(i) === '/' && text.charAt(i + 1) === '*') {
|
|
3210
|
+
const close = text.indexOf('*/', i + 2);
|
|
3211
|
+
i = close === -1 ? text.length : close + 1;
|
|
3212
|
+
continue;
|
|
3213
|
+
}
|
|
3214
|
+
if (text.charAt(i) === '/' && text.charAt(i + 1) === '/') {
|
|
3215
|
+
while (i < text.length && !isCharNewLine(text.charAt(i))) {
|
|
3216
|
+
i++;
|
|
3217
|
+
}
|
|
3218
|
+
continue;
|
|
3219
|
+
}
|
|
3220
|
+
result += text.charAt(i);
|
|
3221
|
+
}
|
|
3222
|
+
|
|
3223
|
+
return result;
|
|
3224
|
+
}
|
|
3225
|
+
|
|
3226
|
+
/**
|
|
3227
|
+
* Check whether `export default` wraps its declaration in parentheses.
|
|
3228
|
+
*
|
|
3229
|
+
* The parens are load-bearing for a named class or function expression:
|
|
3230
|
+
* `export default (class Named {})` binds `Named` only inside the class body,
|
|
3231
|
+
* while `export default class Named {}` binds it module-wide. Dropping them
|
|
3232
|
+
* silently turns the expression into a declaration.
|
|
3233
|
+
*
|
|
3234
|
+
* The node type alone cannot tell the two apart. Node spans exclude the
|
|
3235
|
+
* parens, and a decorated `export default @dec class Named {}` also parses as
|
|
3236
|
+
* a ClassExpression even though it is a declaration, so the source text
|
|
3237
|
+
* between the keyword and the declaration has to be consulted.
|
|
3238
|
+
*
|
|
3239
|
+
* @param {AST.TSRXExportDefaultDeclaration} node - The export default node
|
|
3240
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
3241
|
+
* @returns {boolean}
|
|
3242
|
+
*/
|
|
3243
|
+
function isParenthesizedDefaultExport(node, options) {
|
|
3244
|
+
const declaration = node.declaration;
|
|
3245
|
+
|
|
3246
|
+
if (
|
|
3247
|
+
!declaration ||
|
|
3248
|
+
(declaration.type !== 'ClassExpression' && declaration.type !== 'FunctionExpression')
|
|
3249
|
+
) {
|
|
3250
|
+
return false;
|
|
3251
|
+
}
|
|
3252
|
+
|
|
3253
|
+
const text = options.originalText;
|
|
3254
|
+
|
|
3255
|
+
if (typeof text !== 'string') {
|
|
3256
|
+
return false;
|
|
3257
|
+
}
|
|
3258
|
+
|
|
3259
|
+
const start = options.locStart(/** @type {AST.NodeWithLocation} */ (node));
|
|
3260
|
+
const end = options.locStart(/** @type {AST.NodeWithLocation} */ (declaration));
|
|
3261
|
+
|
|
3262
|
+
if (!(start < end)) {
|
|
3263
|
+
return false;
|
|
3264
|
+
}
|
|
3265
|
+
|
|
3266
|
+
// Only the keyword, whitespace, comments and opening parens can appear
|
|
3267
|
+
// here, so a trailing `(` after stripping comments means the declaration
|
|
3268
|
+
// was parenthesized.
|
|
3269
|
+
return stripComments(text.slice(start, end)).trimEnd().endsWith('(');
|
|
3270
|
+
}
|
|
3271
|
+
|
|
3272
|
+
/**
|
|
3273
|
+
* Whether an `export default` takes a statement terminator.
|
|
3274
|
+
*
|
|
3275
|
+
* The grammar admits two shapes after the keyword. A HoistableDeclaration,
|
|
3276
|
+
* ClassDeclaration or TypeScript declaration form is a *declaration* and ends
|
|
3277
|
+
* at its closing brace. Everything else is an AssignmentExpression, which is a
|
|
3278
|
+
* *statement* and needs a `;`.
|
|
3279
|
+
*
|
|
3280
|
+
* Omitting it is an ASI hazard rather than a cosmetic slip: the next line is
|
|
3281
|
+
* swallowed into the exported expression whenever it starts with `(`, `[`,
|
|
3282
|
+
* a template literal, `+`, `-`, or `/`. `export default foo;` followed by
|
|
3283
|
+
* `(function () {})();` silently becomes the single call
|
|
3284
|
+
* `export default foo(function () {})();`.
|
|
3285
|
+
*
|
|
3286
|
+
* The node type alone cannot decide this. A decorated
|
|
3287
|
+
* `export default @dec class Named {}` parses as a ClassExpression but is
|
|
3288
|
+
* still a declaration, so the two expression node types defer to
|
|
3289
|
+
* {@link isParenthesizedDefaultExport}, which reads the source.
|
|
3290
|
+
*
|
|
3291
|
+
* @param {AST.TSRXExportDefaultDeclaration} node - The export default node
|
|
3292
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
3293
|
+
* @returns {boolean}
|
|
3294
|
+
*/
|
|
3295
|
+
function defaultExportNeedsSemicolon(node, options) {
|
|
3296
|
+
const declaration = node.declaration;
|
|
3297
|
+
|
|
3298
|
+
if (!declaration) {
|
|
3299
|
+
return false;
|
|
3300
|
+
}
|
|
3301
|
+
|
|
3302
|
+
switch (declaration.type) {
|
|
3303
|
+
case 'FunctionDeclaration':
|
|
3304
|
+
case 'ClassDeclaration':
|
|
3305
|
+
case 'TSDeclareFunction':
|
|
3306
|
+
case 'TSInterfaceDeclaration':
|
|
3307
|
+
return false;
|
|
3308
|
+
case 'ClassExpression':
|
|
3309
|
+
case 'FunctionExpression':
|
|
3310
|
+
return isParenthesizedDefaultExport(node, options);
|
|
3311
|
+
default:
|
|
3312
|
+
return true;
|
|
3313
|
+
}
|
|
3314
|
+
}
|
|
3315
|
+
|
|
2977
3316
|
/**
|
|
2978
3317
|
* Print an export default declaration
|
|
2979
3318
|
* @param {AST.ExportDefaultDeclaration} node - The export default node
|
|
@@ -2985,8 +3324,29 @@ function isTemplateExpression(node) {
|
|
|
2985
3324
|
function printExportDefaultDeclaration(node, path, options, print) {
|
|
2986
3325
|
/** @type {Doc[]} */
|
|
2987
3326
|
const parts = [];
|
|
3327
|
+
parts.push(...printDeclarationDecorators(node, path, options, print));
|
|
2988
3328
|
parts.push('export default ');
|
|
2989
|
-
|
|
3329
|
+
|
|
3330
|
+
// Declaration forms end at their closing brace; expression forms are
|
|
3331
|
+
// statements and terminate.
|
|
3332
|
+
const terminator = defaultExportNeedsSemicolon(node, options) ? semi(options) : '';
|
|
3333
|
+
|
|
3334
|
+
if (isParenthesizedDefaultExport(node, options)) {
|
|
3335
|
+
// An expression export, not a declaration: it keeps its parens.
|
|
3336
|
+
const declaration = path.call(print, 'declaration');
|
|
3337
|
+
|
|
3338
|
+
if (getDecorators(/** @type {AST.Node} */ (node.declaration)).length > 0) {
|
|
3339
|
+
// The decorators stay inside the parens, each on its own line, so
|
|
3340
|
+
// the whole expression is indented to keep them off column zero.
|
|
3341
|
+
parts.push('(', indent([hardline, declaration]), hardline, ')', terminator);
|
|
3342
|
+
return parts;
|
|
3343
|
+
}
|
|
3344
|
+
|
|
3345
|
+
parts.push('(', declaration, ')', terminator);
|
|
3346
|
+
return parts;
|
|
3347
|
+
}
|
|
3348
|
+
|
|
3349
|
+
parts.push(path.call(print, 'declaration'), terminator);
|
|
2990
3350
|
return parts;
|
|
2991
3351
|
}
|
|
2992
3352
|
|
|
@@ -3539,7 +3899,13 @@ function printFunctionDeclaration(node, path, options, print) {
|
|
|
3539
3899
|
}
|
|
3540
3900
|
|
|
3541
3901
|
parts.push(' ');
|
|
3542
|
-
|
|
3902
|
+
|
|
3903
|
+
// `export default function () {}` is an anonymous FunctionDeclaration, the
|
|
3904
|
+
// one position where the name is optional. The space stays either way, so
|
|
3905
|
+
// the parameter list is not glued to the keyword.
|
|
3906
|
+
if (node.id) {
|
|
3907
|
+
parts.push(node.id.name);
|
|
3908
|
+
}
|
|
3543
3909
|
|
|
3544
3910
|
// Add TypeScript generics if present
|
|
3545
3911
|
if (node.typeParameters) {
|
|
@@ -4017,6 +4383,16 @@ function printObjectExpression(node, path, options, print, args) {
|
|
|
4017
4383
|
function printClassDeclaration(node, path, options, print) {
|
|
4018
4384
|
/** @type {Doc[]} */
|
|
4019
4385
|
const parts = [];
|
|
4386
|
+
|
|
4387
|
+
// Ambient/abstract markers change what the class is, so they must survive
|
|
4388
|
+
if (node.declare) {
|
|
4389
|
+
parts.push('declare ');
|
|
4390
|
+
}
|
|
4391
|
+
|
|
4392
|
+
if (node.abstract) {
|
|
4393
|
+
parts.push('abstract ');
|
|
4394
|
+
}
|
|
4395
|
+
|
|
4020
4396
|
parts.push('class');
|
|
4021
4397
|
|
|
4022
4398
|
// Class name (optional for ClassExpression)
|
|
@@ -4147,6 +4523,11 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4147
4523
|
/** @type {Doc[]} */
|
|
4148
4524
|
const parts = [];
|
|
4149
4525
|
|
|
4526
|
+
// `declare` must lead the modifier list
|
|
4527
|
+
if (node.declare) {
|
|
4528
|
+
parts.push('declare ');
|
|
4529
|
+
}
|
|
4530
|
+
|
|
4150
4531
|
// Access modifiers (public, private, protected)
|
|
4151
4532
|
if (node.accessibility) {
|
|
4152
4533
|
parts.push(node.accessibility);
|
|
@@ -4158,13 +4539,28 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4158
4539
|
parts.push('static ');
|
|
4159
4540
|
}
|
|
4160
4541
|
|
|
4542
|
+
// Abstract / override keywords
|
|
4543
|
+
if (node.abstract) {
|
|
4544
|
+
parts.push('abstract ');
|
|
4545
|
+
}
|
|
4546
|
+
|
|
4547
|
+
if (node.override) {
|
|
4548
|
+
parts.push('override ');
|
|
4549
|
+
}
|
|
4550
|
+
|
|
4161
4551
|
// Readonly keyword
|
|
4162
4552
|
if (node.readonly) {
|
|
4163
4553
|
parts.push('readonly ');
|
|
4164
4554
|
}
|
|
4165
4555
|
|
|
4166
|
-
//
|
|
4167
|
-
|
|
4556
|
+
// `accessor` turns the field into a getter/setter pair — not decoration
|
|
4557
|
+
if (node.accessor) {
|
|
4558
|
+
parts.push('accessor ');
|
|
4559
|
+
}
|
|
4560
|
+
|
|
4561
|
+
// Property name. Must go through printKey so a computed key keeps its
|
|
4562
|
+
// brackets — `[key] = 1` and `key = 1` name different fields.
|
|
4563
|
+
parts.push(...printKey(node, path, options, print));
|
|
4168
4564
|
|
|
4169
4565
|
// Optional marker
|
|
4170
4566
|
if (node.optional) {
|
|
@@ -4211,6 +4607,15 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4211
4607
|
parts.push('static ');
|
|
4212
4608
|
}
|
|
4213
4609
|
|
|
4610
|
+
// Abstract / override keywords
|
|
4611
|
+
if (node.abstract) {
|
|
4612
|
+
parts.push('abstract ');
|
|
4613
|
+
}
|
|
4614
|
+
|
|
4615
|
+
if (node.override) {
|
|
4616
|
+
parts.push('override ');
|
|
4617
|
+
}
|
|
4618
|
+
|
|
4214
4619
|
// Method kind and name
|
|
4215
4620
|
if (node.kind === 'constructor') {
|
|
4216
4621
|
// skip as it's covered by the key
|
|
@@ -4258,12 +4663,14 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4258
4663
|
parts.push(': ', path.call(print, 'value', 'returnType'));
|
|
4259
4664
|
}
|
|
4260
4665
|
|
|
4261
|
-
// Method body
|
|
4262
|
-
|
|
4666
|
+
// Method body. Bodiless members (abstract, declared, overload signatures)
|
|
4667
|
+
// terminate with a semicolon — inventing an empty body makes an abstract
|
|
4668
|
+
// method concrete.
|
|
4263
4669
|
if (node.value && node.value.body) {
|
|
4670
|
+
parts.push(' ');
|
|
4264
4671
|
parts.push(path.call(print, 'value', 'body'));
|
|
4265
4672
|
} else {
|
|
4266
|
-
parts.push(
|
|
4673
|
+
parts.push(semi(options));
|
|
4267
4674
|
}
|
|
4268
4675
|
|
|
4269
4676
|
return parts;
|
|
@@ -4640,6 +5047,10 @@ function printTSEnumDeclaration(node, path, options, print) {
|
|
|
4640
5047
|
/** @type {Doc[]} */
|
|
4641
5048
|
const parts = [];
|
|
4642
5049
|
|
|
5050
|
+
if (node.declare) {
|
|
5051
|
+
parts.push('declare ');
|
|
5052
|
+
}
|
|
5053
|
+
|
|
4643
5054
|
// Handle 'const enum' vs 'enum'
|
|
4644
5055
|
if (node.const) {
|
|
4645
5056
|
parts.push('const ');
|
|
@@ -5398,6 +5809,12 @@ function printVariableDeclarator(node, path, options, print) {
|
|
|
5398
5809
|
const id = path.call(print, 'id');
|
|
5399
5810
|
const init = path.call(print, 'init');
|
|
5400
5811
|
|
|
5812
|
+
// A decorated class expression leads with its decorators on their own
|
|
5813
|
+
// lines, so the whole thing moves below the `=` to stay indented.
|
|
5814
|
+
if (node.init.type === 'ClassExpression' && getDecorators(node.init).length > 0) {
|
|
5815
|
+
return [id, ' =', indent([hardline, init])];
|
|
5816
|
+
}
|
|
5817
|
+
|
|
5401
5818
|
// For conditional expressions that will break, put them on a new line
|
|
5402
5819
|
if (node.init.type === 'ConditionalExpression') {
|
|
5403
5820
|
// Check if the ternary will break by checking if it has complex branches
|
|
@@ -5576,7 +5993,19 @@ function printTSTypeLiteral(node, path, options, print) {
|
|
|
5576
5993
|
function printTSPropertySignature(node, path, options, print) {
|
|
5577
5994
|
/** @type {Doc[]} */
|
|
5578
5995
|
const parts = [];
|
|
5579
|
-
|
|
5996
|
+
|
|
5997
|
+
// `readonly` is part of the declared type, not decoration — dropping it
|
|
5998
|
+
// silently widens the member to mutable.
|
|
5999
|
+
if (node.readonly) {
|
|
6000
|
+
parts.push('readonly ');
|
|
6001
|
+
}
|
|
6002
|
+
|
|
6003
|
+
// Computed keys keep their brackets — `[Symbol.iterator]` is not `Symbol.iterator`
|
|
6004
|
+
if (node.computed) {
|
|
6005
|
+
parts.push('[', path.call(print, 'key'), ']');
|
|
6006
|
+
} else {
|
|
6007
|
+
parts.push(path.call(print, 'key'));
|
|
6008
|
+
}
|
|
5580
6009
|
|
|
5581
6010
|
if (node.optional) {
|
|
5582
6011
|
parts.push('?');
|
|
@@ -5602,8 +6031,20 @@ function printTSMethodSignature(node, path, options, print) {
|
|
|
5602
6031
|
/** @type {Doc[]} */
|
|
5603
6032
|
const parts = [];
|
|
5604
6033
|
|
|
5605
|
-
//
|
|
5606
|
-
|
|
6034
|
+
// Accessor kind — without it `get x(): number` and `set x(v: number)`
|
|
6035
|
+
// both collapse to `x(...)`, which is a different (and duplicated) member.
|
|
6036
|
+
if (node.kind === 'get') {
|
|
6037
|
+
parts.push('get ');
|
|
6038
|
+
} else if (node.kind === 'set') {
|
|
6039
|
+
parts.push('set ');
|
|
6040
|
+
}
|
|
6041
|
+
|
|
6042
|
+
// Print the method name/key, keeping brackets on computed keys
|
|
6043
|
+
if (node.computed) {
|
|
6044
|
+
parts.push('[', path.call(print, 'key'), ']');
|
|
6045
|
+
} else {
|
|
6046
|
+
parts.push(path.call(print, 'key'));
|
|
6047
|
+
}
|
|
5607
6048
|
|
|
5608
6049
|
// Add optional marker if present
|
|
5609
6050
|
if (node.optional) {
|
|
@@ -5825,6 +6266,10 @@ function printTSIndexSignature(node, path, options, print) {
|
|
|
5825
6266
|
function printTSConstructorType(node, path, options, print) {
|
|
5826
6267
|
/** @type {Doc[]} */
|
|
5827
6268
|
const parts = [];
|
|
6269
|
+
// `abstract new () => T` only accepts abstract constructors
|
|
6270
|
+
if (node.abstract) {
|
|
6271
|
+
parts.push('abstract ');
|
|
6272
|
+
}
|
|
5828
6273
|
parts.push('new ');
|
|
5829
6274
|
parts.push('(');
|
|
5830
6275
|
const hasParameters = Array.isArray(node.parameters) && node.parameters.length > 0;
|
package/src/index.test.js
CHANGED
|
@@ -6972,4 +6972,594 @@ function g() {
|
|
|
6972
6972
|
expect(result).toBeWithNewline(expected);
|
|
6973
6973
|
});
|
|
6974
6974
|
});
|
|
6975
|
+
|
|
6976
|
+
// A formatter may never change what the source declares. Every modifier
|
|
6977
|
+
// below is load-bearing: dropping it silently retypes or redefines the
|
|
6978
|
+
// member, and the result still compiles, so nothing catches it downstream.
|
|
6979
|
+
describe('TypeScript modifiers survive formatting', () => {
|
|
6980
|
+
/**
|
|
6981
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
6982
|
+
* @param {string} source
|
|
6983
|
+
*/
|
|
6984
|
+
const expectUnchanged = async (source) => {
|
|
6985
|
+
const result = await format(source);
|
|
6986
|
+
expect(result).toBeWithNewline(source);
|
|
6987
|
+
};
|
|
6988
|
+
|
|
6989
|
+
it('keeps readonly on interface members', async () => {
|
|
6990
|
+
await expectUnchanged(`export interface BenchRow {
|
|
6991
|
+
readonly id: number;
|
|
6992
|
+
readonly label: string;
|
|
6993
|
+
mutable: string;
|
|
6994
|
+
}`);
|
|
6995
|
+
});
|
|
6996
|
+
|
|
6997
|
+
it('keeps readonly on type literal members', async () => {
|
|
6998
|
+
await expectUnchanged(`type Row = {
|
|
6999
|
+
readonly id: number;
|
|
7000
|
+
readonly label?: string;
|
|
7001
|
+
mutable: string;
|
|
7002
|
+
};`);
|
|
7003
|
+
});
|
|
7004
|
+
|
|
7005
|
+
it('keeps readonly on nested type literal members', async () => {
|
|
7006
|
+
await expectUnchanged(`interface Outer {
|
|
7007
|
+
readonly inner: {
|
|
7008
|
+
readonly deep: number;
|
|
7009
|
+
};
|
|
7010
|
+
}`);
|
|
7011
|
+
});
|
|
7012
|
+
|
|
7013
|
+
it('keeps readonly on index signatures', async () => {
|
|
7014
|
+
await expectUnchanged(`interface Bag {
|
|
7015
|
+
readonly [key: string]: number;
|
|
7016
|
+
}`);
|
|
7017
|
+
});
|
|
7018
|
+
|
|
7019
|
+
it('keeps readonly array and tuple type operators', async () => {
|
|
7020
|
+
await expectUnchanged(`interface Lists {
|
|
7021
|
+
xs: readonly string[];
|
|
7022
|
+
ys: ReadonlyArray<number>;
|
|
7023
|
+
pair: readonly [number, string];
|
|
7024
|
+
}`);
|
|
7025
|
+
});
|
|
7026
|
+
|
|
7027
|
+
it('keeps get and set accessor kinds on method signatures', async () => {
|
|
7028
|
+
await expectUnchanged(`interface Box {
|
|
7029
|
+
get value(): number;
|
|
7030
|
+
set value(next: number);
|
|
7031
|
+
}`);
|
|
7032
|
+
});
|
|
7033
|
+
|
|
7034
|
+
it('keeps class field modifiers', async () => {
|
|
7035
|
+
await expectUnchanged(`class Fields {
|
|
7036
|
+
readonly a = 1;
|
|
7037
|
+
static readonly b = 2;
|
|
7038
|
+
private readonly c = 3;
|
|
7039
|
+
protected d = 4;
|
|
7040
|
+
public e = 5;
|
|
7041
|
+
declare f: number;
|
|
7042
|
+
accessor g = 6;
|
|
7043
|
+
}`);
|
|
7044
|
+
});
|
|
7045
|
+
|
|
7046
|
+
it('keeps abstract on classes and their members', async () => {
|
|
7047
|
+
await expectUnchanged(`abstract class Shape {
|
|
7048
|
+
abstract area(): number;
|
|
7049
|
+
abstract readonly sides: number;
|
|
7050
|
+
protected abstract render(): void;
|
|
7051
|
+
}`);
|
|
7052
|
+
});
|
|
7053
|
+
|
|
7054
|
+
it('keeps override on class members', async () => {
|
|
7055
|
+
await expectUnchanged(`class Derived extends Base {
|
|
7056
|
+
override toString(): string {
|
|
7057
|
+
return "";
|
|
7058
|
+
}
|
|
7059
|
+
override readonly tag: string = "derived";
|
|
7060
|
+
}`);
|
|
7061
|
+
});
|
|
7062
|
+
|
|
7063
|
+
it('keeps modifiers on constructor parameter properties', async () => {
|
|
7064
|
+
await expectUnchanged(`class Point {
|
|
7065
|
+
readonly origin = 0;
|
|
7066
|
+
constructor(private readonly x: number, public y: string) {}
|
|
7067
|
+
}`);
|
|
7068
|
+
});
|
|
7069
|
+
|
|
7070
|
+
it('keeps declare on ambient declarations', async () => {
|
|
7071
|
+
await expectUnchanged(`declare const version: number;
|
|
7072
|
+
declare function init(): void;
|
|
7073
|
+
declare class Ambient {}
|
|
7074
|
+
declare enum Level {
|
|
7075
|
+
A = 1,
|
|
7076
|
+
}`);
|
|
7077
|
+
});
|
|
7078
|
+
|
|
7079
|
+
it('keeps declare global rather than declaring a module named global', async () => {
|
|
7080
|
+
await expectUnchanged(`declare global {
|
|
7081
|
+
interface Window {
|
|
7082
|
+
readonly octane: number;
|
|
7083
|
+
}
|
|
7084
|
+
}`);
|
|
7085
|
+
});
|
|
7086
|
+
|
|
7087
|
+
it('keeps declare module', async () => {
|
|
7088
|
+
await expectUnchanged(`declare module "octane" {
|
|
7089
|
+
const x: number;
|
|
7090
|
+
}`);
|
|
7091
|
+
});
|
|
7092
|
+
|
|
7093
|
+
it('keeps const enum', async () => {
|
|
7094
|
+
await expectUnchanged(`const enum Flags {
|
|
7095
|
+
None = 0,
|
|
7096
|
+
}`);
|
|
7097
|
+
});
|
|
7098
|
+
|
|
7099
|
+
it('keeps abstract on constructor types', async () => {
|
|
7100
|
+
await expectUnchanged(`type Ctor = abstract new () => object;`);
|
|
7101
|
+
});
|
|
7102
|
+
|
|
7103
|
+
it('keeps class static blocks', async () => {
|
|
7104
|
+
await expectUnchanged(`class WithStatic {
|
|
7105
|
+
static {
|
|
7106
|
+
console.log(1);
|
|
7107
|
+
}
|
|
7108
|
+
}`);
|
|
7109
|
+
});
|
|
7110
|
+
|
|
7111
|
+
it('keeps readonly through the mapped type modifier forms', async () => {
|
|
7112
|
+
await expectUnchanged(`type Frozen = { readonly [K in keyof T]: T[K] };`);
|
|
7113
|
+
await expectUnchanged(`type Thawed = { -readonly [K in keyof T]: T[K] };`);
|
|
7114
|
+
});
|
|
7115
|
+
|
|
7116
|
+
it('terminates bodiless class members with a semicolon, not an empty body', async () => {
|
|
7117
|
+
const input = `abstract class Shape {
|
|
7118
|
+
abstract area(): number;
|
|
7119
|
+
}`;
|
|
7120
|
+
|
|
7121
|
+
const result = await format(input);
|
|
7122
|
+
expect(result).not.toContain('{}');
|
|
7123
|
+
});
|
|
7124
|
+
|
|
7125
|
+
it('keeps brackets on computed signature keys', async () => {
|
|
7126
|
+
await expectUnchanged(`interface Iterable {
|
|
7127
|
+
readonly [Symbol.iterator]: () => void;
|
|
7128
|
+
[Symbol.asyncIterator](): void;
|
|
7129
|
+
}`);
|
|
7130
|
+
});
|
|
7131
|
+
|
|
7132
|
+
it('keeps brackets on computed class field keys', async () => {
|
|
7133
|
+
await expectUnchanged(`class Keyed { [key] = 1; readonly [other] = 2; }`);
|
|
7134
|
+
});
|
|
7135
|
+
|
|
7136
|
+
it('normalises readonly onto reformatted interface members', async () => {
|
|
7137
|
+
const input = `interface Row {readonly id:number
|
|
7138
|
+
readonly label : string}`;
|
|
7139
|
+
const expected = `interface Row {
|
|
7140
|
+
readonly id: number;
|
|
7141
|
+
readonly label: string;
|
|
7142
|
+
}`;
|
|
7143
|
+
|
|
7144
|
+
const result = await format(input);
|
|
7145
|
+
expect(result).toBeWithNewline(expected);
|
|
7146
|
+
});
|
|
7147
|
+
});
|
|
7148
|
+
|
|
7149
|
+
// `export default (class Named {})` is an expression: `Named` is bound only
|
|
7150
|
+
// inside the class body. `export default class Named {}` is a declaration:
|
|
7151
|
+
// `Named` becomes a module-scoped binding. Dropping the parens swaps one for
|
|
7152
|
+
// the other, and the file still compiles, so nothing catches it downstream.
|
|
7153
|
+
describe('export default parentheses survive formatting', () => {
|
|
7154
|
+
/**
|
|
7155
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7156
|
+
* @param {string} source
|
|
7157
|
+
*/
|
|
7158
|
+
const expectUnchanged = async (source) => {
|
|
7159
|
+
const result = await format(source);
|
|
7160
|
+
expect(result).toBeWithNewline(source);
|
|
7161
|
+
};
|
|
7162
|
+
|
|
7163
|
+
it('keeps parens around a named class expression', async () => {
|
|
7164
|
+
await expectUnchanged(`export default (class Named {});`);
|
|
7165
|
+
});
|
|
7166
|
+
|
|
7167
|
+
it('keeps parens around an anonymous class expression', async () => {
|
|
7168
|
+
await expectUnchanged(`export default (class {});`);
|
|
7169
|
+
});
|
|
7170
|
+
|
|
7171
|
+
it('keeps parens around a class expression with a superclass', async () => {
|
|
7172
|
+
await expectUnchanged(`export default (class Named extends Base {});`);
|
|
7173
|
+
});
|
|
7174
|
+
|
|
7175
|
+
it('keeps parens around a named function expression', async () => {
|
|
7176
|
+
await expectUnchanged(`export default (function foo() {});`);
|
|
7177
|
+
});
|
|
7178
|
+
|
|
7179
|
+
it('keeps parens around an anonymous function expression', async () => {
|
|
7180
|
+
await expectUnchanged(`export default (function () {});`);
|
|
7181
|
+
});
|
|
7182
|
+
|
|
7183
|
+
it('does not leak the class expression name into module scope', async () => {
|
|
7184
|
+
const input = `export default (class Named {});
|
|
7185
|
+
export const alias = Named;`;
|
|
7186
|
+
|
|
7187
|
+
await expectUnchanged(input);
|
|
7188
|
+
});
|
|
7189
|
+
|
|
7190
|
+
it('collapses redundant parens down to one pair', async () => {
|
|
7191
|
+
const input = `export default ((class Named {}));`;
|
|
7192
|
+
const expected = `export default (class Named {});`;
|
|
7193
|
+
|
|
7194
|
+
const result = await format(input);
|
|
7195
|
+
expect(result).toBeWithNewline(expected);
|
|
7196
|
+
});
|
|
7197
|
+
|
|
7198
|
+
it('collapses a multiline parenthesized class expression', async () => {
|
|
7199
|
+
const input = `export default (
|
|
7200
|
+
class Named {}
|
|
7201
|
+
);`;
|
|
7202
|
+
const expected = `export default (class Named {});`;
|
|
7203
|
+
|
|
7204
|
+
const result = await format(input);
|
|
7205
|
+
expect(result).toBeWithNewline(expected);
|
|
7206
|
+
});
|
|
7207
|
+
|
|
7208
|
+
it('omits the terminator when semi is disabled', async () => {
|
|
7209
|
+
const input = `export default (class Named {});`;
|
|
7210
|
+
const expected = `export default (class Named {})`;
|
|
7211
|
+
|
|
7212
|
+
const result = await format(input, { semi: false });
|
|
7213
|
+
expect(result).toBeWithNewline(expected);
|
|
7214
|
+
});
|
|
7215
|
+
|
|
7216
|
+
it('leaves an unparenthesized class declaration alone', async () => {
|
|
7217
|
+
await expectUnchanged(`export default class Named {}`);
|
|
7218
|
+
});
|
|
7219
|
+
|
|
7220
|
+
it('leaves an unparenthesized function declaration alone', async () => {
|
|
7221
|
+
await expectUnchanged(`export default function foo() {}`);
|
|
7222
|
+
});
|
|
7223
|
+
|
|
7224
|
+
it('does not invent parens around other default exports', async () => {
|
|
7225
|
+
const input = `export default (0);`;
|
|
7226
|
+
const expected = `export default 0;`;
|
|
7227
|
+
|
|
7228
|
+
const result = await format(input);
|
|
7229
|
+
expect(result).toBeWithNewline(expected);
|
|
7230
|
+
});
|
|
7231
|
+
});
|
|
7232
|
+
|
|
7233
|
+
// `export default <expression>` is a statement and terminates; only the
|
|
7234
|
+
// declaration forms end at their closing brace. Dropping the `;` lets ASI
|
|
7235
|
+
// pull the next line into the exported expression.
|
|
7236
|
+
describe('export default terminators', () => {
|
|
7237
|
+
/**
|
|
7238
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7239
|
+
* @param {string} source
|
|
7240
|
+
*/
|
|
7241
|
+
const expectUnchanged = async (source) => {
|
|
7242
|
+
const result = await format(source);
|
|
7243
|
+
expect(result).toBeWithNewline(source);
|
|
7244
|
+
};
|
|
7245
|
+
|
|
7246
|
+
it('terminates an identifier export', async () => {
|
|
7247
|
+
await expectUnchanged(`export default foo;`);
|
|
7248
|
+
});
|
|
7249
|
+
|
|
7250
|
+
it('terminates an object export', async () => {
|
|
7251
|
+
await expectUnchanged(`export default { a: 1 };`);
|
|
7252
|
+
});
|
|
7253
|
+
|
|
7254
|
+
it('terminates an array export', async () => {
|
|
7255
|
+
await expectUnchanged(`export default [1, 2];`);
|
|
7256
|
+
});
|
|
7257
|
+
|
|
7258
|
+
it('terminates a numeric literal export', async () => {
|
|
7259
|
+
await expectUnchanged(`export default 42;`);
|
|
7260
|
+
});
|
|
7261
|
+
|
|
7262
|
+
it('terminates an arrow function export', async () => {
|
|
7263
|
+
await expectUnchanged(`export default (a, b) => a + b;`);
|
|
7264
|
+
});
|
|
7265
|
+
|
|
7266
|
+
it('terminates a call expression export', async () => {
|
|
7267
|
+
await expectUnchanged(`export default createStore();`);
|
|
7268
|
+
});
|
|
7269
|
+
|
|
7270
|
+
it('terminates an `as` expression export', async () => {
|
|
7271
|
+
await expectUnchanged(`export default foo as Bar;`);
|
|
7272
|
+
});
|
|
7273
|
+
|
|
7274
|
+
// Without the terminator these two lines reparse as the single call
|
|
7275
|
+
// `export default foo(function () {})();`.
|
|
7276
|
+
it('does not let a following paren line join the exported expression', async () => {
|
|
7277
|
+
await expectUnchanged(`export default foo;
|
|
7278
|
+
(function () {})();`);
|
|
7279
|
+
});
|
|
7280
|
+
|
|
7281
|
+
it('does not let a following bracket line join the exported expression', async () => {
|
|
7282
|
+
await expectUnchanged(`export default foo;
|
|
7283
|
+
[1, 2].forEach(log);`);
|
|
7284
|
+
});
|
|
7285
|
+
|
|
7286
|
+
it('does not let a following template line join the exported expression', async () => {
|
|
7287
|
+
await expectUnchanged('export default foo;\n`side effect`;');
|
|
7288
|
+
});
|
|
7289
|
+
|
|
7290
|
+
it('omits the terminator when semi is disabled', async () => {
|
|
7291
|
+
const input = `export default foo;`;
|
|
7292
|
+
const expected = `export default foo`;
|
|
7293
|
+
|
|
7294
|
+
const result = await format(input, { semi: false });
|
|
7295
|
+
expect(result).toBeWithNewline(expected);
|
|
7296
|
+
});
|
|
7297
|
+
|
|
7298
|
+
it('leaves a class declaration unterminated', async () => {
|
|
7299
|
+
await expectUnchanged(`export default class Named {}`);
|
|
7300
|
+
});
|
|
7301
|
+
|
|
7302
|
+
it('leaves an abstract class declaration unterminated', async () => {
|
|
7303
|
+
await expectUnchanged(`export default abstract class A {}`);
|
|
7304
|
+
});
|
|
7305
|
+
|
|
7306
|
+
it('leaves a function declaration unterminated', async () => {
|
|
7307
|
+
await expectUnchanged(`export default function foo() {}`);
|
|
7308
|
+
});
|
|
7309
|
+
|
|
7310
|
+
it('leaves an interface declaration unterminated', async () => {
|
|
7311
|
+
await expectUnchanged(`export default interface Foo {}`);
|
|
7312
|
+
});
|
|
7313
|
+
|
|
7314
|
+
// A decorated default export parses as a ClassExpression but is still a
|
|
7315
|
+
// declaration, so it must not pick up a terminator.
|
|
7316
|
+
it('leaves a decorated class declaration unterminated', async () => {
|
|
7317
|
+
const input = `export default @dec class Named {}`;
|
|
7318
|
+
const expected = `@dec
|
|
7319
|
+
export default class Named {}`;
|
|
7320
|
+
|
|
7321
|
+
const result = await format(input);
|
|
7322
|
+
expect(result).toBeWithNewline(expected);
|
|
7323
|
+
});
|
|
7324
|
+
});
|
|
7325
|
+
|
|
7326
|
+
// `export default function () {}` is the one position where a
|
|
7327
|
+
// FunctionDeclaration may be anonymous.
|
|
7328
|
+
describe('anonymous default-exported function declarations', () => {
|
|
7329
|
+
/**
|
|
7330
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7331
|
+
* @param {string} source
|
|
7332
|
+
*/
|
|
7333
|
+
const expectUnchanged = async (source) => {
|
|
7334
|
+
const result = await format(source);
|
|
7335
|
+
expect(result).toBeWithNewline(source);
|
|
7336
|
+
};
|
|
7337
|
+
|
|
7338
|
+
it('prints an anonymous function declaration', async () => {
|
|
7339
|
+
await expectUnchanged(`export default function () {}`);
|
|
7340
|
+
});
|
|
7341
|
+
|
|
7342
|
+
it('prints an anonymous async function declaration', async () => {
|
|
7343
|
+
await expectUnchanged(`export default async function () {}`);
|
|
7344
|
+
});
|
|
7345
|
+
|
|
7346
|
+
it('prints an anonymous generator declaration', async () => {
|
|
7347
|
+
await expectUnchanged(`export default function* () {}`);
|
|
7348
|
+
});
|
|
7349
|
+
|
|
7350
|
+
it('prints an anonymous function declaration with parameters', async () => {
|
|
7351
|
+
await expectUnchanged(`export default function (a, b) {
|
|
7352
|
+
return a + b;
|
|
7353
|
+
}`);
|
|
7354
|
+
});
|
|
7355
|
+
});
|
|
7356
|
+
|
|
7357
|
+
describe('decorators survive formatting', () => {
|
|
7358
|
+
/**
|
|
7359
|
+
* Assert the input is already formatted and comes back byte-identical.
|
|
7360
|
+
* @param {string} source
|
|
7361
|
+
*/
|
|
7362
|
+
const expectUnchanged = async (source) => {
|
|
7363
|
+
const result = await format(source);
|
|
7364
|
+
expect(result).toBeWithNewline(source);
|
|
7365
|
+
};
|
|
7366
|
+
|
|
7367
|
+
it('keeps decorators in every position', async () => {
|
|
7368
|
+
await expectUnchanged(`@sealed
|
|
7369
|
+
class A {
|
|
7370
|
+
@log
|
|
7371
|
+
method() {}
|
|
7372
|
+
@inject accessor x = 1;
|
|
7373
|
+
m(@param() a: number) {}
|
|
7374
|
+
}`);
|
|
7375
|
+
});
|
|
7376
|
+
|
|
7377
|
+
it('keeps a decorator on a class declaration', async () => {
|
|
7378
|
+
await expectUnchanged(`@sealed
|
|
7379
|
+
class Widget {
|
|
7380
|
+
render() {
|
|
7381
|
+
return 1;
|
|
7382
|
+
}
|
|
7383
|
+
}`);
|
|
7384
|
+
});
|
|
7385
|
+
|
|
7386
|
+
it('gives each class decorator its own line', async () => {
|
|
7387
|
+
const input = `@first @second class Widget {}`;
|
|
7388
|
+
const expected = `@first
|
|
7389
|
+
@second
|
|
7390
|
+
class Widget {}`;
|
|
7391
|
+
|
|
7392
|
+
const result = await format(input);
|
|
7393
|
+
expect(result).toBeWithNewline(expected);
|
|
7394
|
+
});
|
|
7395
|
+
|
|
7396
|
+
it('keeps a class member decorator on its own line', async () => {
|
|
7397
|
+
await expectUnchanged(`class Store {
|
|
7398
|
+
@observable
|
|
7399
|
+
count = 0;
|
|
7400
|
+
|
|
7401
|
+
@action
|
|
7402
|
+
increment() {
|
|
7403
|
+
this.count++;
|
|
7404
|
+
}
|
|
7405
|
+
}`);
|
|
7406
|
+
});
|
|
7407
|
+
|
|
7408
|
+
it('keeps a class member decorator inline when it was written inline', async () => {
|
|
7409
|
+
await expectUnchanged(`class Store {
|
|
7410
|
+
@observable count = 0;
|
|
7411
|
+
@inject accessor service = null;
|
|
7412
|
+
|
|
7413
|
+
@action increment() {
|
|
7414
|
+
this.count++;
|
|
7415
|
+
}
|
|
7416
|
+
}`);
|
|
7417
|
+
});
|
|
7418
|
+
|
|
7419
|
+
it('keeps several inline decorators on one member', async () => {
|
|
7420
|
+
await expectUnchanged(`class Store {
|
|
7421
|
+
@first @second count = 0;
|
|
7422
|
+
|
|
7423
|
+
ping() {
|
|
7424
|
+
return 1;
|
|
7425
|
+
}
|
|
7426
|
+
}`);
|
|
7427
|
+
});
|
|
7428
|
+
|
|
7429
|
+
it('moves an inline decorator too long for the line onto its own line', async () => {
|
|
7430
|
+
const input = `class Store {
|
|
7431
|
+
@veryLongDecoratorNameHere({ option: 1, another: 2, third: 3, fourth: 4 }) method() {
|
|
7432
|
+
return 1;
|
|
7433
|
+
}
|
|
7434
|
+
}`;
|
|
7435
|
+
const expected = `class Store {
|
|
7436
|
+
@veryLongDecoratorNameHere({ option: 1, another: 2, third: 3, fourth: 4 })
|
|
7437
|
+
method() {
|
|
7438
|
+
return 1;
|
|
7439
|
+
}
|
|
7440
|
+
}`;
|
|
7441
|
+
|
|
7442
|
+
const result = await format(input);
|
|
7443
|
+
expect(result).toBeWithNewline(expected);
|
|
7444
|
+
});
|
|
7445
|
+
|
|
7446
|
+
it('keeps decorators alongside other member modifiers', async () => {
|
|
7447
|
+
await expectUnchanged(`class Store {
|
|
7448
|
+
@dec static base = 1;
|
|
7449
|
+
@dec declare readonly id: number;
|
|
7450
|
+
|
|
7451
|
+
@dec
|
|
7452
|
+
@other
|
|
7453
|
+
private static async *walk() {
|
|
7454
|
+
yield 1;
|
|
7455
|
+
}
|
|
7456
|
+
}`);
|
|
7457
|
+
});
|
|
7458
|
+
|
|
7459
|
+
it('keeps decorators on accessors and computed keys', async () => {
|
|
7460
|
+
await expectUnchanged(`class Store {
|
|
7461
|
+
@dec ["computed"] = 1;
|
|
7462
|
+
|
|
7463
|
+
@dec
|
|
7464
|
+
@other()
|
|
7465
|
+
get value() {
|
|
7466
|
+
return 1;
|
|
7467
|
+
}
|
|
7468
|
+
|
|
7469
|
+
@dec set value(next) {
|
|
7470
|
+
this.inner = next;
|
|
7471
|
+
}
|
|
7472
|
+
}`);
|
|
7473
|
+
});
|
|
7474
|
+
|
|
7475
|
+
it('keeps decorators built from member and call expressions', async () => {
|
|
7476
|
+
await expectUnchanged(`@dec.nested.deep({ a: 1 })
|
|
7477
|
+
class Widget {}`);
|
|
7478
|
+
});
|
|
7479
|
+
|
|
7480
|
+
it('keeps parameter decorators inline', async () => {
|
|
7481
|
+
await expectUnchanged(`class Store {
|
|
7482
|
+
handle(@inject() service: Service, @body() payload: Payload) {
|
|
7483
|
+
return service;
|
|
7484
|
+
}
|
|
7485
|
+
}`);
|
|
7486
|
+
});
|
|
7487
|
+
|
|
7488
|
+
it('keeps parameter decorators before parameter property modifiers', async () => {
|
|
7489
|
+
await expectUnchanged(`class Store {
|
|
7490
|
+
constructor(@inject private readonly service: Service) {
|
|
7491
|
+
this.ready = true;
|
|
7492
|
+
}
|
|
7493
|
+
}`);
|
|
7494
|
+
});
|
|
7495
|
+
|
|
7496
|
+
it('keeps decorators above the export keyword', async () => {
|
|
7497
|
+
await expectUnchanged(`@sealed
|
|
7498
|
+
export class Widget {}`);
|
|
7499
|
+
});
|
|
7500
|
+
|
|
7501
|
+
it('hoists decorators written after the export keyword', async () => {
|
|
7502
|
+
const input = `export @sealed class Widget {}`;
|
|
7503
|
+
const expected = `@sealed
|
|
7504
|
+
export class Widget {}`;
|
|
7505
|
+
|
|
7506
|
+
const result = await format(input);
|
|
7507
|
+
expect(result).toBeWithNewline(expected);
|
|
7508
|
+
});
|
|
7509
|
+
|
|
7510
|
+
it('keeps decorators on a default-exported class', async () => {
|
|
7511
|
+
await expectUnchanged(`@sealed
|
|
7512
|
+
export default class Widget {}`);
|
|
7513
|
+
});
|
|
7514
|
+
|
|
7515
|
+
// Hoisting these above `export default` would change what they decorate
|
|
7516
|
+
// and strand the parens, which a second pass then drops — turning the
|
|
7517
|
+
// class expression into a declaration and leaking its name into module
|
|
7518
|
+
// scope.
|
|
7519
|
+
it('keeps decorators inside a parenthesized default export', async () => {
|
|
7520
|
+
await expectUnchanged(`export default (
|
|
7521
|
+
@sealed
|
|
7522
|
+
class Named {}
|
|
7523
|
+
);`);
|
|
7524
|
+
});
|
|
7525
|
+
|
|
7526
|
+
it('does not hoist decorators out of a parenthesized default export', async () => {
|
|
7527
|
+
const input = `export default (@sealed class Named {});`;
|
|
7528
|
+
const expected = `export default (
|
|
7529
|
+
@sealed
|
|
7530
|
+
class Named {}
|
|
7531
|
+
);`;
|
|
7532
|
+
|
|
7533
|
+
const result = await format(input);
|
|
7534
|
+
expect(result).toBeWithNewline(expected);
|
|
7535
|
+
});
|
|
7536
|
+
|
|
7537
|
+
it('keeps decorators on a class expression', async () => {
|
|
7538
|
+
await expectUnchanged(`const Widget =
|
|
7539
|
+
@sealed
|
|
7540
|
+
class {};`);
|
|
7541
|
+
});
|
|
7542
|
+
|
|
7543
|
+
it('keeps decorators alongside leading comments', async () => {
|
|
7544
|
+
await expectUnchanged(`// widget entry point
|
|
7545
|
+
@sealed
|
|
7546
|
+
class Widget {
|
|
7547
|
+
// the counter
|
|
7548
|
+
@observable
|
|
7549
|
+
count = 0;
|
|
7550
|
+
|
|
7551
|
+
ping() {
|
|
7552
|
+
return 1;
|
|
7553
|
+
}
|
|
7554
|
+
}`);
|
|
7555
|
+
});
|
|
7556
|
+
|
|
7557
|
+
it('keeps blank lines between decorated classes', async () => {
|
|
7558
|
+
await expectUnchanged(`@first
|
|
7559
|
+
class A {}
|
|
7560
|
+
|
|
7561
|
+
@second
|
|
7562
|
+
class B {}`);
|
|
7563
|
+
});
|
|
7564
|
+
});
|
|
6975
7565
|
});
|