flatlint 1.89.0 → 1.90.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.31, v1.90.1
2
+
3
+ feature:
4
+ - a1794be flatlint: compare: simplify
5
+
6
+ 2025.01.31, v1.90.0
7
+
8
+ feature:
9
+ - 441f386 flatlint: add-missing-round-brace: destructuring
10
+
1
11
  2025.01.30, v1.89.0
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -77,6 +77,9 @@ const m = {
77
77
  - z: z('hello'
78
78
  + z: z('hello')
79
79
  }
80
+
81
+ -{hello} = world;
82
+ +({hello} = world);
80
83
  ```
81
84
 
82
85
  </details>
@@ -51,5 +51,8 @@ export const collectExpression = ({currentTokenIndex, tokens, nextTemplateToken
51
51
  if (isNewLine(tokens[index - 1]))
52
52
  --index;
53
53
 
54
+ if (currentTokenIndex === index)
55
+ return currentTokenIndex;
56
+
54
57
  return --index;
55
58
  };
@@ -23,17 +23,21 @@ export const compare = (source, template, {index = 0} = {}) => {
23
23
 
24
24
  const n = tokens.length - 1;
25
25
  const templateTokensLength = templateTokens.length;
26
+
26
27
  let isEqual = false;
27
28
  let start = 0;
28
29
  let end = 0;
29
30
  let delta = 0;
30
31
 
31
32
  for (; index < n; index++) {
33
+ const indexCheck = index;
32
34
  let skip = 0;
33
35
 
34
- for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
36
+ for (const [templateIndex] of templateTokens.entries()) {
35
37
  let currentTokenIndex = index + templateIndex - skip;
36
38
 
39
+ checkIndexes(index, indexCheck);
40
+
37
41
  if (currentTokenIndex > n)
38
42
  return [NOT_OK];
39
43
 
@@ -64,11 +68,12 @@ export const compare = (source, template, {index = 0} = {}) => {
64
68
  nextTemplateToken: templateTokens[templateIndex + 1],
65
69
  });
66
70
 
67
- if (indexOfExpressionEnd >= n - 1) {
71
+ const sameTokensIndex = templateIndex === templateTokensLength - 1;
72
+ const outOfBound = indexOfExpressionEnd >= n;
73
+
74
+ if (outOfBound || sameTokensIndex) {
68
75
  end = indexOfExpressionEnd;
69
76
  currentTokenIndex = end;
70
- } else if (templateIndex === templateTokensLength - 1) {
71
- currentTokenIndex = end;
72
77
  } else {
73
78
  delta = indexOfExpressionEnd - currentTokenIndex;
74
79
  index = indexOfExpressionEnd - templateIndex;
@@ -124,3 +129,13 @@ function compareAll(a, b) {
124
129
 
125
130
  return false;
126
131
  }
132
+
133
+ function checkIndexes(index, indexCheck) {
134
+ /* c8 ignore start */
135
+ if (indexCheck > index + 1)
136
+ throw Error(`index should never decrease more then on one: ${index} > ${indexCheck}`);
137
+
138
+ if (index < 0)
139
+ throw Error(`index should never be < zero: ${index}`);
140
+ /* c8 ignore end */
141
+ }
@@ -1,5 +1,6 @@
1
1
  import {
2
2
  assign,
3
+ isKeyword,
3
4
  isOneOfKeywords,
4
5
  isOneOfPunctuators,
5
6
  semicolon,
@@ -9,10 +10,9 @@ export const report = () => 'Add missing assign';
9
10
 
10
11
  export const match = () => ({
11
12
  '__x __a __expr': ({__expr, __x}, path) => {
12
- if (path.isNextKeyword())
13
- return false;
13
+ const [first] = __expr;
14
14
 
15
- if (path.isNextPunctuator(assign))
15
+ if (isKeyword(first))
16
16
  return false;
17
17
 
18
18
  if (!isOneOfKeywords(__x, ['const', 'let', 'var']))
@@ -20,4 +20,5 @@ export const replace = () => ({
20
20
  'if (__a.__b(__args) {': 'if (__a.__b(__args)) {',
21
21
  'if (__a(__args) {': 'if (__a(__args)) {',
22
22
  'if (__a(__args)': 'if (__a(__args))',
23
+ '{__a} = __expr;': '({__a} = __expr);',
23
24
  });
@@ -8,6 +8,7 @@ import {
8
8
  closeCurlyBrace,
9
9
  isOnlyWhitespaces,
10
10
  isKeyword,
11
+ openRoundBrace,
11
12
  } from '#types';
12
13
 
13
14
  export const report = () => 'Add missing semicolon';
@@ -25,6 +26,8 @@ export const match = () => ({
25
26
  semicolon,
26
27
  openCurlyBrace,
27
28
  question,
29
+ closeRoundBrace,
30
+ openRoundBrace,
28
31
  ];
29
32
 
30
33
  return !path.isNextPunctuator(punctuators);
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  assign,
3
- closeRoundBrace,
4
3
  hasRoundBraces,
5
4
  isBalancedRoundBraces,
6
5
  isPunctuator,
@@ -17,16 +16,22 @@ export const match = () => ({
17
16
  return !isBalancedRoundBraces(__expr);
18
17
  },
19
18
 
20
- '__a);': ({__a}, path) => {
19
+ '__a);': (vars, path) => {
20
+ let result = false;
21
+
21
22
  for (const current of path.getAllPrev()) {
22
- if (isPunctuator(current, openRoundBrace))
23
- return false;
23
+ if (isPunctuator(current, openRoundBrace)) {
24
+ result = false;
25
+ break;
26
+ }
24
27
 
25
- if (isPunctuator(current, assign))
26
- return true;
28
+ if (isPunctuator(current, assign)) {
29
+ result = true;
30
+ break;
31
+ }
27
32
  }
28
33
 
29
- return false;
34
+ return result;
30
35
  },
31
36
  });
32
37
 
@@ -22,6 +22,7 @@ export const replace = (tokens, {fix, rule, plugin}) => {
22
22
  let index = 0;
23
23
 
24
24
  while (index < tokens.length - 1) {
25
+ log(`compare: ${rule}: ${from} -> ${to}`);
25
26
  const [ok, start, end] = compare(tokens, from, {
26
27
  index,
27
28
  });
@@ -52,7 +53,7 @@ export const replace = (tokens, {fix, rule, plugin}) => {
52
53
  }
53
54
 
54
55
  if (fix) {
55
- log(`${rule}: ${from} -> ${to}`);
56
+ log(`fix: ${rule}: ${from} -> ${to}`);
56
57
 
57
58
  const preparedTo = prepare(to);
58
59
  const waysTo = findVarsWays(preparedTo);
@@ -29,12 +29,8 @@ export const isIdentifier = (token, newToken) => {
29
29
  export const isStringLiteral = ({type}) => type === 'StringLiteral';
30
30
  export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
31
31
  export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
32
- export const notWhiteSpace = (a) => !isWhiteSpace(a);
33
32
 
34
33
  export const isKeyword = (token, name) => {
35
- if (!token)
36
- return false;
37
-
38
34
  const {value} = token;
39
35
 
40
36
  if (!keyword.isKeyword(value))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.89.0",
3
+ "version": "1.90.1",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",