@tsrx/prettier-plugin 0.3.116 → 0.3.118
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 +468 -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.118",
|
|
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.56"
|
|
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,19 @@ 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 =
|
|
2304
|
+
node.kind === 'global'
|
|
2305
|
+
? [node.declare ? 'declare ' : '', path.call(print, 'id'), ' ', path.call(print, 'body')]
|
|
2306
|
+
: [
|
|
2307
|
+
node.declare ? 'declare ' : '',
|
|
2308
|
+
node.kind,
|
|
2309
|
+
' ',
|
|
2310
|
+
path.call(print, 'id'),
|
|
2311
|
+
' ',
|
|
2312
|
+
path.call(print, 'body'),
|
|
2313
|
+
];
|
|
2157
2314
|
break;
|
|
2158
2315
|
}
|
|
2159
2316
|
|
|
@@ -2560,6 +2717,58 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2560
2717
|
break;
|
|
2561
2718
|
}
|
|
2562
2719
|
|
|
2720
|
+
case 'TSParameterProperty': {
|
|
2721
|
+
// A constructor parameter property declares a class field. Losing
|
|
2722
|
+
// the modifiers loses the field, so this must not fall through to
|
|
2723
|
+
// the unknown-node path.
|
|
2724
|
+
/** @type {Doc[]} */
|
|
2725
|
+
const parts = [];
|
|
2726
|
+
|
|
2727
|
+
// The parser hangs the decorators off the inner parameter, but they
|
|
2728
|
+
// are written before the modifiers: `@inject private readonly x: T`.
|
|
2729
|
+
const parameter = /** @type {AST.Node} */ (/** @type {unknown} */ (node.parameter));
|
|
2730
|
+
|
|
2731
|
+
if (getDecorators(parameter).length > 0) {
|
|
2732
|
+
parts.push(
|
|
2733
|
+
.../** @type {Doc[]} */ (
|
|
2734
|
+
path.call(
|
|
2735
|
+
(parameterPath) => printDecorators(parameter, parameterPath, options, print),
|
|
2736
|
+
'parameter',
|
|
2737
|
+
)
|
|
2738
|
+
),
|
|
2739
|
+
);
|
|
2740
|
+
}
|
|
2741
|
+
|
|
2742
|
+
if (node.accessibility) {
|
|
2743
|
+
parts.push(node.accessibility, ' ');
|
|
2744
|
+
}
|
|
2745
|
+
|
|
2746
|
+
if (node.override) {
|
|
2747
|
+
parts.push('override ');
|
|
2748
|
+
}
|
|
2749
|
+
|
|
2750
|
+
if (node.readonly) {
|
|
2751
|
+
parts.push('readonly ');
|
|
2752
|
+
}
|
|
2753
|
+
|
|
2754
|
+
parts.push(path.call(print, 'parameter'));
|
|
2755
|
+
nodeContent = parts;
|
|
2756
|
+
break;
|
|
2757
|
+
}
|
|
2758
|
+
|
|
2759
|
+
case 'StaticBlock': {
|
|
2760
|
+
nodeContent =
|
|
2761
|
+
node.body && node.body.length > 0
|
|
2762
|
+
? group([
|
|
2763
|
+
'static {',
|
|
2764
|
+
indent([hardline, join(hardline, path.map(print, 'body'))]),
|
|
2765
|
+
hardline,
|
|
2766
|
+
'}',
|
|
2767
|
+
])
|
|
2768
|
+
: 'static {}';
|
|
2769
|
+
break;
|
|
2770
|
+
}
|
|
2771
|
+
|
|
2563
2772
|
case 'TSExpressionWithTypeArguments': {
|
|
2564
2773
|
/** @type {Doc[]} */
|
|
2565
2774
|
const parts = [];
|
|
@@ -2617,6 +2826,10 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2617
2826
|
break;
|
|
2618
2827
|
}
|
|
2619
2828
|
|
|
2829
|
+
case 'Decorator':
|
|
2830
|
+
nodeContent = ['@', path.call(print, 'expression')];
|
|
2831
|
+
break;
|
|
2832
|
+
|
|
2620
2833
|
default:
|
|
2621
2834
|
// Fallback for unknown node types
|
|
2622
2835
|
console.warn('Unknown node type:', node.type);
|
|
@@ -2624,6 +2837,13 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
2624
2837
|
break;
|
|
2625
2838
|
}
|
|
2626
2839
|
|
|
2840
|
+
// Decorators have runtime effects, so every decorated node prints them —
|
|
2841
|
+
// unless an ancestor hoisted them out (see `decoratorsPrintedByParent`).
|
|
2842
|
+
const decorated = /** @type {AST.Node} */ (node);
|
|
2843
|
+
if (getDecorators(decorated).length > 0 && !decoratorsPrintedByParent(decorated, path, options)) {
|
|
2844
|
+
nodeContent = [...printDecorators(decorated, path, options, print), nodeContent];
|
|
2845
|
+
}
|
|
2846
|
+
|
|
2627
2847
|
return finishRippleNode(/** @type {AST.Node} */ (node), parts, nodeContent);
|
|
2628
2848
|
}
|
|
2629
2849
|
|
|
@@ -2743,6 +2963,7 @@ function printExportNamedDeclaration(node, path, options, print) {
|
|
|
2743
2963
|
if (node.declaration) {
|
|
2744
2964
|
/** @type {Doc[]} */
|
|
2745
2965
|
const parts = [];
|
|
2966
|
+
parts.push(...printDeclarationDecorators(node, path, options, print));
|
|
2746
2967
|
parts.push('export ');
|
|
2747
2968
|
parts.push(path.call(print, 'declaration'));
|
|
2748
2969
|
return parts;
|
|
@@ -2812,8 +3033,11 @@ function printVariableDeclaration(node, path, options, print) {
|
|
|
2812
3033
|
const declarations = path.map(print, 'declarations');
|
|
2813
3034
|
const declarationParts = join(', ', declarations);
|
|
2814
3035
|
|
|
3036
|
+
// `declare` makes the binding ambient (no emit) — never a for-loop head
|
|
3037
|
+
const declarePrefix = node.declare ? 'declare ' : '';
|
|
3038
|
+
|
|
2815
3039
|
if (!isForLoopInit) {
|
|
2816
|
-
return [kind, ' ', declarationParts, semi(options)];
|
|
3040
|
+
return [declarePrefix, kind, ' ', declarationParts, semi(options)];
|
|
2817
3041
|
}
|
|
2818
3042
|
|
|
2819
3043
|
return [kind, ' ', declarationParts];
|
|
@@ -2974,6 +3198,122 @@ function isTemplateExpression(node) {
|
|
|
2974
3198
|
return node.type === 'JSXElement' || node.type === 'JSXFragment';
|
|
2975
3199
|
}
|
|
2976
3200
|
|
|
3201
|
+
/**
|
|
3202
|
+
* Remove line and block comments from a span of source text.
|
|
3203
|
+
* @param {string} text - Source text
|
|
3204
|
+
* @returns {string} - The text with every comment replaced by nothing
|
|
3205
|
+
*/
|
|
3206
|
+
function stripComments(text) {
|
|
3207
|
+
let result = '';
|
|
3208
|
+
|
|
3209
|
+
for (let i = 0; i < text.length; i++) {
|
|
3210
|
+
if (text.charAt(i) === '/' && text.charAt(i + 1) === '*') {
|
|
3211
|
+
const close = text.indexOf('*/', i + 2);
|
|
3212
|
+
i = close === -1 ? text.length : close + 1;
|
|
3213
|
+
continue;
|
|
3214
|
+
}
|
|
3215
|
+
if (text.charAt(i) === '/' && text.charAt(i + 1) === '/') {
|
|
3216
|
+
while (i < text.length && !isCharNewLine(text.charAt(i))) {
|
|
3217
|
+
i++;
|
|
3218
|
+
}
|
|
3219
|
+
continue;
|
|
3220
|
+
}
|
|
3221
|
+
result += text.charAt(i);
|
|
3222
|
+
}
|
|
3223
|
+
|
|
3224
|
+
return result;
|
|
3225
|
+
}
|
|
3226
|
+
|
|
3227
|
+
/**
|
|
3228
|
+
* Check whether `export default` wraps its declaration in parentheses.
|
|
3229
|
+
*
|
|
3230
|
+
* The parens are load-bearing for a named class or function expression:
|
|
3231
|
+
* `export default (class Named {})` binds `Named` only inside the class body,
|
|
3232
|
+
* while `export default class Named {}` binds it module-wide. Dropping them
|
|
3233
|
+
* silently turns the expression into a declaration.
|
|
3234
|
+
*
|
|
3235
|
+
* The node type alone cannot tell the two apart. Node spans exclude the
|
|
3236
|
+
* parens, and a decorated `export default @dec class Named {}` also parses as
|
|
3237
|
+
* a ClassExpression even though it is a declaration, so the source text
|
|
3238
|
+
* between the keyword and the declaration has to be consulted.
|
|
3239
|
+
*
|
|
3240
|
+
* @param {AST.TSRXExportDefaultDeclaration} node - The export default node
|
|
3241
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
3242
|
+
* @returns {boolean}
|
|
3243
|
+
*/
|
|
3244
|
+
function isParenthesizedDefaultExport(node, options) {
|
|
3245
|
+
const declaration = node.declaration;
|
|
3246
|
+
|
|
3247
|
+
if (
|
|
3248
|
+
!declaration ||
|
|
3249
|
+
(declaration.type !== 'ClassExpression' && declaration.type !== 'FunctionExpression')
|
|
3250
|
+
) {
|
|
3251
|
+
return false;
|
|
3252
|
+
}
|
|
3253
|
+
|
|
3254
|
+
const text = options.originalText;
|
|
3255
|
+
|
|
3256
|
+
if (typeof text !== 'string') {
|
|
3257
|
+
return false;
|
|
3258
|
+
}
|
|
3259
|
+
|
|
3260
|
+
const start = options.locStart(/** @type {AST.NodeWithLocation} */ (node));
|
|
3261
|
+
const end = options.locStart(/** @type {AST.NodeWithLocation} */ (declaration));
|
|
3262
|
+
|
|
3263
|
+
if (!(start < end)) {
|
|
3264
|
+
return false;
|
|
3265
|
+
}
|
|
3266
|
+
|
|
3267
|
+
// Only the keyword, whitespace, comments and opening parens can appear
|
|
3268
|
+
// here, so a trailing `(` after stripping comments means the declaration
|
|
3269
|
+
// was parenthesized.
|
|
3270
|
+
return stripComments(text.slice(start, end)).trimEnd().endsWith('(');
|
|
3271
|
+
}
|
|
3272
|
+
|
|
3273
|
+
/**
|
|
3274
|
+
* Whether an `export default` takes a statement terminator.
|
|
3275
|
+
*
|
|
3276
|
+
* The grammar admits two shapes after the keyword. A HoistableDeclaration,
|
|
3277
|
+
* ClassDeclaration or TypeScript declaration form is a *declaration* and ends
|
|
3278
|
+
* at its closing brace. Everything else is an AssignmentExpression, which is a
|
|
3279
|
+
* *statement* and needs a `;`.
|
|
3280
|
+
*
|
|
3281
|
+
* Omitting it is an ASI hazard rather than a cosmetic slip: the next line is
|
|
3282
|
+
* swallowed into the exported expression whenever it starts with `(`, `[`,
|
|
3283
|
+
* a template literal, `+`, `-`, or `/`. `export default foo;` followed by
|
|
3284
|
+
* `(function () {})();` silently becomes the single call
|
|
3285
|
+
* `export default foo(function () {})();`.
|
|
3286
|
+
*
|
|
3287
|
+
* The node type alone cannot decide this. A decorated
|
|
3288
|
+
* `export default @dec class Named {}` parses as a ClassExpression but is
|
|
3289
|
+
* still a declaration, so the two expression node types defer to
|
|
3290
|
+
* {@link isParenthesizedDefaultExport}, which reads the source.
|
|
3291
|
+
*
|
|
3292
|
+
* @param {AST.TSRXExportDefaultDeclaration} node - The export default node
|
|
3293
|
+
* @param {RippleFormatOptions} options - Prettier options
|
|
3294
|
+
* @returns {boolean}
|
|
3295
|
+
*/
|
|
3296
|
+
function defaultExportNeedsSemicolon(node, options) {
|
|
3297
|
+
const declaration = node.declaration;
|
|
3298
|
+
|
|
3299
|
+
if (!declaration) {
|
|
3300
|
+
return false;
|
|
3301
|
+
}
|
|
3302
|
+
|
|
3303
|
+
switch (declaration.type) {
|
|
3304
|
+
case 'FunctionDeclaration':
|
|
3305
|
+
case 'ClassDeclaration':
|
|
3306
|
+
case 'TSDeclareFunction':
|
|
3307
|
+
case 'TSInterfaceDeclaration':
|
|
3308
|
+
return false;
|
|
3309
|
+
case 'ClassExpression':
|
|
3310
|
+
case 'FunctionExpression':
|
|
3311
|
+
return isParenthesizedDefaultExport(node, options);
|
|
3312
|
+
default:
|
|
3313
|
+
return true;
|
|
3314
|
+
}
|
|
3315
|
+
}
|
|
3316
|
+
|
|
2977
3317
|
/**
|
|
2978
3318
|
* Print an export default declaration
|
|
2979
3319
|
* @param {AST.ExportDefaultDeclaration} node - The export default node
|
|
@@ -2985,8 +3325,29 @@ function isTemplateExpression(node) {
|
|
|
2985
3325
|
function printExportDefaultDeclaration(node, path, options, print) {
|
|
2986
3326
|
/** @type {Doc[]} */
|
|
2987
3327
|
const parts = [];
|
|
3328
|
+
parts.push(...printDeclarationDecorators(node, path, options, print));
|
|
2988
3329
|
parts.push('export default ');
|
|
2989
|
-
|
|
3330
|
+
|
|
3331
|
+
// Declaration forms end at their closing brace; expression forms are
|
|
3332
|
+
// statements and terminate.
|
|
3333
|
+
const terminator = defaultExportNeedsSemicolon(node, options) ? semi(options) : '';
|
|
3334
|
+
|
|
3335
|
+
if (isParenthesizedDefaultExport(node, options)) {
|
|
3336
|
+
// An expression export, not a declaration: it keeps its parens.
|
|
3337
|
+
const declaration = path.call(print, 'declaration');
|
|
3338
|
+
|
|
3339
|
+
if (getDecorators(/** @type {AST.Node} */ (node.declaration)).length > 0) {
|
|
3340
|
+
// The decorators stay inside the parens, each on its own line, so
|
|
3341
|
+
// the whole expression is indented to keep them off column zero.
|
|
3342
|
+
parts.push('(', indent([hardline, declaration]), hardline, ')', terminator);
|
|
3343
|
+
return parts;
|
|
3344
|
+
}
|
|
3345
|
+
|
|
3346
|
+
parts.push('(', declaration, ')', terminator);
|
|
3347
|
+
return parts;
|
|
3348
|
+
}
|
|
3349
|
+
|
|
3350
|
+
parts.push(path.call(print, 'declaration'), terminator);
|
|
2990
3351
|
return parts;
|
|
2991
3352
|
}
|
|
2992
3353
|
|
|
@@ -3539,7 +3900,13 @@ function printFunctionDeclaration(node, path, options, print) {
|
|
|
3539
3900
|
}
|
|
3540
3901
|
|
|
3541
3902
|
parts.push(' ');
|
|
3542
|
-
|
|
3903
|
+
|
|
3904
|
+
// `export default function () {}` is an anonymous FunctionDeclaration, the
|
|
3905
|
+
// one position where the name is optional. The space stays either way, so
|
|
3906
|
+
// the parameter list is not glued to the keyword.
|
|
3907
|
+
if (node.id) {
|
|
3908
|
+
parts.push(node.id.name);
|
|
3909
|
+
}
|
|
3543
3910
|
|
|
3544
3911
|
// Add TypeScript generics if present
|
|
3545
3912
|
if (node.typeParameters) {
|
|
@@ -4017,6 +4384,16 @@ function printObjectExpression(node, path, options, print, args) {
|
|
|
4017
4384
|
function printClassDeclaration(node, path, options, print) {
|
|
4018
4385
|
/** @type {Doc[]} */
|
|
4019
4386
|
const parts = [];
|
|
4387
|
+
|
|
4388
|
+
// Ambient/abstract markers change what the class is, so they must survive
|
|
4389
|
+
if (node.declare) {
|
|
4390
|
+
parts.push('declare ');
|
|
4391
|
+
}
|
|
4392
|
+
|
|
4393
|
+
if (node.abstract) {
|
|
4394
|
+
parts.push('abstract ');
|
|
4395
|
+
}
|
|
4396
|
+
|
|
4020
4397
|
parts.push('class');
|
|
4021
4398
|
|
|
4022
4399
|
// Class name (optional for ClassExpression)
|
|
@@ -4147,6 +4524,11 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4147
4524
|
/** @type {Doc[]} */
|
|
4148
4525
|
const parts = [];
|
|
4149
4526
|
|
|
4527
|
+
// `declare` must lead the modifier list
|
|
4528
|
+
if (node.declare) {
|
|
4529
|
+
parts.push('declare ');
|
|
4530
|
+
}
|
|
4531
|
+
|
|
4150
4532
|
// Access modifiers (public, private, protected)
|
|
4151
4533
|
if (node.accessibility) {
|
|
4152
4534
|
parts.push(node.accessibility);
|
|
@@ -4158,13 +4540,28 @@ function printPropertyDefinition(node, path, options, print) {
|
|
|
4158
4540
|
parts.push('static ');
|
|
4159
4541
|
}
|
|
4160
4542
|
|
|
4543
|
+
// Abstract / override keywords
|
|
4544
|
+
if (node.abstract) {
|
|
4545
|
+
parts.push('abstract ');
|
|
4546
|
+
}
|
|
4547
|
+
|
|
4548
|
+
if (node.override) {
|
|
4549
|
+
parts.push('override ');
|
|
4550
|
+
}
|
|
4551
|
+
|
|
4161
4552
|
// Readonly keyword
|
|
4162
4553
|
if (node.readonly) {
|
|
4163
4554
|
parts.push('readonly ');
|
|
4164
4555
|
}
|
|
4165
4556
|
|
|
4166
|
-
//
|
|
4167
|
-
|
|
4557
|
+
// `accessor` turns the field into a getter/setter pair — not decoration
|
|
4558
|
+
if (node.accessor) {
|
|
4559
|
+
parts.push('accessor ');
|
|
4560
|
+
}
|
|
4561
|
+
|
|
4562
|
+
// Property name. Must go through printKey so a computed key keeps its
|
|
4563
|
+
// brackets — `[key] = 1` and `key = 1` name different fields.
|
|
4564
|
+
parts.push(...printKey(node, path, options, print));
|
|
4168
4565
|
|
|
4169
4566
|
// Optional marker
|
|
4170
4567
|
if (node.optional) {
|
|
@@ -4211,6 +4608,15 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4211
4608
|
parts.push('static ');
|
|
4212
4609
|
}
|
|
4213
4610
|
|
|
4611
|
+
// Abstract / override keywords
|
|
4612
|
+
if (node.abstract) {
|
|
4613
|
+
parts.push('abstract ');
|
|
4614
|
+
}
|
|
4615
|
+
|
|
4616
|
+
if (node.override) {
|
|
4617
|
+
parts.push('override ');
|
|
4618
|
+
}
|
|
4619
|
+
|
|
4214
4620
|
// Method kind and name
|
|
4215
4621
|
if (node.kind === 'constructor') {
|
|
4216
4622
|
// skip as it's covered by the key
|
|
@@ -4258,12 +4664,14 @@ function printMethodDefinition(node, path, options, print) {
|
|
|
4258
4664
|
parts.push(': ', path.call(print, 'value', 'returnType'));
|
|
4259
4665
|
}
|
|
4260
4666
|
|
|
4261
|
-
// Method body
|
|
4262
|
-
|
|
4667
|
+
// Method body. Bodiless members (abstract, declared, overload signatures)
|
|
4668
|
+
// terminate with a semicolon — inventing an empty body makes an abstract
|
|
4669
|
+
// method concrete.
|
|
4263
4670
|
if (node.value && node.value.body) {
|
|
4671
|
+
parts.push(' ');
|
|
4264
4672
|
parts.push(path.call(print, 'value', 'body'));
|
|
4265
4673
|
} else {
|
|
4266
|
-
parts.push(
|
|
4674
|
+
parts.push(semi(options));
|
|
4267
4675
|
}
|
|
4268
4676
|
|
|
4269
4677
|
return parts;
|
|
@@ -4640,6 +5048,10 @@ function printTSEnumDeclaration(node, path, options, print) {
|
|
|
4640
5048
|
/** @type {Doc[]} */
|
|
4641
5049
|
const parts = [];
|
|
4642
5050
|
|
|
5051
|
+
if (node.declare) {
|
|
5052
|
+
parts.push('declare ');
|
|
5053
|
+
}
|
|
5054
|
+
|
|
4643
5055
|
// Handle 'const enum' vs 'enum'
|
|
4644
5056
|
if (node.const) {
|
|
4645
5057
|
parts.push('const ');
|
|
@@ -5398,6 +5810,12 @@ function printVariableDeclarator(node, path, options, print) {
|
|
|
5398
5810
|
const id = path.call(print, 'id');
|
|
5399
5811
|
const init = path.call(print, 'init');
|
|
5400
5812
|
|
|
5813
|
+
// A decorated class expression leads with its decorators on their own
|
|
5814
|
+
// lines, so the whole thing moves below the `=` to stay indented.
|
|
5815
|
+
if (node.init.type === 'ClassExpression' && getDecorators(node.init).length > 0) {
|
|
5816
|
+
return [id, ' =', indent([hardline, init])];
|
|
5817
|
+
}
|
|
5818
|
+
|
|
5401
5819
|
// For conditional expressions that will break, put them on a new line
|
|
5402
5820
|
if (node.init.type === 'ConditionalExpression') {
|
|
5403
5821
|
// Check if the ternary will break by checking if it has complex branches
|
|
@@ -5576,7 +5994,19 @@ function printTSTypeLiteral(node, path, options, print) {
|
|
|
5576
5994
|
function printTSPropertySignature(node, path, options, print) {
|
|
5577
5995
|
/** @type {Doc[]} */
|
|
5578
5996
|
const parts = [];
|
|
5579
|
-
|
|
5997
|
+
|
|
5998
|
+
// `readonly` is part of the declared type, not decoration — dropping it
|
|
5999
|
+
// silently widens the member to mutable.
|
|
6000
|
+
if (node.readonly) {
|
|
6001
|
+
parts.push('readonly ');
|
|
6002
|
+
}
|
|
6003
|
+
|
|
6004
|
+
// Computed keys keep their brackets — `[Symbol.iterator]` is not `Symbol.iterator`
|
|
6005
|
+
if (node.computed) {
|
|
6006
|
+
parts.push('[', path.call(print, 'key'), ']');
|
|
6007
|
+
} else {
|
|
6008
|
+
parts.push(path.call(print, 'key'));
|
|
6009
|
+
}
|
|
5580
6010
|
|
|
5581
6011
|
if (node.optional) {
|
|
5582
6012
|
parts.push('?');
|
|
@@ -5602,8 +6032,20 @@ function printTSMethodSignature(node, path, options, print) {
|
|
|
5602
6032
|
/** @type {Doc[]} */
|
|
5603
6033
|
const parts = [];
|
|
5604
6034
|
|
|
5605
|
-
//
|
|
5606
|
-
|
|
6035
|
+
// Accessor kind — without it `get x(): number` and `set x(v: number)`
|
|
6036
|
+
// both collapse to `x(...)`, which is a different (and duplicated) member.
|
|
6037
|
+
if (node.kind === 'get') {
|
|
6038
|
+
parts.push('get ');
|
|
6039
|
+
} else if (node.kind === 'set') {
|
|
6040
|
+
parts.push('set ');
|
|
6041
|
+
}
|
|
6042
|
+
|
|
6043
|
+
// Print the method name/key, keeping brackets on computed keys
|
|
6044
|
+
if (node.computed) {
|
|
6045
|
+
parts.push('[', path.call(print, 'key'), ']');
|
|
6046
|
+
} else {
|
|
6047
|
+
parts.push(path.call(print, 'key'));
|
|
6048
|
+
}
|
|
5607
6049
|
|
|
5608
6050
|
// Add optional marker if present
|
|
5609
6051
|
if (node.optional) {
|
|
@@ -5825,6 +6267,10 @@ function printTSIndexSignature(node, path, options, print) {
|
|
|
5825
6267
|
function printTSConstructorType(node, path, options, print) {
|
|
5826
6268
|
/** @type {Doc[]} */
|
|
5827
6269
|
const parts = [];
|
|
6270
|
+
// `abstract new () => T` only accepts abstract constructors
|
|
6271
|
+
if (node.abstract) {
|
|
6272
|
+
parts.push('abstract ');
|
|
6273
|
+
}
|
|
5828
6274
|
parts.push('new ');
|
|
5829
6275
|
parts.push('(');
|
|
5830
6276
|
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
|
});
|