flatlint 1.20.0 → 1.21.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.
package/ChangeLog CHANGED
@@ -1,3 +1,13 @@
1
+ 2025.01.03, v1.21.1
2
+
3
+ feature:
4
+ - eea4c32 flatlint: collect-args: simplify
5
+
6
+ 2025.01.03, v1.21.0
7
+
8
+ feature:
9
+ - d13da41 flatlint: add-missing-squire-brace: empty array
10
+
1
11
  2025.01.03, v1.20.0
2
12
 
3
13
  feature:
@@ -1,33 +1,33 @@
1
1
  import {
2
- CloseRoundBrace,
3
- OpenRoundBrace,
4
- Punctuator,
2
+ closeRoundBrace,
3
+ closeSquareBrace,
4
+ NOT_OK,
5
+ OK,
5
6
  } from '#types';
6
7
  import {equal} from './equal.js';
7
8
 
8
- export const collectArgs = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(')')}) => {
9
+ export const collectArgs = ({currentTokenIndex, tokens, nextTemplateToken = closeRoundBrace}) => {
9
10
  const n = tokens.length;
10
- const brace = Punctuator(']');
11
11
  let index = currentTokenIndex;
12
12
 
13
- if (equal(tokens[index - 1], OpenRoundBrace) && equal(tokens[index], CloseRoundBrace))
14
- return [false];
13
+ if (equal(tokens[index], closeRoundBrace))
14
+ return [NOT_OK];
15
15
 
16
16
  for (; index < n; index++) {
17
17
  const token = tokens[index];
18
18
 
19
- if (equal(token, CloseRoundBrace))
19
+ if (equal(token, closeRoundBrace))
20
20
  break;
21
21
 
22
22
  if (equal(token, nextTemplateToken))
23
23
  break;
24
24
 
25
- if (equal(token, brace))
25
+ if (equal(token, closeSquareBrace))
26
26
  break;
27
27
  }
28
28
 
29
29
  return [
30
- true,
30
+ OK,
31
31
  --index,
32
32
  ];
33
33
  };
@@ -1,26 +1,34 @@
1
1
  import {
2
- CloseRoundBrace,
3
- Punctuator,
2
+ closeRoundBrace,
3
+ closeSquareBrace,
4
+ semicolon,
5
+ OK,
6
+ NOT_OK,
4
7
  } from '#types';
5
8
  import {equal} from './equal.js';
6
9
 
7
- export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(';')}) => {
10
+ export const collectArray = ({currentTokenIndex, tokens, nextTemplateToken = semicolon}) => {
8
11
  const n = tokens.length;
9
- const brace = Punctuator(']');
10
12
  let index = currentTokenIndex;
11
13
 
14
+ if (equal(tokens[index], closeSquareBrace))
15
+ return [NOT_OK];
16
+
12
17
  for (; index < n; index++) {
13
18
  const token = tokens[index];
14
19
 
15
- if (equal(token, CloseRoundBrace))
20
+ if (equal(token, closeRoundBrace))
16
21
  break;
17
22
 
18
23
  if (equal(token, nextTemplateToken))
19
24
  break;
20
25
 
21
- if (equal(token, brace))
26
+ if (equal(token, closeSquareBrace))
22
27
  break;
23
28
  }
24
29
 
25
- return --index;
30
+ return [
31
+ OK,
32
+ --index,
33
+ ];
26
34
  };
@@ -1,11 +1,12 @@
1
- import {Punctuator} from '#types';
1
+ import {
2
+ closeRoundBrace,
3
+ openRoundBrace,
4
+ semicolon,
5
+ } from '#types';
2
6
  import {equal} from './equal.js';
3
7
 
4
- export const collectExpression = ({currentTokenIndex, tokens, nextTemplateToken = Punctuator(';')}) => {
8
+ export const collectExpression = ({currentTokenIndex, tokens, nextTemplateToken = semicolon}) => {
5
9
  const n = tokens.length;
6
- const closeBrace = Punctuator(')');
7
- const openBrace = Punctuator('(');
8
- const semicolon = Punctuator(';');
9
10
  let index = currentTokenIndex;
10
11
 
11
12
  for (; index < n; index++) {
@@ -17,10 +18,10 @@ export const collectExpression = ({currentTokenIndex, tokens, nextTemplateToken
17
18
  if (equal(token, nextTemplateToken))
18
19
  break;
19
20
 
20
- if (equal(token, openBrace))
21
+ if (equal(token, openRoundBrace))
21
22
  break;
22
23
 
23
- if (equal(token, closeBrace))
24
+ if (equal(token, closeRoundBrace))
24
25
  break;
25
26
  }
26
27
 
@@ -64,15 +64,19 @@ export const compare = (source, template) => {
64
64
  delta = indexOfExpressionEnd - currentTokenIndex;
65
65
  index = indexOfExpressionEnd - templateIndex;
66
66
  } else if (isTemplateArrayToken(templateToken)) {
67
- const indexOfArrayEnd = collectArray({
67
+ const [ok, indexOfArrayEnd] = collectArray({
68
68
  currentTokenIndex,
69
69
  tokens,
70
70
  templateToken,
71
71
  nextTemplateToken: templateTokens[templateIndex + 1],
72
72
  });
73
73
 
74
- delta = indexOfArrayEnd - currentTokenIndex;
75
- index = indexOfArrayEnd - templateIndex;
74
+ if (!ok) {
75
+ ++skip;
76
+ } else {
77
+ delta = indexOfArrayEnd - currentTokenIndex;
78
+ index = indexOfArrayEnd - templateIndex;
79
+ }
76
80
  } else if (!compareAll(currentToken, templateToken)) {
77
81
  isEqual = false;
78
82
  break;
@@ -113,29 +113,27 @@ function getValues(tokens, waysFrom) {
113
113
 
114
114
  for (const [name, index] of entries(waysFrom)) {
115
115
  let end = index;
116
+ let ok = true;
116
117
 
117
- if (isTemplateArray(name)) {
118
- end = collectArray({
118
+ if (isTemplateArray(name))
119
+ [ok, end] = collectArray({
119
120
  currentTokenIndex: index,
120
121
  tokens,
121
122
  });
122
- } else if (isTemplateExpression(name)) {
123
+ else if (isTemplateExpression(name))
123
124
  end = collectExpression({
124
125
  currentTokenIndex: index,
125
126
  tokens,
126
127
  });
127
- } else if (isTemplateArgs(name)) {
128
- let ok = false;
129
-
128
+ else if (isTemplateArgs(name))
130
129
  [ok, end] = collectArgs({
131
130
  currentTokenIndex: index,
132
131
  tokens,
133
132
  });
134
-
135
- if (!ok) {
136
- values[name] = [];
137
- continue;
138
- }
133
+
134
+ if (!ok) {
135
+ values[name] = [];
136
+ continue;
139
137
  }
140
138
 
141
139
  values[name] = tokens.slice(index, end + 1);
@@ -67,5 +67,10 @@ export const isTemplateArrayToken = (a) => isIdentifier(a) && isTemplateArray(a.
67
67
  export const isTemplateExpressionToken = (a) => isIdentifier(a) && isTemplateExpression(a.value);
68
68
  export const isTemplateArgsToken = (a) => isIdentifier(a) && isTemplateArgs(a.value);
69
69
 
70
- export const OpenRoundBrace = Punctuator('(');
71
- export const CloseRoundBrace = Punctuator(')');
70
+ export const openRoundBrace = Punctuator('(');
71
+ export const closeRoundBrace = Punctuator(')');
72
+ export const closeSquareBrace = Punctuator(']');
73
+ export const semicolon = Punctuator(';');
74
+
75
+ export const OK = true;
76
+ export const NOT_OK = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.20.0",
3
+ "version": "1.21.1",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",