prettier-plugin-insert-comma 0.1.0

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/README.md ADDED
@@ -0,0 +1,77 @@
1
+ # prettier-plugin-insert-comma
2
+
3
+ A [Prettier](https://prettier.io/) plugin that automatically inserts missing commas between object properties before formatting.
4
+
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
+
7
+ ## Example
8
+
9
+ **Before** — Prettier would throw a parse error:
10
+
11
+ ```js
12
+ const config = {
13
+ host: 'localhost'
14
+ port: 3000
15
+ options: {
16
+ debug: true
17
+ verbose: false
18
+ }
19
+ }
20
+ ```
21
+
22
+ **After** — commas are inserted, Prettier formats normally:
23
+
24
+ ```js
25
+ const config = {
26
+ host: 'localhost',
27
+ port: 3000,
28
+ options: {
29
+ debug: true,
30
+ verbose: false,
31
+ },
32
+ };
33
+ ```
34
+
35
+ ## Installation
36
+
37
+ Install `prettier-plugin-insert-comma` as a dev dependency:
38
+
39
+ ```sh
40
+ npm install -D prettier prettier-plugin-insert-comma
41
+ ```
42
+
43
+ Then add the plugin to your [Prettier configuration](https://prettier.io/docs/configuration.html):
44
+
45
+ ```jsonc
46
+ // .prettierrc
47
+ {
48
+ "plugins": ["prettier-plugin-insert-comma"],
49
+ }
50
+ ```
51
+
52
+ For JSON files, you need to specify the parser explicitly with `overrides`:
53
+
54
+ ```json
55
+ {
56
+ "plugins": ["prettier-plugin-insert-comma"],
57
+ "overrides": [
58
+ {
59
+ "files": ["*.json"],
60
+ "options": {
61
+ "parser": "json",
62
+ "quoteProps": "preserve",
63
+ "singleQuote": false,
64
+ "trailingComma": "none"
65
+ }
66
+ }
67
+ ]
68
+ }
69
+ ```
70
+
71
+ ## Supported Parsers
72
+
73
+ | Parser | Languages |
74
+ | ------------ | --------------- |
75
+ | `typescript` | TypeScript, TSX |
76
+ | `babel` | JavaScript, JSX |
77
+ | `json5` | JSON5 |
@@ -0,0 +1,9 @@
1
+ import type { Parser } from 'prettier';
2
+ declare const plugin: {
3
+ parsers: {
4
+ typescript: Parser<any>;
5
+ babel: Parser<any>;
6
+ json: Parser<any>;
7
+ };
8
+ };
9
+ export default plugin;
package/dist/index.js ADDED
@@ -0,0 +1,120 @@
1
+ import typescriptParser from 'prettier/parser-typescript';
2
+ import babelParser from 'prettier/parser-babel';
3
+ function fixMissingCommas(code) {
4
+ let depth = 0;
5
+ let inString = false;
6
+ let quote = null;
7
+ let isEscaped = false;
8
+ let out = '';
9
+ for (let i = 0; i < code.length; i++) {
10
+ const ch = code[i];
11
+ if (inString) {
12
+ out += ch;
13
+ if (isEscaped) {
14
+ isEscaped = false;
15
+ }
16
+ else if (ch === '\\') {
17
+ isEscaped = true;
18
+ }
19
+ else if (ch === quote) {
20
+ inString = false;
21
+ }
22
+ continue;
23
+ }
24
+ if (ch === '/' && code[i + 1] === '/') {
25
+ const lineEnd = code.indexOf('\n', i);
26
+ const comment = code.slice(i, lineEnd === -1 ? code.length : lineEnd);
27
+ out += comment;
28
+ i += comment.length - 1;
29
+ continue;
30
+ }
31
+ if (ch === '/' && code[i + 1] === '*') {
32
+ const endIdx = code.indexOf('*/', i + 2);
33
+ if (endIdx !== -1) {
34
+ const comment = code.slice(i, endIdx + 2);
35
+ out += comment;
36
+ i += comment.length - 1;
37
+ continue;
38
+ }
39
+ }
40
+ if (ch === '"' || ch === "'" || ch === '`') {
41
+ inString = true;
42
+ quote = ch;
43
+ isEscaped = false;
44
+ out += ch;
45
+ continue;
46
+ }
47
+ if (ch === '{' || ch === '[')
48
+ depth++;
49
+ if (ch === '}' || ch === ']')
50
+ depth--;
51
+ if (depth > 0) {
52
+ if (ch === '\n') {
53
+ let lastCharIdx = out.length - 1;
54
+ while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
55
+ lastCharIdx--;
56
+ }
57
+ const prev = out[lastCharIdx];
58
+ let nextStartIdx = i + 1;
59
+ while (nextStartIdx < code.length && /\s/.test(code[nextStartIdx])) {
60
+ nextStartIdx++;
61
+ }
62
+ const nextSlice = code.slice(nextStartIdx);
63
+ const isNextKeyOrClosing = nextSlice[0] === '}' ||
64
+ nextSlice[0] === ']' ||
65
+ /^[^\n:]+:/.test(nextSlice);
66
+ const isPrevSeparator = prev === ',' ||
67
+ prev === '{' ||
68
+ prev === '[' ||
69
+ prev === ':' ||
70
+ prev === ';' ||
71
+ prev === undefined;
72
+ if (isNextKeyOrClosing && !isPrevSeparator) {
73
+ out += ',';
74
+ }
75
+ }
76
+ if (ch === ' ') {
77
+ const nextSlice = code.slice(i + 1);
78
+ if (/^[^\n:]+:/.test(nextSlice)) {
79
+ let lastCharIdx = out.length - 1;
80
+ while (lastCharIdx >= 0 && /\s/.test(out[lastCharIdx])) {
81
+ lastCharIdx--;
82
+ }
83
+ const prev = out[lastCharIdx];
84
+ const isPrevSeparator = prev === ',' ||
85
+ prev === '{' ||
86
+ prev === '[' ||
87
+ prev === ':' ||
88
+ prev === ';' ||
89
+ prev === undefined;
90
+ if (!isPrevSeparator) {
91
+ out += ',';
92
+ }
93
+ }
94
+ }
95
+ }
96
+ out += ch;
97
+ }
98
+ return out;
99
+ }
100
+ function wrapParser(parser) {
101
+ return {
102
+ ...parser,
103
+ async preprocess(text, options) {
104
+ let next = text;
105
+ if (parser.preprocess) {
106
+ const result = await parser.preprocess(text, options);
107
+ next = result;
108
+ }
109
+ return fixMissingCommas(next);
110
+ },
111
+ };
112
+ }
113
+ const plugin = {
114
+ parsers: {
115
+ typescript: wrapParser(typescriptParser.parsers.typescript),
116
+ babel: wrapParser(babelParser.parsers.babel),
117
+ json: wrapParser(babelParser.parsers.json),
118
+ },
119
+ };
120
+ export default plugin;
package/license ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) refirst11 and contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "type": "module",
3
+ "name": "prettier-plugin-insert-comma",
4
+ "version": "0.1.0",
5
+ "description": "A Prettier plugin for inserting missing commas in objects.",
6
+ "funding": "https://github.com/sponsors/refirst11",
7
+ "author": "Refirst11",
8
+ "license": "MIT",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/refirst11/prettier-plugin-insert-comma.git"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/refirst11/prettier-plugin-insert-comma/issues"
15
+ },
16
+ "keywords": [
17
+ "prettier",
18
+ "prettier-plugin",
19
+ "comma",
20
+ "missing-comma",
21
+ "auto-fix",
22
+ "typescript",
23
+ "javascript",
24
+ "json"
25
+ ],
26
+ "main": "dist/index.js",
27
+ "module": "dist/index.js",
28
+ "types": "dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "scripts": {
33
+ "build": "pnpm tsc"
34
+ },
35
+ "peerDependencies": {
36
+ "prettier": "^3.0"
37
+ },
38
+ "devDependencies": {
39
+ "typescript": "^5.9.3"
40
+ }
41
+ }