flatlint 1.25.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 +11 -0
- package/README.md +5 -0
- package/lib/compare/collect-args.js +4 -0
- package/lib/compare/compare.js +2 -2
- package/lib/flatlint.js +1 -3
- package/lib/parser/string-literal.js +14 -4
- package/lib/plugins/add-missing-quote/index.js +5 -0
- package/lib/plugins/add-missing-semicolon/index.js +3 -0
- package/lib/printer/printer.js +11 -0
- package/lib/runner/path.js +29 -5
- package/lib/runner/replacer.js +58 -41
- package/lib/traverser/traverser.js +1 -1
- package/package.json +5 -1
package/ChangeLog
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
+
2025.01.08, v1.27.0
|
|
2
|
+
|
|
3
|
+
feature:
|
|
4
|
+
- a6b1991 flatlint: multiple fixes in one source
|
|
5
|
+
|
|
6
|
+
2025.01.07, v1.26.0
|
|
7
|
+
|
|
8
|
+
feature:
|
|
9
|
+
- 0ba6115 flatlint: add-missing-quote: round brace before quote
|
|
10
|
+
- 0aca502 flatlint: add-missing-round-brace: improve
|
|
11
|
+
|
|
1
12
|
2025.01.06, v1.25.0
|
|
2
13
|
|
|
3
14
|
feature:
|
package/README.md
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
closeRoundBrace,
|
|
3
|
+
isNewLine,
|
|
3
4
|
NOT_OK,
|
|
4
5
|
OK,
|
|
5
6
|
} from '#types';
|
|
@@ -17,6 +18,9 @@ export const collectArgs = ({currentTokenIndex, tokens}) => {
|
|
|
17
18
|
|
|
18
19
|
if (equal(token, closeRoundBrace))
|
|
19
20
|
break;
|
|
21
|
+
|
|
22
|
+
if (isNewLine(token))
|
|
23
|
+
break;
|
|
20
24
|
}
|
|
21
25
|
|
|
22
26
|
return [
|
package/lib/compare/compare.js
CHANGED
|
@@ -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 (
|
|
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,9 +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.map(getValue).join('');
|
|
4
|
+
import {print} from '#printer';
|
|
7
5
|
|
|
8
6
|
export function lint(source, overrides = {}) {
|
|
9
7
|
const {fix = true, plugins: pluginNames = []} = overrides;
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
closeRoundBrace,
|
|
3
|
+
comma,
|
|
4
|
+
Punctuator,
|
|
5
|
+
semicolon,
|
|
6
|
+
StringLiteral,
|
|
7
|
+
} from '#types';
|
|
2
8
|
|
|
3
9
|
export function parseStringLiteral({i, token, tokens}) {
|
|
4
10
|
const {closed} = token;
|
|
@@ -17,15 +23,19 @@ export function parseStringLiteral({i, token, tokens}) {
|
|
|
17
23
|
let count = 0;
|
|
18
24
|
|
|
19
25
|
if (value.endsWith(';')) {
|
|
20
|
-
const semicolon = Punctuator(';');
|
|
21
|
-
|
|
22
26
|
value = value.slice(0, -1);
|
|
23
27
|
newTokens.push(semicolon);
|
|
24
28
|
++count;
|
|
25
29
|
}
|
|
26
30
|
|
|
31
|
+
if (value.endsWith(',')) {
|
|
32
|
+
value = value.slice(0, -1);
|
|
33
|
+
newTokens.push(comma);
|
|
34
|
+
++count;
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
if (value.endsWith(')')) {
|
|
28
|
-
const brace =
|
|
38
|
+
const brace = closeRoundBrace;
|
|
29
39
|
|
|
30
40
|
value = value.slice(0, -1);
|
|
31
41
|
const {length} = newTokens;
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
export const report = () => 'Add missing quote';
|
|
2
2
|
|
|
3
|
+
export const match = () => ({
|
|
4
|
+
'__a("__b': (vars, path) => !path.isNextPunctuator(vars.quote),
|
|
5
|
+
});
|
|
6
|
+
|
|
3
7
|
export const replace = () => ({
|
|
4
8
|
'const __a = "__b;': 'const __a = "__b";',
|
|
5
9
|
'__a("__b)': '__a("__b")',
|
|
10
|
+
'__a("__b': '__a("__b")',
|
|
6
11
|
});
|
package/lib/runner/path.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import {
|
|
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,12 +17,26 @@ export const createPath = ({tokens, start, end}) => ({
|
|
|
13
17
|
tokens,
|
|
14
18
|
end,
|
|
15
19
|
}),
|
|
16
|
-
|
|
20
|
+
isPrevIdentifier: createIsPrevIdentifier({
|
|
21
|
+
tokens,
|
|
22
|
+
start,
|
|
23
|
+
}),
|
|
24
|
+
isEndsWithPunctuator: createIsCurrentPunctuator({
|
|
25
|
+
tokens,
|
|
26
|
+
end,
|
|
27
|
+
}),
|
|
28
|
+
isCurrentPunctuator: createIsCurrentPunctuator({
|
|
17
29
|
tokens,
|
|
18
30
|
end,
|
|
19
31
|
}),
|
|
20
32
|
});
|
|
21
33
|
|
|
34
|
+
const createIsCurrentPunctuator = ({tokens, end}) => (punctuator) => {
|
|
35
|
+
const current = tokens[end - 1];
|
|
36
|
+
|
|
37
|
+
return isPunctuator(current, punctuator);
|
|
38
|
+
};
|
|
39
|
+
|
|
22
40
|
const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
|
|
23
41
|
const current = tokens[end];
|
|
24
42
|
|
|
@@ -28,9 +46,15 @@ const createIsNextPunctuator = ({tokens, end}) => (punctuator) => {
|
|
|
28
46
|
return isPunctuator(current, punctuator);
|
|
29
47
|
};
|
|
30
48
|
|
|
31
|
-
const
|
|
32
|
-
const
|
|
33
|
-
|
|
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);
|
|
34
58
|
};
|
|
35
59
|
|
|
36
60
|
const createGetAllPrev = ({tokens, start}) => function*() {
|
package/lib/runner/replacer.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import debug from 'debug';
|
|
1
2
|
import {compare} from '#compare';
|
|
2
3
|
import {prepare} from '#parser';
|
|
3
4
|
import {traverse} from '#traverser';
|
|
@@ -14,62 +15,78 @@ import {collectArgs} from '../compare/collect-args.js';
|
|
|
14
15
|
|
|
15
16
|
const returns = (a) => () => a;
|
|
16
17
|
const {entries} = Object;
|
|
18
|
+
const log = debug('flatlint');
|
|
17
19
|
|
|
18
20
|
export const replace = (tokens, {fix, rule, plugin}) => {
|
|
19
21
|
const places = [];
|
|
20
22
|
let isFixed = false;
|
|
21
23
|
const match = plugin.match?.() ?? returns({});
|
|
22
24
|
|
|
23
|
-
for (
|
|
24
|
-
|
|
25
|
+
for (const [from, to] of entries(plugin.replace())) {
|
|
26
|
+
let index = 0;
|
|
25
27
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
const matchFn = match[from];
|
|
37
|
-
|
|
38
|
-
if (matchFn) {
|
|
39
|
-
const is = matchFn(values, createPath({
|
|
40
|
-
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,
|
|
41
38
|
start,
|
|
42
39
|
end,
|
|
43
|
-
|
|
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
|
+
}
|
|
44
57
|
|
|
45
|
-
if (
|
|
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
|
+
|
|
46
74
|
continue;
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
if (fix) {
|
|
50
|
-
to = prepare(to);
|
|
51
|
-
const waysTo = findVarsWays(to);
|
|
75
|
+
}
|
|
52
76
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
77
|
+
const {line, column} = tokens[start];
|
|
78
|
+
const message = plugin.report();
|
|
79
|
+
|
|
80
|
+
places.push({
|
|
81
|
+
rule,
|
|
82
|
+
message,
|
|
83
|
+
line,
|
|
84
|
+
column,
|
|
57
85
|
});
|
|
58
86
|
|
|
59
|
-
|
|
60
|
-
isFixed =
|
|
61
|
-
continue;
|
|
87
|
+
index = end;
|
|
88
|
+
isFixed = false;
|
|
62
89
|
}
|
|
63
|
-
|
|
64
|
-
const {line, column} = tokens[start];
|
|
65
|
-
const message = plugin.report();
|
|
66
|
-
|
|
67
|
-
places.push({
|
|
68
|
-
rule,
|
|
69
|
-
message,
|
|
70
|
-
line,
|
|
71
|
-
column,
|
|
72
|
-
});
|
|
73
90
|
}
|
|
74
91
|
|
|
75
92
|
return [isFixed, places];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flatlint",
|
|
3
|
-
"version": "1.
|
|
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
|
},
|
|
@@ -76,6 +79,7 @@
|
|
|
76
79
|
},
|
|
77
80
|
"dependencies": {
|
|
78
81
|
"@putout/engine-loader": "^15.0.1",
|
|
82
|
+
"debug": "^4.4.0",
|
|
79
83
|
"js-tokens": "^9.0.1",
|
|
80
84
|
"putout": "^37.0.0"
|
|
81
85
|
},
|