@tsrx/prettier-plugin 0.3.112 → 0.3.114
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 +42 -2
- package/src/index.test.js +40 -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.114",
|
|
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.52"
|
|
31
31
|
},
|
|
32
32
|
"files": [
|
|
33
33
|
"src/"
|
package/src/index.js
CHANGED
|
@@ -214,6 +214,12 @@ function isRawScriptElement(node) {
|
|
|
214
214
|
* @returns {string} - The formatted string literal with quotes
|
|
215
215
|
*/
|
|
216
216
|
function formatStringLiteral(value, options) {
|
|
217
|
+
if (typeof value === 'bigint') {
|
|
218
|
+
// `JSON.stringify` throws on BigInt; a BigInt literal is its decimal
|
|
219
|
+
// digits plus the `n` suffix.
|
|
220
|
+
return `${value}n`;
|
|
221
|
+
}
|
|
222
|
+
|
|
217
223
|
if (typeof value !== 'string') {
|
|
218
224
|
return JSON.stringify(value);
|
|
219
225
|
}
|
|
@@ -229,6 +235,31 @@ function formatStringLiteral(value, options) {
|
|
|
229
235
|
return quote + escapedValue + quote;
|
|
230
236
|
}
|
|
231
237
|
|
|
238
|
+
/**
|
|
239
|
+
* Normalize a numeric literal the way Prettier's core printer does: keep the
|
|
240
|
+
* author's radix, separators, and exponent, but lowercase the notation and drop
|
|
241
|
+
* redundant zeroes. Reprinting `value` instead would rewrite the source
|
|
242
|
+
* (`0xff` -> `255`, `1_000` -> `1000`).
|
|
243
|
+
* @param {string} raw - The literal exactly as written in the source
|
|
244
|
+
* @returns {string} - The normalized literal
|
|
245
|
+
*/
|
|
246
|
+
function formatNumericLiteral(raw) {
|
|
247
|
+
return (
|
|
248
|
+
raw
|
|
249
|
+
.toLowerCase()
|
|
250
|
+
// Remove unnecessary plus and zeroes from scientific notation.
|
|
251
|
+
.replace(/^([+-]?[\d.]+e)(?:\+|(-))?0*(\d)/, '$1$2$3')
|
|
252
|
+
// Remove unnecessary scientific notation (1x).
|
|
253
|
+
.replace(/^([+-]?[\d.]+)e[+-]?0+$/, '$1')
|
|
254
|
+
// Make sure numbers always start with a digit.
|
|
255
|
+
.replace(/^([+-])?\./, '$10.')
|
|
256
|
+
// Remove extraneous trailing decimal zeroes.
|
|
257
|
+
.replace(/(\.\d+?)0+(?=e|$)/, '$1')
|
|
258
|
+
// Remove trailing dot.
|
|
259
|
+
.replace(/\.(?=e|$)/, '')
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
|
|
232
263
|
/**
|
|
233
264
|
* Add semicolon based on options.semi setting
|
|
234
265
|
* @param {RippleFormatOptions} options - Prettier options
|
|
@@ -1968,17 +1999,26 @@ function printRippleNode(node, path, options, print, args) {
|
|
|
1968
1999
|
}
|
|
1969
2000
|
break;
|
|
1970
2001
|
}
|
|
1971
|
-
case 'Literal':
|
|
2002
|
+
case 'Literal': {
|
|
1972
2003
|
// Handle regex literals specially
|
|
1973
2004
|
const node_typed = /** @type {AST.RegExpLiteral} */ (node);
|
|
2005
|
+
const bigint_typed = /** @type {AST.BigIntLiteral} */ (node);
|
|
1974
2006
|
if (node_typed.regex) {
|
|
1975
2007
|
// Regex literal: use the raw representation
|
|
1976
2008
|
nodeContent = node_typed.raw || `/${node_typed.regex.pattern}/${node_typed.regex.flags}`;
|
|
2009
|
+
} else if (typeof bigint_typed.bigint === 'string') {
|
|
2010
|
+
// BigInt literal: `bigint` is always decimal, so only `raw` keeps
|
|
2011
|
+
// the author's radix (`0xffn`).
|
|
2012
|
+
nodeContent = (bigint_typed.raw || `${bigint_typed.bigint}n`).toLowerCase();
|
|
2013
|
+
} else if (typeof node.value === 'number' && typeof node_typed.raw === 'string') {
|
|
2014
|
+
// Numeric literal: normalize, but never reprint from `value`.
|
|
2015
|
+
nodeContent = formatNumericLiteral(node_typed.raw);
|
|
1977
2016
|
} else {
|
|
1978
|
-
// String,
|
|
2017
|
+
// String, boolean, or null literal
|
|
1979
2018
|
nodeContent = formatStringLiteral(node.value, options);
|
|
1980
2019
|
}
|
|
1981
2020
|
break;
|
|
2021
|
+
}
|
|
1982
2022
|
|
|
1983
2023
|
case 'ArrowFunctionExpression':
|
|
1984
2024
|
nodeContent = printArrowFunction(node, path, options, print, args);
|
package/src/index.test.js
CHANGED
|
@@ -6687,6 +6687,46 @@ function RowList({ rows, Row }) {
|
|
|
6687
6687
|
});
|
|
6688
6688
|
});
|
|
6689
6689
|
|
|
6690
|
+
describe('numeric literals', () => {
|
|
6691
|
+
it('prints bigint literals instead of crashing on JSON.stringify', async () => {
|
|
6692
|
+
const input = `const total = 1n;
|
|
6693
|
+
const mask = 0xffn;`;
|
|
6694
|
+
|
|
6695
|
+
const result = await format(input);
|
|
6696
|
+
expect(result).toBeWithNewline(input);
|
|
6697
|
+
});
|
|
6698
|
+
|
|
6699
|
+
it('prints bigint literals inside templates', async () => {
|
|
6700
|
+
const input = `function App() @{
|
|
6701
|
+
const label = "big:";
|
|
6702
|
+
<div>
|
|
6703
|
+
{label}
|
|
6704
|
+
{1n}
|
|
6705
|
+
</div>
|
|
6706
|
+
}`;
|
|
6707
|
+
|
|
6708
|
+
const result = await format(input);
|
|
6709
|
+
expect(result).toBeWithNewline(input);
|
|
6710
|
+
});
|
|
6711
|
+
|
|
6712
|
+
it('keeps the authored radix, separators, and exponent of numeric literals', async () => {
|
|
6713
|
+
const input = `const a = 0xFF;
|
|
6714
|
+
const b = 1_000_000;
|
|
6715
|
+
const c = .5;
|
|
6716
|
+
const d = 1e21;
|
|
6717
|
+
const e = 1E3;
|
|
6718
|
+
const f = 1.50;`;
|
|
6719
|
+
|
|
6720
|
+
const result = await format(input);
|
|
6721
|
+
expect(result).toBeWithNewline(`const a = 0xff;
|
|
6722
|
+
const b = 1_000_000;
|
|
6723
|
+
const c = 0.5;
|
|
6724
|
+
const d = 1e21;
|
|
6725
|
+
const e = 1e3;
|
|
6726
|
+
const f = 1.5;`);
|
|
6727
|
+
});
|
|
6728
|
+
});
|
|
6729
|
+
|
|
6690
6730
|
describe('idempotence', () => {
|
|
6691
6731
|
/**
|
|
6692
6732
|
* @param {string} code
|