prettier-plugin-insert-comma 1.1.0 → 1.1.1

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.
Files changed (3) hide show
  1. package/dist/index.js +31 -57
  2. package/package.json +1 -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
- if (ch === '\n') {
59
- let lastCharIdx = out.length - 1;
60
- while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
61
- lastCharIdx--;
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
- const prev = out[lastCharIdx];
64
- let nextStartIdx = i + 1;
65
- while (nextStartIdx < code.length) {
66
- while (nextStartIdx < code.length && /\s/.test(code[nextStartIdx])) {
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
- const nextSlice = code.slice(nextStartIdx);
82
- const isNextKeyOrClosing = nextSlice[0] === '}' ||
83
- nextSlice[0] === ']' ||
84
- /^[^\n:]+:/.test(nextSlice);
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
- if (ch === ' ') {
96
- const nextSlice = code.slice(i + 1);
97
- if (/^[^\n:]+:/.test(nextSlice)) {
98
- let lastCharIdx = out.length - 1;
99
- while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
100
- lastCharIdx--;
101
- }
102
- const prev = out[lastCharIdx];
103
- const isPrevSeparator = prev === ',' ||
104
- prev === '{' ||
105
- prev === '[' ||
106
- prev === ':' ||
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,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "prettier-plugin-insert-comma",
4
- "version": "1.1.0",
4
+ "version": "1.1.1",
5
5
  "description": "A Prettier plugin for inserting missing commas in objects.",
6
6
  "funding": "https://github.com/sponsors/refirst11",
7
7
  "author": "Refirst11",
@@ -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 between object properties before formatting.
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