flatlint 1.26.0 → 1.27.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.08, v1.27.0
2
+
3
+ feature:
4
+ - a6b1991 flatlint: multiple fixes in one source
5
+
1
6
  2025.01.07, v1.26.0
2
7
 
3
8
  feature:
@@ -15,7 +15,7 @@ import {
15
15
  equalStr,
16
16
  } from './equal.js';
17
17
 
18
- export const compare = (source, template) => {
18
+ export const compare = (source, template, {index = 0} = {}) => {
19
19
  const templateTokens = prepare(template);
20
20
  const tokens = prepare(source);
21
21
 
@@ -27,7 +27,7 @@ export const compare = (source, template) => {
27
27
  let delta = 0;
28
28
  let skip = 0;
29
29
 
30
- for (let index = 0; index < n; index++) {
30
+ for (; index < n; index++) {
31
31
  for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
32
32
  let currentTokenIndex = index + templateIndex - skip;
33
33
 
package/lib/flatlint.js CHANGED
@@ -1,11 +1,7 @@
1
1
  import {loadPlugins} from '@putout/engine-loader';
2
2
  import {parse} from '#parser';
3
3
  import {run} from '#runner';
4
-
5
- const getValue = (token) => token.value;
6
- const print = (tokens) => tokens
7
- .map(getValue)
8
- .join('');
4
+ import {print} from '#printer';
9
5
 
10
6
  export function lint(source, overrides = {}) {
11
7
  const {fix = true, plugins: pluginNames = []} = overrides;
@@ -20,6 +20,9 @@ function check(vars, path) {
20
20
  if (path.isEndsWithPunctuator(comma))
21
21
  return false;
22
22
 
23
+ if (path.isPrevIdentifier('function'))
24
+ return false;
25
+
23
26
  for (const token of path.getAllNext()) {
24
27
  if (isPunctuator(token, semicolon))
25
28
  return false;
@@ -0,0 +1,11 @@
1
+ const isString = (a) => typeof a === 'string';
2
+ const getValue = (token) => token.value;
3
+
4
+ export const print = (tokens) => {
5
+ if (isString(tokens))
6
+ return tokens;
7
+
8
+ return tokens
9
+ .map(getValue)
10
+ .join('');
11
+ };
@@ -1,4 +1,8 @@
1
- import {isNewLine, isPunctuator} from '#types';
1
+ import {
2
+ isIdentifier,
3
+ isNewLine,
4
+ isPunctuator,
5
+ } from '#types';
2
6
 
3
7
  export const createPath = ({tokens, start, end}) => ({
4
8
  getAllPrev: createGetAllPrev({
@@ -13,6 +17,10 @@ export const createPath = ({tokens, start, end}) => ({
13
17
  tokens,
14
18
  end,
15
19
  }),
20
+ isPrevIdentifier: createIsPrevIdentifier({
21
+ tokens,
22
+ start,
23
+ }),
16
24
  isEndsWithPunctuator: createIsCurrentPunctuator({
17
25
  tokens,
18
26
  end,
@@ -38,6 +46,17 @@ const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
38
46
  return isPunctuator(current, punctuator);
39
47
  };
40
48
 
49
+ const createIsPrevIdentifier = ({tokens, start}) => (value) => {
50
+ const SPACE = 1;
51
+ const FUNCTION = 1;
52
+ const current = tokens[start - (FUNCTION + SPACE)];
53
+
54
+ if (!current)
55
+ return false;
56
+
57
+ return isIdentifier(current, value);
58
+ };
59
+
41
60
  const createGetAllPrev = ({tokens, start}) => function*() {
42
61
  for (let i = start; i >= 0; --i) {
43
62
  yield tokens[i];
@@ -22,57 +22,71 @@ export const replace = (tokens, {fix, rule, plugin}) => {
22
22
  let isFixed = false;
23
23
  const match = plugin.match?.() ?? returns({});
24
24
 
25
- for (let [from, to] of entries(plugin.replace())) {
26
- const [ok, start, end] = compare(tokens, from);
25
+ for (const [from, to] of entries(plugin.replace())) {
26
+ let index = 0;
27
27
 
28
- if (!ok)
29
- continue;
30
-
31
- const values = getCurrentValues({
32
- from,
33
- start,
34
- end,
35
- tokens,
36
- });
37
-
38
- const matchFn = match[from];
39
-
40
- if (matchFn) {
41
- const is = matchFn(values, createPath({
42
- tokens,
28
+ while (index < tokens.length - 1) {
29
+ const [ok, start, end] = compare(tokens, from, {
30
+ index,
31
+ });
32
+
33
+ if (!ok)
34
+ break;
35
+
36
+ const values = getCurrentValues({
37
+ from,
43
38
  start,
44
39
  end,
45
- }));
40
+ tokens,
41
+ });
42
+
43
+ const matchFn = match[from];
44
+
45
+ if (matchFn) {
46
+ const is = matchFn(values, createPath({
47
+ tokens,
48
+ start,
49
+ end,
50
+ }));
51
+
52
+ if (!is) {
53
+ index = end;
54
+ continue;
55
+ }
56
+ }
46
57
 
47
- if (!is)
58
+ if (fix) {
59
+ log(`${rule}: ${from} -> ${to}`);
60
+
61
+ const preparedTo = prepare(to);
62
+ const waysTo = findVarsWays(preparedTo);
63
+
64
+ setValues({
65
+ values,
66
+ waysTo,
67
+ to: preparedTo,
68
+ });
69
+
70
+ tokens.splice(start, end - start, ...preparedTo);
71
+ isFixed = true;
72
+ index = end;
73
+
48
74
  continue;
49
- }
50
-
51
- if (fix) {
52
- log(`${rule}: ${from} -> ${to}`);
53
- to = prepare(to);
54
- const waysTo = findVarsWays(to);
75
+ }
55
76
 
56
- setValues({
57
- values,
58
- waysTo,
59
- to,
77
+ const {line, column} = tokens[start];
78
+ const message = plugin.report();
79
+
80
+ places.push({
81
+ rule,
82
+ message,
83
+ line,
84
+ column,
60
85
  });
61
86
 
62
- tokens.splice(start, end - start, ...to);
63
- isFixed = true;
64
- continue;
87
+ index = end;
88
+ isFixed = false;
65
89
  }
66
-
67
- const {line, column} = tokens[start];
68
- const message = plugin.report();
69
-
70
- places.push({
71
- rule,
72
- message,
73
- line,
74
- column,
75
- });
76
90
  }
77
91
 
78
92
  return [isFixed, places];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.26.0",
3
+ "version": "1.27.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",
@@ -17,6 +17,9 @@
17
17
  "#parser": {
18
18
  "default": "./lib/parser/parser.js"
19
19
  },
20
+ "#printer": {
21
+ "default": "./lib/printer/printer.js"
22
+ },
20
23
  "#traverser": {
21
24
  "default": "./lib/traverser/traverser.js"
22
25
  },