flatlint 1.21.2 → 1.23.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,13 @@
1
+ 2025.01.05, v1.23.0
2
+
3
+ feature:
4
+ - a38f979 flatlint: replacer: improve match
5
+
6
+ 2025.01.04, v1.22.0
7
+
8
+ feature:
9
+ - 807d059 flatlint: add-missing-semicolon: add
10
+
1
11
  2025.01.03, v1.21.2
2
12
 
3
13
  feature:
package/README.md CHANGED
@@ -132,6 +132,15 @@ const a = class {
132
132
 
133
133
  </details>
134
134
 
135
+ <details><summary>add missing semicolon</summary>
136
+
137
+ ```diff
138
+ -const a = 5
139
+ +const a = 5;
140
+ ```
141
+
142
+ </details>
143
+
135
144
  ## Template literals
136
145
 
137
146
  **FlatLint** uses language similar to 🐊[**PutoutScript**](https://github.com/coderaiser/putout/blob/master/docs/putout-script.md#-putoutscript).
@@ -1,12 +1,11 @@
1
1
  import {
2
2
  closeRoundBrace,
3
- closeSquareBrace,
4
3
  NOT_OK,
5
4
  OK,
6
5
  } from '#types';
7
6
  import {equal} from './equal.js';
8
7
 
9
- export const collectArgs = ({currentTokenIndex, tokens, nextTemplateToken = closeRoundBrace}) => {
8
+ export const collectArgs = ({currentTokenIndex, tokens}) => {
10
9
  const n = tokens.length;
11
10
  let index = currentTokenIndex;
12
11
 
@@ -18,12 +17,6 @@ export const collectArgs = ({currentTokenIndex, tokens, nextTemplateToken = clos
18
17
 
19
18
  if (equal(token, closeRoundBrace))
20
19
  break;
21
-
22
- if (equal(token, nextTemplateToken))
23
- break;
24
-
25
- if (equal(token, closeSquareBrace))
26
- break;
27
20
  }
28
21
 
29
22
  return [
@@ -30,12 +30,6 @@ export const compare = (source, template) => {
30
30
  for (let index = 0; index < n; index++) {
31
31
  for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
32
32
  const currentTokenIndex = index + templateIndex - skip;
33
-
34
- if (currentTokenIndex > n) {
35
- isEqual = false;
36
- break;
37
- }
38
-
39
33
  const templateToken = templateTokens[templateIndex];
40
34
  const currentToken = tokens[currentTokenIndex];
41
35
 
@@ -61,6 +55,11 @@ export const compare = (source, template) => {
61
55
  nextTemplateToken: templateTokens[templateIndex + 1],
62
56
  });
63
57
 
58
+ if (n === indexOfExpressionEnd) {
59
+ end = indexOfExpressionEnd;
60
+ continue;
61
+ }
62
+
64
63
  delta = indexOfExpressionEnd - currentTokenIndex;
65
64
  index = indexOfExpressionEnd - templateIndex;
66
65
  } else if (isTemplateArrayToken(templateToken)) {
@@ -1,9 +1,5 @@
1
- export function report() {
2
- return 'Add missing round braces';
3
- }
1
+ export const report = () => 'Add missing round braces';
4
2
 
5
- export function replace() {
6
- return {
7
- 'if __a > __b': 'if (__a > __b)',
8
- };
9
- }
3
+ export const replace = () => ({
4
+ 'if __a > __b': 'if (__a > __b)',
5
+ });
@@ -0,0 +1,23 @@
1
+ import {isPunctuator} from '#types';
2
+
3
+ export const report = () => 'Add missing semicolon';
4
+
5
+ export const match = () => ({
6
+ 'const __a = __expr': check,
7
+ '__a(__args)': check,
8
+ });
9
+
10
+ export const replace = () => ({
11
+ 'const __a = __expr': 'const __a = __expr;',
12
+ '__a(__args)': '__a(__args);',
13
+ });
14
+
15
+ function check(vars, path) {
16
+ let last;
17
+
18
+ for (const token of path.getNext()) {
19
+ last = token;
20
+ }
21
+
22
+ return !isPunctuator(last, ';');
23
+ }
@@ -1,9 +1,5 @@
1
- export function report() {
2
- return 'Add missing square brace';
3
- }
1
+ export const report = () => 'Add missing square brace';
4
2
 
5
- export function replace() {
6
- return {
7
- '[__array;': '[__array];',
8
- };
9
- }
3
+ export const replace = () => ({
4
+ '[__array;': '[__array];',
5
+ });
@@ -5,7 +5,7 @@ export const report = () => 'Remove useless semicolon';
5
5
  export const match = () => ({
6
6
  '__a: __expr;': (vars, path) => {
7
7
  for (const token of path.getPrev()) {
8
- if (isIdentifier(token) && token.value === 'interface')
8
+ if (isIdentifier(token, 'interface'))
9
9
  return false;
10
10
  }
11
11
 
@@ -2,8 +2,6 @@ export function report() {
2
2
  return `Wrap the assignment in parentheses after '&&'`;
3
3
  }
4
4
 
5
- export function replace() {
6
- return {
7
- '__a && __b = __c': '__a && (__b = __c)',
8
- };
9
- }
5
+ export const replace = () => ({
6
+ '__a && __b = __c': '__a && (__b = __c)',
7
+ });
@@ -1,8 +1,14 @@
1
+ import {isNewLine} from '#types';
2
+
1
3
  export const createPath = ({tokens, start}) => ({
2
4
  getPrev: createGetPrev({
3
5
  tokens,
4
6
  start,
5
7
  }),
8
+ getNext: createGetNext({
9
+ tokens,
10
+ start,
11
+ }),
6
12
  });
7
13
 
8
14
  function createGetPrev({tokens, start}) {
@@ -12,3 +18,16 @@ function createGetPrev({tokens, start}) {
12
18
  }
13
19
  };
14
20
  }
21
+
22
+ function createGetNext({tokens, start}) {
23
+ return function*() {
24
+ for (let i = start; i < tokens.length; ++i) {
25
+ const current = tokens[i];
26
+
27
+ if (isNewLine(current))
28
+ continue;
29
+
30
+ yield current;
31
+ }
32
+ };
33
+ }
@@ -15,44 +15,48 @@ import {collectArgs} from '../compare/collect-args.js';
15
15
  const returns = (a) => () => a;
16
16
  const {entries} = Object;
17
17
 
18
- export const match = (tokens, {plugin}) => {
19
- const match = plugin.match ?? returns([]);
20
- let result = true;
21
-
22
- for (const [from, fn] of entries(match())) {
23
- const [, start, end] = compare(tokens, from);
24
- const current = tokens.slice(start, end);
25
-
26
- const waysFrom = findVarsWays(prepare(from));
27
- const values = getValues(current, waysFrom);
28
-
29
- result = fn(values, createPath({
30
- tokens,
31
- start,
32
- end,
33
- }));
34
- }
35
-
36
- return result;
37
- };
38
-
39
18
  export const replace = (tokens, {fix, rule, plugin}) => {
40
19
  const places = [];
41
20
  let isFixed = false;
21
+ const match = plugin.match?.() ?? returns({});
42
22
 
43
- for (const [from, to] of entries(plugin.replace())) {
23
+ for (let [from, to] of entries(plugin.replace())) {
44
24
  const [ok, start, end] = compare(tokens, from);
45
25
 
46
26
  if (!ok)
47
27
  continue;
48
28
 
49
- if (fix) {
50
- runReplace(tokens, {
51
- from,
52
- to,
29
+ const values = getCurrentValues({
30
+ from,
31
+ start,
32
+ end,
33
+ tokens,
34
+ });
35
+
36
+ const matchFn = match[from];
37
+
38
+ if (matchFn) {
39
+ const is = matchFn(values, createPath({
40
+ tokens,
53
41
  start,
54
42
  end,
43
+ }));
44
+
45
+ if (!is)
46
+ continue;
47
+ }
48
+
49
+ if (fix) {
50
+ to = prepare(to);
51
+ const waysTo = findVarsWays(to);
52
+
53
+ setValues({
54
+ values,
55
+ waysTo,
56
+ to,
55
57
  });
58
+
59
+ tokens.splice(start, end - start, ...to);
56
60
  isFixed = true;
57
61
  continue;
58
62
  }
@@ -71,26 +75,6 @@ export const replace = (tokens, {fix, rule, plugin}) => {
71
75
  return [isFixed, places];
72
76
  };
73
77
 
74
- function runReplace(tokens, {from, to, start, end}) {
75
- const current = tokens.slice(start, end);
76
-
77
- to = prepare(to);
78
- from = prepare(from);
79
-
80
- const waysFrom = findVarsWays(from);
81
- const values = getValues(current, waysFrom);
82
-
83
- const waysTo = findVarsWays(to);
84
-
85
- setValues({
86
- values,
87
- waysTo,
88
- to,
89
- });
90
-
91
- tokens.splice(start, end - start, ...to);
92
- }
93
-
94
78
  function findVarsWays(tokens) {
95
79
  const ways = {};
96
80
 
@@ -147,3 +131,11 @@ function setValues({to, waysTo, values}) {
147
131
  to.splice(index, 1, ...values[name]);
148
132
  }
149
133
  }
134
+
135
+ function getCurrentValues({from, start, end, tokens}) {
136
+ const current = tokens.slice(start, end);
137
+
138
+ const waysFrom = findVarsWays(prepare(from));
139
+ return getValues(current, waysFrom);
140
+ }
141
+
@@ -1,4 +1,4 @@
1
- import {match, replace} from './replacer.js';
1
+ import {replace} from './replacer.js';
2
2
 
3
3
  export const run = (tokens, {fix, fixCount = fix ? 10 : 1, plugins}) => {
4
4
  const places = [];
@@ -7,16 +7,14 @@ export const run = (tokens, {fix, fixCount = fix ? 10 : 1, plugins}) => {
7
7
  const fixed = [];
8
8
 
9
9
  for (const {rule, plugin} of plugins) {
10
- if (match(tokens, {plugin})) {
11
- const [isFixed, newPlaces] = replace(tokens, {
12
- fix,
13
- rule,
14
- plugin,
15
- });
16
-
17
- fixed.push(isFixed);
18
- places.push(...newPlaces);
19
- }
10
+ const [isFixed, newPlaces] = replace(tokens, {
11
+ fix,
12
+ rule,
13
+ plugin,
14
+ });
15
+
16
+ fixed.push(isFixed);
17
+ places.push(...newPlaces);
20
18
  }
21
19
 
22
20
  if (!fixed.filter(Boolean).length)
@@ -15,7 +15,12 @@ export const isStringLiteral = ({type}) => type === 'StringLiteral';
15
15
  export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
16
16
  export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
17
17
  export const notWhiteSpace = (a) => !isWhiteSpace(a);
18
- export const isPunctuator = ({type}) => type === 'Punctuator';
18
+ export const isPunctuator = (token, value) => {
19
+ if (token.type !== 'Punctuator')
20
+ return false;
21
+
22
+ return !value || token.value === value;
23
+ };
19
24
 
20
25
  export const StringLiteral = (value) => ({
21
26
  type: 'StringLiteral',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.21.2",
3
+ "version": "1.23.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",