prettier-plugin-insert-comma 1.1.6 → 1.1.7

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 (2) hide show
  1. package/dist/index.js +85 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,8 +7,52 @@ function fixMissingCommas(code, skipTrailing = false) {
7
7
  let isEscaped = false;
8
8
  let out = '';
9
9
  let lastWasComment = false;
10
+ let inTemplateLiteral = false;
11
+ const recentTokens = [];
12
+ let currentWord = '';
13
+ const blockStack = [];
10
14
  for (let i = 0; i < code.length; i++) {
11
15
  const ch = code[i];
16
+ if (!inString && !inTemplateLiteral && !lastWasComment) {
17
+ if (/[a-zA-Z0-9_$]/.test(ch)) {
18
+ currentWord += ch;
19
+ }
20
+ else {
21
+ if (currentWord) {
22
+ recentTokens.push(currentWord);
23
+ if (recentTokens.length > 5)
24
+ recentTokens.shift();
25
+ currentWord = '';
26
+ }
27
+ if (!/\s/.test(ch)) {
28
+ if (ch === '/' && (code[i + 1] === '/' || code[i + 1] === '*')) {
29
+ }
30
+ else if (ch === '=' && code[i + 1] === '>') {
31
+ recentTokens.push('=>');
32
+ if (recentTokens.length > 5)
33
+ recentTokens.shift();
34
+ }
35
+ else {
36
+ recentTokens.push(ch);
37
+ if (recentTokens.length > 5)
38
+ recentTokens.shift();
39
+ }
40
+ }
41
+ }
42
+ }
43
+ if (inTemplateLiteral) {
44
+ out += ch;
45
+ if (isEscaped) {
46
+ isEscaped = false;
47
+ }
48
+ else if (ch === '\\') {
49
+ isEscaped = true;
50
+ }
51
+ else if (ch === '`') {
52
+ inTemplateLiteral = false;
53
+ }
54
+ continue;
55
+ }
12
56
  if (inString) {
13
57
  out += ch;
14
58
  if (isEscaped) {
@@ -40,18 +84,54 @@ function fixMissingCommas(code, skipTrailing = false) {
40
84
  continue;
41
85
  }
42
86
  }
43
- if (ch === '"' || ch === "'" || ch === '`') {
87
+ if (ch === '`') {
88
+ inTemplateLiteral = true;
89
+ isEscaped = false;
90
+ out += ch;
91
+ continue;
92
+ }
93
+ if (ch === '"' || ch === "'") {
44
94
  inString = true;
45
95
  quote = ch;
46
96
  isEscaped = false;
47
97
  out += ch;
48
98
  continue;
49
99
  }
50
- if (ch === '{' || ch === '[')
100
+ if (ch === '{' || ch === '[') {
101
+ let type = 'object';
102
+ if (ch === '{') {
103
+ if (recentTokens.length > 1) {
104
+ const prev = recentTokens[recentTokens.length - 2];
105
+ if (prev === ')' || prev === 'try' || prev === 'catch' ||
106
+ prev === 'finally' || prev === 'else' || prev === 'do' ||
107
+ prev === '>' || prev === 'class' || prev === 'interface' ||
108
+ prev === 'type' || prev === 'namespace' || prev === 'enum' ||
109
+ prev === 'get' || prev === 'set' || prev === 'module' ||
110
+ prev === 'switch') {
111
+ type = 'block';
112
+ }
113
+ else if (recentTokens.length > 2) {
114
+ const prev2 = recentTokens[recentTokens.length - 3];
115
+ if (prev2 === 'function' || prev2 === 'class' ||
116
+ prev2 === 'interface' || prev2 === 'extends' ||
117
+ prev2 === 'implements' || prev2 === 'type') {
118
+ type = 'block';
119
+ }
120
+ }
121
+ }
122
+ else {
123
+ type = 'block';
124
+ }
125
+ }
126
+ blockStack.push(type);
51
127
  depth++;
52
- if (ch === '}' || ch === ']')
128
+ }
129
+ if (ch === '}' || ch === ']') {
53
130
  depth--;
54
- if (depth > 0 && ch === '\n') {
131
+ blockStack.pop();
132
+ }
133
+ const isObjectContext = blockStack.length > 0 && blockStack[blockStack.length - 1] === 'object';
134
+ if (isObjectContext && ch === '\n') {
55
135
  let lastCharIdx = out.length - 1;
56
136
  while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
57
137
  lastCharIdx--;
@@ -77,7 +157,7 @@ function fixMissingCommas(code, skipTrailing = false) {
77
157
  const nextSlice = code.slice(nextStartIdx);
78
158
  const isNextKeyOrClosing = nextSlice[0] === '}' ||
79
159
  nextSlice[0] === ']' ||
80
- /^[^\n:]+:/.test(nextSlice);
160
+ /^(?:[a-zA-Z_$][a-zA-Z0-9_$]*|'[^']*'|"[^"]*"|\[[^\]\n]+\])\s*:/.test(nextSlice);
81
161
  const isPrevSeparator = prev === ',' ||
82
162
  prev === '{' ||
83
163
  prev === '[' ||
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.6",
4
+ "version": "1.1.7",
5
5
  "description": "A Prettier plugin for inserting missing commas in multi-line objects.",
6
6
  "funding": "https://github.com/sponsors/refirst11",
7
7
  "author": "Refirst11",