flatlint 1.22.0 → 1.24.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.24.0
2
+
3
+ feature:
4
+ - b6252d0 flatlint: path: getPrev -> getAllPrev
5
+
6
+ 2025.01.05, v1.23.0
7
+
8
+ feature:
9
+ - a38f979 flatlint: replacer: improve match
10
+
1
11
  2025.01.04, v1.22.0
2
12
 
3
13
  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
-
package/lib/flatlint.js CHANGED
@@ -2,6 +2,9 @@ import {loadPlugins} from '@putout/engine-loader';
2
2
  import {parse} from '#parser';
3
3
  import {run} from '#runner';
4
4
 
5
+ const getValue = (token) => token.value;
6
+ const print = (tokens) => tokens.map(getValue).join('');
7
+
5
8
  export function lint(source, overrides = {}) {
6
9
  const {fix = true, plugins: pluginNames = []} = overrides;
7
10
  const plugins = loadPlugins({
@@ -23,9 +26,3 @@ export function lint(source, overrides = {}) {
23
26
  places,
24
27
  ];
25
28
  }
26
-
27
- function print(tokens) {
28
- return tokens
29
- .map((token) => token.value)
30
- .join('');
31
- }
@@ -1,19 +1,22 @@
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
+ for (const token of path.getAllNext()) {
17
+ if (isPunctuator(token))
18
+ return false;
19
+ }
20
+
21
+ return true;
22
+ }
@@ -4,7 +4,7 @@ export const report = () => 'Remove useless coma';
4
4
 
5
5
  export const match = () => ({
6
6
  '__a(__args) {},': (vars, path) => {
7
- for (const token of path.getPrev()) {
7
+ for (const token of path.getAllPrev()) {
8
8
  if (isIdentifier(token, 'class'))
9
9
  return true;
10
10
  }
@@ -4,7 +4,7 @@ export const report = () => 'Remove useless semicolon';
4
4
 
5
5
  export const match = () => ({
6
6
  '__a: __expr;': (vars, path) => {
7
- for (const token of path.getPrev()) {
7
+ for (const token of path.getAllPrev()) {
8
8
  if (isIdentifier(token, 'interface'))
9
9
  return false;
10
10
  }
@@ -1,33 +1,59 @@
1
- import {isNewLine} from '#types';
1
+ import {isNewLine, isPunctuator} from '#types';
2
2
 
3
- export const createPath = ({tokens, start}) => ({
4
- getPrev: createGetPrev({
3
+ export const createPath = ({tokens, start, end}) => ({
4
+ getAllPrev: createGetAllPrev({
5
5
  tokens,
6
6
  start,
7
7
  }),
8
- getNext: createGetNext({
8
+ getAllNext: createGetAllNext({
9
9
  tokens,
10
- start,
10
+ end,
11
+ }),
12
+ isNextPunctuator: createIsNextPunctuator({
13
+ tokens,
14
+ end,
15
+ }),
16
+ isEndsWithPunctuator: createIsEndsWithPunctuator({
17
+ tokens,
18
+ end,
19
+ }),
20
+ isNext: createIsNext({
21
+ tokens,
22
+ end,
11
23
  }),
12
24
  });
13
25
 
14
- function createGetPrev({tokens, start}) {
15
- return function*() {
16
- for (let i = start; i >= 0; --i) {
17
- yield tokens[i];
18
- }
19
- };
20
- }
26
+ const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
27
+ const current = tokens[end];
28
+
29
+ if (!current)
30
+ return false;
31
+
32
+ return isPunctuator(current, punctuator);
33
+ };
34
+
35
+ const createIsEndsWithPunctuator = ({tokens, end}) => (punctuator) => {
36
+ const current = tokens[end - 1];
37
+ return isPunctuator(current, punctuator);
38
+ };
39
+
40
+ const createIsNext = ({tokens, end}) => () => {
41
+ return Boolean(tokens[end]);
42
+ };
43
+
44
+ const createGetAllPrev = ({tokens, start}) => function*() {
45
+ for (let i = start; i >= 0; --i) {
46
+ yield tokens[i];
47
+ }
48
+ };
21
49
 
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
- }
50
+ const createGetAllNext = ({tokens, end}) => function*() {
51
+ for (let i = end; i < tokens.length; ++i) {
52
+ const current = tokens[i];
53
+
54
+ if (isNewLine(current))
55
+ continue;
56
+
57
+ yield current;
58
+ }
59
+ };
@@ -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
+
140
+ return getValues(current, waysFrom);
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,11 +15,16 @@ 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 = (token, value) => {
18
+ export const isPunctuator = (token, punctuator) => {
19
19
  if (token.type !== 'Punctuator')
20
20
  return false;
21
21
 
22
- return !value || token.value === value;
22
+ if (!punctuator)
23
+ return true;
24
+
25
+ const punctuatorValue = punctuator.value || punctuator;
26
+
27
+ return token.value === punctuatorValue;
23
28
  };
24
29
 
25
30
  export const StringLiteral = (value) => ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flatlint",
3
- "version": "1.22.0",
3
+ "version": "1.24.0",
4
4
  "description": "JavaScript tokens-based linter",
5
5
  "main": "lib/flatlint.js",
6
6
  "type": "module",