flatlint 1.22.0 → 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,8 @@
1
+ 2025.01.05, v1.23.0
2
+
3
+ feature:
4
+ - a38f979 flatlint: replacer: improve match
5
+
1
6
  2025.01.04, v1.22.0
2
7
 
3
8
  feature:
@@ -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
 
@@ -55,8 +55,6 @@ export const compare = (source, template) => {
55
55
  nextTemplateToken: templateTokens[templateIndex + 1],
56
56
  });
57
57
 
58
- debugger;
59
-
60
58
  if (n === indexOfExpressionEnd) {
61
59
  end = indexOfExpressionEnd;
62
60
  continue;
@@ -111,4 +109,3 @@ function compareAll(a, b) {
111
109
 
112
110
  return false;
113
111
  }
114
-
@@ -1,19 +1,23 @@
1
- import {isNewLine, isPunctuator} from '#types';
1
+ import {isPunctuator} from '#types';
2
2
 
3
3
  export const report = () => 'Add missing semicolon';
4
4
 
5
5
  export const match = () => ({
6
- 'const __a = __expr': (vars, path) => {
7
- let last;
8
-
9
- for (const token of path.getNext()) {
10
- last = token;
11
- }
12
-
13
- return !isPunctuator(last, ';');
14
- },
6
+ 'const __a = __expr': check,
7
+ '__a(__args)': check,
15
8
  });
16
9
 
17
10
  export const replace = () => ({
18
11
  'const __a = __expr': 'const __a = __expr;',
12
+ '__a(__args)': '__a(__args);',
19
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
+ }
@@ -15,45 +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
- debugger;
30
- result = fn(values, createPath({
31
- tokens,
32
- start,
33
- end,
34
- }));
35
- }
36
-
37
- return result;
38
- };
39
-
40
18
  export const replace = (tokens, {fix, rule, plugin}) => {
41
19
  const places = [];
42
20
  let isFixed = false;
21
+ const match = plugin.match?.() ?? returns({});
43
22
 
44
- for (const [from, to] of entries(plugin.replace())) {
23
+ for (let [from, to] of entries(plugin.replace())) {
45
24
  const [ok, start, end] = compare(tokens, from);
46
25
 
47
26
  if (!ok)
48
27
  continue;
49
28
 
50
- if (fix) {
51
- runReplace(tokens, {
52
- from,
53
- 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,
54
41
  start,
55
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,
56
57
  });
58
+
59
+ tokens.splice(start, end - start, ...to);
57
60
  isFixed = true;
58
61
  continue;
59
62
  }
@@ -72,26 +75,6 @@ export const replace = (tokens, {fix, rule, plugin}) => {
72
75
  return [isFixed, places];
73
76
  };
74
77
 
75
- function runReplace(tokens, {from, to, start, end}) {
76
- const current = tokens.slice(start, end);
77
-
78
- to = prepare(to);
79
- from = prepare(from);
80
-
81
- const waysFrom = findVarsWays(from);
82
- const values = getValues(current, waysFrom);
83
-
84
- const waysTo = findVarsWays(to);
85
-
86
- setValues({
87
- values,
88
- waysTo,
89
- to,
90
- });
91
-
92
- tokens.splice(start, end - start, ...to);
93
- }
94
-
95
78
  function findVarsWays(tokens) {
96
79
  const ways = {};
97
80
 
@@ -148,3 +131,11 @@ function setValues({to, waysTo, values}) {
148
131
  to.splice(index, 1, ...values[name]);
149
132
  }
150
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)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.22.0",
3
+ "version": "1.23.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",