flatlint 1.28.0 → 1.30.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/ChangeLog CHANGED
@@ -1,3 +1,14 @@
1
+ 2025.01.09, v1.30.0
2
+
3
+ feature:
4
+ - 2379871 flatlint: remoe-useless-comma: improve
5
+
6
+ 2025.01.09, v1.29.0
7
+
8
+ feature:
9
+ - 8d308ac flatlint: remove-useless-comma: curly
10
+ - f070e0c flatlint: convert-coma-to-semicolon: return
11
+
1
12
  2025.01.08, v1.28.0
2
13
 
3
14
  feature:
package/README.md CHANGED
@@ -24,6 +24,11 @@ npm i flatlint
24
24
  ```diff
25
25
  -const a = 5,
26
26
  +const a = 5;
27
+
28
+ function x() {
29
+ - return m,
30
+ + return m;
31
+ }
27
32
  ```
28
33
 
29
34
  </details>
@@ -106,6 +111,17 @@ const a = {
106
111
 
107
112
  </details>
108
113
 
114
+ <details><summary>remove useless comma</summary>
115
+
116
+ ```diff
117
+ function x() {
118
+ return m;
119
+ -},
120
+ +}
121
+ ```
122
+
123
+ </details>
124
+
109
125
  <details><summary>add missing quote</summary>
110
126
 
111
127
  ```diff
@@ -1,7 +1,9 @@
1
+ import {quote} from '#types';
2
+
1
3
  export const report = () => 'Add missing quote';
2
4
 
3
5
  export const match = () => ({
4
- '__a("__b': (vars, path) => !path.isNextPunctuator(vars.quote),
6
+ '__a("__b': (vars, path) => !path.isNextPunctuator(quote),
5
7
  });
6
8
 
7
9
  export const replace = () => ({
@@ -15,4 +15,5 @@ export const match = () => ({
15
15
  export const replace = () => ({
16
16
  'const __a = __b,': 'const __a = __b;',
17
17
  '__a(__args),': '__a(__args);',
18
+ 'return __expr,': 'return __expr;',
18
19
  });
@@ -1,4 +1,11 @@
1
- import {isIdentifier} from '#types';
1
+ import {
2
+ closeCurlyBrace,
3
+ closeRoundBrace,
4
+ isIdentifier,
5
+ isPunctuator,
6
+ openCurlyBrace,
7
+ openSquireBrace,
8
+ } from '#types';
2
9
 
3
10
  export const report = () => 'Remove useless coma';
4
11
 
@@ -11,9 +18,25 @@ export const match = () => ({
11
18
 
12
19
  return false;
13
20
  },
21
+ '},': (vars, path) => {
22
+ for (const token of path.getAllPrev()) {
23
+ if (isPunctuator(token, openSquireBrace))
24
+ return false;
25
+ }
26
+
27
+ if (path.isNextPunctuator(closeRoundBrace))
28
+ return true;
29
+
30
+ if (path.isNextPunctuator(closeCurlyBrace))
31
+ return false;
32
+
33
+ return path.isNextIdentifier('const');
34
+ },
14
35
  });
15
36
 
16
37
  export const replace = () => ({
17
38
  '__a(__args) {},': '__a(__args) {}',
18
39
  '__a(),': '__a()',
40
+ '},': '}',
41
+ '}),': '})',
19
42
  });
@@ -2,12 +2,17 @@ import {
2
2
  isIdentifier,
3
3
  isNewLine,
4
4
  isPunctuator,
5
+ isWhiteSpace,
5
6
  } from '#types';
6
7
 
7
8
  export const createPath = ({tokens, start, end}) => ({
8
9
  tokens,
9
10
  start,
10
11
  end,
12
+ isNextIdentifier: createIsNextIdentifier({
13
+ tokens,
14
+ end,
15
+ }),
11
16
  getAllPrev: createGetAllPrev({
12
17
  tokens,
13
18
  start,
@@ -30,8 +35,42 @@ export const createPath = ({tokens, start, end}) => ({
30
35
  }),
31
36
  });
32
37
 
38
+ const next = ({tokens, end}) => {
39
+ let i = end - 1;
40
+
41
+ while (++i) {
42
+ const token = tokens[i];
43
+
44
+ if (!token)
45
+ return token;
46
+
47
+ if (isNewLine(token))
48
+ continue;
49
+
50
+ if (isWhiteSpace(token))
51
+ continue;
52
+
53
+ return token;
54
+ }
55
+ };
56
+
57
+ const createIsNextIdentifier = ({tokens, end}) => (value) => {
58
+ const current = next({
59
+ tokens,
60
+ end,
61
+ });
62
+
63
+ if (!current)
64
+ return false;
65
+
66
+ return isIdentifier(current, value);
67
+ };
68
+
33
69
  const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
34
- const current = tokens[end];
70
+ const current = next({
71
+ tokens,
72
+ end,
73
+ });
35
74
 
36
75
  if (!current)
37
76
  return false;
@@ -2,6 +2,9 @@ const isString = (a) => typeof a === 'string';
2
2
 
3
3
  export const isWhiteSpace = ({type}) => type === 'WhiteSpace';
4
4
  export const isIdentifier = (token, value = token.value) => {
5
+ if (!token)
6
+ return false;
7
+
5
8
  const {type} = token;
6
9
  const is = type === 'IdentifierName';
7
10
 
@@ -16,6 +19,9 @@ export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
16
19
  export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
17
20
  export const notWhiteSpace = (a) => !isWhiteSpace(a);
18
21
  export const isPunctuator = (token, punctuator) => {
22
+ if (!token)
23
+ return false;
24
+
19
25
  if (token.type !== 'Punctuator')
20
26
  return false;
21
27
 
@@ -83,6 +89,11 @@ export const closeSquareBrace = Punctuator(']');
83
89
  export const semicolon = Punctuator(';');
84
90
  export const comma = Punctuator(',');
85
91
  export const colon = Punctuator(':');
92
+ export const openCurlyBrace = Punctuator('{');
93
+ export const closeCurlyBrace = Punctuator('}');
94
+ export const openSquireBrace = Punctuator('[');
95
+ export const closeSquireBrace = Punctuator(']');
96
+ export const quote = Punctuator(`'`);
86
97
 
87
98
  export const OK = true;
88
99
  export const NOT_OK = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.28.0",
3
+ "version": "1.30.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",