prettier-plugin-insert-comma 1.1.0 → 1.1.2
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/dist/index.js +31 -57
- package/package.json +2 -3
- package/readme.md +1 -1
package/dist/index.js
CHANGED
|
@@ -1,8 +1,5 @@
|
|
|
1
1
|
import typescriptParser from 'prettier/parser-typescript';
|
|
2
2
|
import babelParser from 'prettier/parser-babel';
|
|
3
|
-
function isCheckMode() {
|
|
4
|
-
return process.argv.some((arg) => arg === '--check' || arg === '-c');
|
|
5
|
-
}
|
|
6
3
|
function fixMissingCommas(code) {
|
|
7
4
|
let depth = 0;
|
|
8
5
|
let inString = false;
|
|
@@ -54,62 +51,41 @@ function fixMissingCommas(code) {
|
|
|
54
51
|
depth++;
|
|
55
52
|
if (ch === '}' || ch === ']')
|
|
56
53
|
depth--;
|
|
57
|
-
if (depth > 0) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
54
|
+
if (depth > 0 && ch === '\n') {
|
|
55
|
+
let lastCharIdx = out.length - 1;
|
|
56
|
+
while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
|
|
57
|
+
lastCharIdx--;
|
|
58
|
+
}
|
|
59
|
+
const prev = out[lastCharIdx];
|
|
60
|
+
let nextStartIdx = i + 1;
|
|
61
|
+
while (nextStartIdx < code.length) {
|
|
62
|
+
while (nextStartIdx < code.length && /\s/.test(code[nextStartIdx])) {
|
|
63
|
+
nextStartIdx++;
|
|
62
64
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
nextStartIdx++;
|
|
68
|
-
}
|
|
69
|
-
if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '/') {
|
|
70
|
-
const lineEnd = code.indexOf('\n', nextStartIdx);
|
|
71
|
-
nextStartIdx = lineEnd === -1 ? code.length : lineEnd + 1;
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '*') {
|
|
75
|
-
const endIdx = code.indexOf('*/', nextStartIdx + 2);
|
|
76
|
-
nextStartIdx = endIdx === -1 ? code.length : endIdx + 2;
|
|
77
|
-
continue;
|
|
78
|
-
}
|
|
79
|
-
break;
|
|
65
|
+
if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '/') {
|
|
66
|
+
const lineEnd = code.indexOf('\n', nextStartIdx);
|
|
67
|
+
nextStartIdx = lineEnd === -1 ? code.length : lineEnd + 1;
|
|
68
|
+
continue;
|
|
80
69
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
const isPrevSeparator = prev === ',' ||
|
|
86
|
-
prev === '{' ||
|
|
87
|
-
prev === '[' ||
|
|
88
|
-
prev === ':' ||
|
|
89
|
-
prev === ';' ||
|
|
90
|
-
prev === undefined;
|
|
91
|
-
if (isNextKeyOrClosing && !isPrevSeparator && !lastWasComment) {
|
|
92
|
-
out += ',';
|
|
70
|
+
if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '*') {
|
|
71
|
+
const endIdx = code.indexOf('*/', nextStartIdx + 2);
|
|
72
|
+
nextStartIdx = endIdx === -1 ? code.length : endIdx + 2;
|
|
73
|
+
continue;
|
|
93
74
|
}
|
|
75
|
+
break;
|
|
94
76
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
prev === ';' ||
|
|
108
|
-
prev === undefined;
|
|
109
|
-
if (!isPrevSeparator && !lastWasComment) {
|
|
110
|
-
out += ',';
|
|
111
|
-
}
|
|
112
|
-
}
|
|
77
|
+
const nextSlice = code.slice(nextStartIdx);
|
|
78
|
+
const isNextKeyOrClosing = nextSlice[0] === '}' ||
|
|
79
|
+
nextSlice[0] === ']' ||
|
|
80
|
+
/^[^\n:]+:/.test(nextSlice);
|
|
81
|
+
const isPrevSeparator = prev === ',' ||
|
|
82
|
+
prev === '{' ||
|
|
83
|
+
prev === '[' ||
|
|
84
|
+
prev === ':' ||
|
|
85
|
+
prev === ';' ||
|
|
86
|
+
prev === undefined;
|
|
87
|
+
if (isNextKeyOrClosing && !isPrevSeparator && !lastWasComment) {
|
|
88
|
+
out += ',';
|
|
113
89
|
}
|
|
114
90
|
}
|
|
115
91
|
out += ch;
|
|
@@ -123,8 +99,6 @@ function wrapParser(parser) {
|
|
|
123
99
|
return {
|
|
124
100
|
...parser,
|
|
125
101
|
async preprocess(text, options) {
|
|
126
|
-
if (isCheckMode())
|
|
127
|
-
return text;
|
|
128
102
|
let next = text;
|
|
129
103
|
if (parser.preprocess) {
|
|
130
104
|
const result = await parser.preprocess(text, options);
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "prettier-plugin-insert-comma",
|
|
4
|
-
"version": "1.1.
|
|
5
|
-
"description": "A Prettier plugin for inserting missing commas in
|
|
4
|
+
"version": "1.1.2",
|
|
5
|
+
"description": "A Prettier plugin for inserting missing commas in multi-line object.",
|
|
6
6
|
"funding": "https://github.com/sponsors/refirst11",
|
|
7
7
|
"author": "Refirst11",
|
|
8
8
|
"license": "MIT",
|
|
@@ -36,7 +36,6 @@
|
|
|
36
36
|
"prettier": "^3.0"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@types/node": "^25.3.3",
|
|
40
39
|
"typescript": "^5.9.3"
|
|
41
40
|
},
|
|
42
41
|
"publishConfig": {
|
package/readme.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# prettier-plugin-insert-comma
|
|
2
2
|
|
|
3
|
-
A [Prettier](https://prettier.io/) plugin that automatically inserts missing commas
|
|
3
|
+
A [Prettier](https://prettier.io/) plugin that automatically inserts missing commas in multi-line object properties before formatting.
|
|
4
4
|
|
|
5
5
|
Prettier cannot format code with syntax errors like missing commas in objects. This plugin runs as a preprocessor to insert commas between properties, allowing Prettier to format your code successfully.
|
|
6
6
|
|