@tsrx/prettier-plugin 0.4.9 → 0.4.11
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 +105 -1
- package/src/index.test.js +190 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tsrx/prettier-plugin",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.11",
|
|
4
4
|
"description": "TSRX plugin for Prettier",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"prettier": "^3.9.6"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@tsrx/core": "0.3.
|
|
30
|
+
"@tsrx/core": "0.3.3"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"src/"
|
package/src/index.js
CHANGED
|
@@ -4624,9 +4624,102 @@ function printClassBody(node, path, options, print) {
|
|
|
4624
4624
|
}
|
|
4625
4625
|
contentParts.push(line);
|
|
4626
4626
|
contentParts.push(members[i]);
|
|
4627
|
+
if (options.semi === false && needsClassPropertySemicolon(node.body[i], node.body[i + 1])) {
|
|
4628
|
+
contentParts.push(';');
|
|
4629
|
+
}
|
|
4627
4630
|
}
|
|
4628
4631
|
|
|
4629
|
-
|
|
4632
|
+
// Without semicolons only a line break ends a field, an index signature or
|
|
4633
|
+
// a bodiless method, so a class with one before another member can't
|
|
4634
|
+
// collapse onto a single line
|
|
4635
|
+
const shouldBreak =
|
|
4636
|
+
options.semi === false &&
|
|
4637
|
+
node.body.some(
|
|
4638
|
+
(member, i) =>
|
|
4639
|
+
i < node.body.length - 1 &&
|
|
4640
|
+
member.type !== 'StaticBlock' &&
|
|
4641
|
+
!(member.type === 'MethodDefinition' && member.value.body),
|
|
4642
|
+
);
|
|
4643
|
+
|
|
4644
|
+
return group(['{', indent(contentParts), line, '}'], { shouldBreak });
|
|
4645
|
+
}
|
|
4646
|
+
|
|
4647
|
+
/**
|
|
4648
|
+
* Whether a class field printed without semicolons still needs one, because
|
|
4649
|
+
* the next member would otherwise continue its value or type (`x = a` then
|
|
4650
|
+
* `[k] = 1` reads as `x = a[k] = 1`) or a bare `static`, `get` or `set` field
|
|
4651
|
+
* would become that member's modifier. Mirrors Prettier's
|
|
4652
|
+
* shouldPrintSemicolonAfterClassProperty.
|
|
4653
|
+
* @param {AST.Node} node - The member just printed
|
|
4654
|
+
* @param {AST.Node | undefined} next - The member after it
|
|
4655
|
+
* @returns {boolean}
|
|
4656
|
+
*/
|
|
4657
|
+
function needsClassPropertySemicolon(node, next) {
|
|
4658
|
+
if (node.type !== 'PropertyDefinition' || !next) {
|
|
4659
|
+
return false;
|
|
4660
|
+
}
|
|
4661
|
+
|
|
4662
|
+
// Only these keywords modify a member on the next line; `readonly`,
|
|
4663
|
+
// `declare`, `async` and the rest must share its line
|
|
4664
|
+
const name = getPrintedKeyName(node);
|
|
4665
|
+
if (
|
|
4666
|
+
!node.value &&
|
|
4667
|
+
!node.typeAnnotation &&
|
|
4668
|
+
(name === 'static' || name === 'get' || name === 'set')
|
|
4669
|
+
) {
|
|
4670
|
+
return true;
|
|
4671
|
+
}
|
|
4672
|
+
|
|
4673
|
+
// Unless a modifier leads, its `[` would index the field's value
|
|
4674
|
+
if (next.type === 'TSIndexSignature') {
|
|
4675
|
+
return !next.static && !next.readonly;
|
|
4676
|
+
}
|
|
4677
|
+
|
|
4678
|
+
// `static { ... }` starts with a keyword
|
|
4679
|
+
if (next.type !== 'PropertyDefinition' && next.type !== 'MethodDefinition') {
|
|
4680
|
+
return false;
|
|
4681
|
+
}
|
|
4682
|
+
|
|
4683
|
+
// So does a member led by a modifier, and a keyword can't continue the field
|
|
4684
|
+
if (
|
|
4685
|
+
next.static ||
|
|
4686
|
+
next.accessibility ||
|
|
4687
|
+
next.abstract ||
|
|
4688
|
+
next.override ||
|
|
4689
|
+
(next.type === 'PropertyDefinition' && (next.readonly || next.declare || next.accessor)) ||
|
|
4690
|
+
(next.type === 'MethodDefinition' &&
|
|
4691
|
+
(next.value.async || next.kind === 'get' || next.kind === 'set'))
|
|
4692
|
+
) {
|
|
4693
|
+
return false;
|
|
4694
|
+
}
|
|
4695
|
+
|
|
4696
|
+
// `in` and `instanceof` read as operators on the field's value
|
|
4697
|
+
const nextName = getPrintedKeyName(next);
|
|
4698
|
+
if (nextName === 'in' || nextName === 'instanceof') {
|
|
4699
|
+
return true;
|
|
4700
|
+
}
|
|
4701
|
+
|
|
4702
|
+
// `[` indexes the field's value and `*` multiplies it
|
|
4703
|
+
return next.computed || (next.type === 'MethodDefinition' && next.value.generator === true);
|
|
4704
|
+
}
|
|
4705
|
+
|
|
4706
|
+
/**
|
|
4707
|
+
* The name a class member's key prints as. printKey unquotes string keys that
|
|
4708
|
+
* are valid identifiers, so `"static"` prints as `static` and parses as one.
|
|
4709
|
+
* @param {AST.PropertyDefinition | AST.MethodDefinition} member - The class member
|
|
4710
|
+
* @returns {string | null} The name, or null for computed and non-name keys
|
|
4711
|
+
*/
|
|
4712
|
+
function getPrintedKeyName(member) {
|
|
4713
|
+
if (member.computed) {
|
|
4714
|
+
return null;
|
|
4715
|
+
}
|
|
4716
|
+
if (member.key.type === 'Identifier') {
|
|
4717
|
+
return member.key.name;
|
|
4718
|
+
}
|
|
4719
|
+
if (member.key.type === 'Literal' && typeof member.key.value === 'string') {
|
|
4720
|
+
return member.key.value;
|
|
4721
|
+
}
|
|
4722
|
+
return null;
|
|
4630
4723
|
}
|
|
4631
4724
|
|
|
4632
4725
|
/**
|
|
@@ -6373,6 +6466,10 @@ function printTSNamedTupleMember(node, path, options, print) {
|
|
|
6373
6466
|
function printTSIndexSignature(node, path, options, print) {
|
|
6374
6467
|
/** @type {Doc[]} */
|
|
6375
6468
|
const parts = [];
|
|
6469
|
+
// A static index signature types the constructor, not its instances
|
|
6470
|
+
if (node.static === true) {
|
|
6471
|
+
parts.push('static ');
|
|
6472
|
+
}
|
|
6376
6473
|
if (node.readonly === true) {
|
|
6377
6474
|
parts.push('readonly ');
|
|
6378
6475
|
}
|
|
@@ -6390,6 +6487,13 @@ function printTSIndexSignature(node, path, options, print) {
|
|
|
6390
6487
|
parts.push(path.call(print, 'typeAnnotation'));
|
|
6391
6488
|
}
|
|
6392
6489
|
|
|
6490
|
+
// Interfaces and type literals separate their members, but class members
|
|
6491
|
+
// end themselves — without this the class body runs into the next member
|
|
6492
|
+
const parent = /** @type {AST.Node | null} */ (path.getParentNode());
|
|
6493
|
+
if (parent?.type === 'ClassBody') {
|
|
6494
|
+
parts.push(semi(options));
|
|
6495
|
+
}
|
|
6496
|
+
|
|
6393
6497
|
return parts;
|
|
6394
6498
|
}
|
|
6395
6499
|
|
package/src/index.test.js
CHANGED
|
@@ -7119,6 +7119,32 @@ function g() {
|
|
|
7119
7119
|
}`);
|
|
7120
7120
|
});
|
|
7121
7121
|
|
|
7122
|
+
it('keeps static and the member separator on class index signatures', async () => {
|
|
7123
|
+
await expectUnchanged('class Registry { [name: string]: number; count = 1; }');
|
|
7124
|
+
await expectUnchanged('class Registry { static [name: string]: number; }');
|
|
7125
|
+
await expectUnchanged(`class Cache {
|
|
7126
|
+
static readonly [key: string]: number;
|
|
7127
|
+
readonly [index: number]: string;
|
|
7128
|
+
[key: symbol]: unknown;
|
|
7129
|
+
size = 0;
|
|
7130
|
+
clear() {}
|
|
7131
|
+
}`);
|
|
7132
|
+
|
|
7133
|
+
const result = await format(
|
|
7134
|
+
`class Cache {
|
|
7135
|
+
static [key: string]: number;
|
|
7136
|
+
[index: number]: string;
|
|
7137
|
+
clearAllEntriesFromTheCacheAndResetTheSize() {}
|
|
7138
|
+
}`,
|
|
7139
|
+
{ semi: false },
|
|
7140
|
+
);
|
|
7141
|
+
expect(result).toBeWithNewline(`class Cache {
|
|
7142
|
+
static [key: string]: number
|
|
7143
|
+
[index: number]: string
|
|
7144
|
+
clearAllEntriesFromTheCacheAndResetTheSize() {}
|
|
7145
|
+
}`);
|
|
7146
|
+
});
|
|
7147
|
+
|
|
7122
7148
|
it('keeps override on class members', async () => {
|
|
7123
7149
|
await expectUnchanged(`class Derived extends Base {
|
|
7124
7150
|
override toString(): string {
|
|
@@ -7214,6 +7240,170 @@ declare enum Level {
|
|
|
7214
7240
|
});
|
|
7215
7241
|
});
|
|
7216
7242
|
|
|
7243
|
+
// With `semi: false` only a line break ends a class field, and a few next
|
|
7244
|
+
// members still read as its continuation, so those keep a semicolon.
|
|
7245
|
+
describe('class members without semicolons', () => {
|
|
7246
|
+
/**
|
|
7247
|
+
* Assert the input is already formatted without semicolons and comes
|
|
7248
|
+
* back byte-identical.
|
|
7249
|
+
* @param {string} source
|
|
7250
|
+
*/
|
|
7251
|
+
const expectUnchanged = async (source) => {
|
|
7252
|
+
const result = await format(source, { semi: false });
|
|
7253
|
+
expect(result).toBeWithNewline(source);
|
|
7254
|
+
};
|
|
7255
|
+
|
|
7256
|
+
it('puts members that end without a block on their own lines', async () => {
|
|
7257
|
+
const result = await format(
|
|
7258
|
+
'class Registry { [name: string]: number; count = 1; reset(): void; clear() {} }',
|
|
7259
|
+
{ semi: false },
|
|
7260
|
+
);
|
|
7261
|
+
expect(result).toBeWithNewline(`class Registry {
|
|
7262
|
+
[name: string]: number
|
|
7263
|
+
count = 1
|
|
7264
|
+
reset(): void
|
|
7265
|
+
clear() {}
|
|
7266
|
+
}`);
|
|
7267
|
+
});
|
|
7268
|
+
|
|
7269
|
+
it.each([
|
|
7270
|
+
'class Point { x = 1 }',
|
|
7271
|
+
'class List { first() {} last() {} }',
|
|
7272
|
+
'class Lazy { static {} value = 1 }',
|
|
7273
|
+
])('keeps a class on one line when no member needs a line break: %s', async (source) => {
|
|
7274
|
+
await expectUnchanged(source);
|
|
7275
|
+
});
|
|
7276
|
+
|
|
7277
|
+
it.each([
|
|
7278
|
+
`class A {
|
|
7279
|
+
x = a;
|
|
7280
|
+
[k] = 1
|
|
7281
|
+
}`,
|
|
7282
|
+
`class A {
|
|
7283
|
+
x: string;
|
|
7284
|
+
[k] = 1
|
|
7285
|
+
}`,
|
|
7286
|
+
`class A {
|
|
7287
|
+
x = a;
|
|
7288
|
+
[k: string]: unknown
|
|
7289
|
+
}`,
|
|
7290
|
+
`class A {
|
|
7291
|
+
x = a;
|
|
7292
|
+
*gen() {}
|
|
7293
|
+
}`,
|
|
7294
|
+
`class A {
|
|
7295
|
+
x = a;
|
|
7296
|
+
[k]() {}
|
|
7297
|
+
}`,
|
|
7298
|
+
`class A {
|
|
7299
|
+
x = a;
|
|
7300
|
+
in = 1
|
|
7301
|
+
}`,
|
|
7302
|
+
`class A {
|
|
7303
|
+
x = a;
|
|
7304
|
+
instanceof = 1
|
|
7305
|
+
}`,
|
|
7306
|
+
`class A {
|
|
7307
|
+
static;
|
|
7308
|
+
run() {}
|
|
7309
|
+
}`,
|
|
7310
|
+
`class A {
|
|
7311
|
+
get;
|
|
7312
|
+
set;
|
|
7313
|
+
value = 1
|
|
7314
|
+
}`,
|
|
7315
|
+
])('keeps the semicolon the next member depends on: %s', async (source) => {
|
|
7316
|
+
await expectUnchanged(source);
|
|
7317
|
+
});
|
|
7318
|
+
|
|
7319
|
+
it.each([
|
|
7320
|
+
`class A {
|
|
7321
|
+
x = a
|
|
7322
|
+
static [k] = 1
|
|
7323
|
+
}`,
|
|
7324
|
+
`class A {
|
|
7325
|
+
x = a
|
|
7326
|
+
private [k] = 1
|
|
7327
|
+
}`,
|
|
7328
|
+
`class A {
|
|
7329
|
+
x = a
|
|
7330
|
+
readonly [k: string]: unknown
|
|
7331
|
+
}`,
|
|
7332
|
+
`class A {
|
|
7333
|
+
x = a
|
|
7334
|
+
async *gen() {}
|
|
7335
|
+
}`,
|
|
7336
|
+
`class A {
|
|
7337
|
+
x = a
|
|
7338
|
+
get [k]() {}
|
|
7339
|
+
}`,
|
|
7340
|
+
`class A {
|
|
7341
|
+
x = a
|
|
7342
|
+
#p = 1
|
|
7343
|
+
}`,
|
|
7344
|
+
`class A {
|
|
7345
|
+
x = a
|
|
7346
|
+
static {}
|
|
7347
|
+
}`,
|
|
7348
|
+
`class A {
|
|
7349
|
+
[k: string]: unknown
|
|
7350
|
+
[j] = 1
|
|
7351
|
+
}`,
|
|
7352
|
+
`class A {
|
|
7353
|
+
x = a
|
|
7354
|
+
as = 1
|
|
7355
|
+
}`,
|
|
7356
|
+
`class A {
|
|
7357
|
+
x = a
|
|
7358
|
+
satisfies: T
|
|
7359
|
+
}`,
|
|
7360
|
+
])('omits the semicolon before a member that cannot continue: %s', async (source) => {
|
|
7361
|
+
await expectUnchanged(source);
|
|
7362
|
+
});
|
|
7363
|
+
|
|
7364
|
+
// Unlike `static`, `get` and `set`, these only modify a member that
|
|
7365
|
+
// starts on the same line
|
|
7366
|
+
it.each([
|
|
7367
|
+
`class A {
|
|
7368
|
+
readonly
|
|
7369
|
+
value = 1
|
|
7370
|
+
}`,
|
|
7371
|
+
`class A {
|
|
7372
|
+
declare
|
|
7373
|
+
value: string
|
|
7374
|
+
}`,
|
|
7375
|
+
`class A {
|
|
7376
|
+
private
|
|
7377
|
+
run() {}
|
|
7378
|
+
}`,
|
|
7379
|
+
`class A {
|
|
7380
|
+
async
|
|
7381
|
+
run() {}
|
|
7382
|
+
}`,
|
|
7383
|
+
])('omits the semicolon after a field named like a same-line modifier: %s', async (source) => {
|
|
7384
|
+
await expectUnchanged(source);
|
|
7385
|
+
});
|
|
7386
|
+
|
|
7387
|
+
it('keeps the semicolon when a quoted key prints as a keyword', async () => {
|
|
7388
|
+
const result = await format(`class A { "static"; run() {} x = a; 'in' = 1 }`, {
|
|
7389
|
+
semi: false,
|
|
7390
|
+
});
|
|
7391
|
+
expect(result).toBeWithNewline(`class A {
|
|
7392
|
+
static;
|
|
7393
|
+
run() {}
|
|
7394
|
+
x = a;
|
|
7395
|
+
in = 1
|
|
7396
|
+
}`);
|
|
7397
|
+
});
|
|
7398
|
+
|
|
7399
|
+
it('keeps the semicolon ahead of a trailing comment', async () => {
|
|
7400
|
+
await expectUnchanged(`class A {
|
|
7401
|
+
x = a; // first
|
|
7402
|
+
[k] = 1
|
|
7403
|
+
}`);
|
|
7404
|
+
});
|
|
7405
|
+
});
|
|
7406
|
+
|
|
7217
7407
|
// Type arguments, `this` types, and heritage clauses decide what a
|
|
7218
7408
|
// declaration means. Dropping one either breaks the file or quietly
|
|
7219
7409
|
// widens a type, so each must come back exactly as written.
|