prettier-plugin-insert-comma 1.0.2 → 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 +30 -38
  2. package/package.json +1 -1
  3. package/readme.md +1 -1
package/dist/index.js CHANGED
@@ -51,49 +51,41 @@ function fixMissingCommas(code) {
51
51
  depth++;
52
52
  if (ch === '}' || ch === ']')
53
53
  depth--;
54
- if (depth > 0) {
55
- if (ch === '\n') {
56
- let lastCharIdx = out.length - 1;
57
- while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
58
- lastCharIdx--;
59
- }
60
- const prev = out[lastCharIdx];
61
- let nextStartIdx = i + 1;
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
62
  while (nextStartIdx < code.length && /\s/.test(code[nextStartIdx])) {
63
63
  nextStartIdx++;
64
64
  }
65
- const nextSlice = code.slice(nextStartIdx);
66
- const isNextKeyOrClosing = nextSlice[0] === '}' ||
67
- nextSlice[0] === ']' ||
68
- /^[^\n:]+:/.test(nextSlice);
69
- const isPrevSeparator = prev === ',' ||
70
- prev === '{' ||
71
- prev === '[' ||
72
- prev === ':' ||
73
- prev === ';' ||
74
- prev === undefined;
75
- if (isNextKeyOrClosing && !isPrevSeparator && !lastWasComment) {
76
- out += ',';
65
+ if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '/') {
66
+ const lineEnd = code.indexOf('\n', nextStartIdx);
67
+ nextStartIdx = lineEnd === -1 ? code.length : lineEnd + 1;
68
+ continue;
77
69
  }
78
- }
79
- if (ch === ' ') {
80
- const nextSlice = code.slice(i + 1);
81
- if (/^[^\n:]+:/.test(nextSlice)) {
82
- let lastCharIdx = out.length - 1;
83
- while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
84
- lastCharIdx--;
85
- }
86
- const prev = out[lastCharIdx];
87
- const isPrevSeparator = prev === ',' ||
88
- prev === '{' ||
89
- prev === '[' ||
90
- prev === ':' ||
91
- prev === ';' ||
92
- prev === undefined;
93
- if (!isPrevSeparator && !lastWasComment) {
94
- out += ',';
95
- }
70
+ if (code[nextStartIdx] === '/' && code[nextStartIdx + 1] === '*') {
71
+ const endIdx = code.indexOf('*/', nextStartIdx + 2);
72
+ nextStartIdx = endIdx === -1 ? code.length : endIdx + 2;
73
+ continue;
96
74
  }
75
+ break;
76
+ }
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 += ',';
97
89
  }
98
90
  }
99
91
  out += ch;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "prettier-plugin-insert-comma",
4
- "version": "1.0.2",
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",
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