flatlint 1.16.0 → 1.17.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 +5 -0
- package/README.md +11 -0
- package/lib/compare/compare.js +6 -0
- package/lib/plugins/remove-useless-semicolon/index.js +18 -0
- package/lib/runner/path.js +13 -0
- package/lib/runner/replacer.js +27 -0
- package/lib/runner/runner.js +7 -6
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
|
@@ -87,6 +87,17 @@ npm i flatlint
|
|
|
87
87
|
|
|
88
88
|
</details>
|
|
89
89
|
|
|
90
|
+
<details><summary>remove useless semicolon</summary>
|
|
91
|
+
|
|
92
|
+
```diff
|
|
93
|
+
const a = {
|
|
94
|
+
- b: 'hello';
|
|
95
|
+
+ b: 'hello',
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
</details>
|
|
100
|
+
|
|
90
101
|
<details><summary>add missing quote</summary>
|
|
91
102
|
|
|
92
103
|
```diff
|
package/lib/compare/compare.js
CHANGED
|
@@ -27,6 +27,12 @@ export const compare = (source, template) => {
|
|
|
27
27
|
for (let index = 0; index < n; index++) {
|
|
28
28
|
for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
|
|
29
29
|
const currentTokenIndex = index + templateIndex;
|
|
30
|
+
|
|
31
|
+
if (currentTokenIndex > n) {
|
|
32
|
+
isEqual = false;
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
|
|
30
36
|
const templateToken = templateTokens[templateIndex];
|
|
31
37
|
const currentToken = tokens[currentTokenIndex];
|
|
32
38
|
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {isIdentifier} from '#types';
|
|
2
|
+
|
|
3
|
+
export const report = () => 'Remove useless semicolon';
|
|
4
|
+
|
|
5
|
+
export const match = () => ({
|
|
6
|
+
'__a: __expr;': (vars, path) => {
|
|
7
|
+
for (const token of path.getPrev()) {
|
|
8
|
+
if (isIdentifier(token) && token.value === 'interface')
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return true;
|
|
13
|
+
},
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const replace = () => ({
|
|
17
|
+
'__a: __expr;': '__a: __expr,',
|
|
18
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export const createPath = ({tokens, start, end}) => {
|
|
2
|
+
return {
|
|
3
|
+
getPrev: createGetPrev({tokens, start}),
|
|
4
|
+
};
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
function createGetPrev({tokens, start}) {
|
|
8
|
+
return function*() {
|
|
9
|
+
for (let i = start; i >= 0; --i) {
|
|
10
|
+
yield tokens[i];
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
}
|
package/lib/runner/replacer.js
CHANGED
|
@@ -8,9 +8,35 @@ import {
|
|
|
8
8
|
} from '#types';
|
|
9
9
|
import {collectArray} from '../compare/collect-array.js';
|
|
10
10
|
import {collectExpression} from '../compare/collect-expression.js';
|
|
11
|
+
import {createPath} from './path.js';
|
|
11
12
|
|
|
13
|
+
const returns = (a) => () => a;
|
|
12
14
|
const {entries} = Object;
|
|
13
15
|
|
|
16
|
+
export const match = (tokens, {plugin}) => {
|
|
17
|
+
const match = plugin.match ?? returns([]);
|
|
18
|
+
|
|
19
|
+
for (const [from, fn] of entries(match())) {
|
|
20
|
+
const [ok, start, end] = compare(tokens, from);
|
|
21
|
+
|
|
22
|
+
if (!ok)
|
|
23
|
+
continue;
|
|
24
|
+
|
|
25
|
+
const current = tokens.slice(start, end);
|
|
26
|
+
|
|
27
|
+
const waysFrom = findVarsWays(prepare(from));
|
|
28
|
+
const values = getValues(current, waysFrom);
|
|
29
|
+
|
|
30
|
+
return fn(values, createPath({
|
|
31
|
+
tokens,
|
|
32
|
+
start,
|
|
33
|
+
end,
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return true;
|
|
38
|
+
};
|
|
39
|
+
|
|
14
40
|
export const replace = (tokens, {fix, rule, plugin}) => {
|
|
15
41
|
const places = [];
|
|
16
42
|
|
|
@@ -109,3 +135,4 @@ function setValues({to, waysTo, values}) {
|
|
|
109
135
|
to.splice(index, 1, ...values[name]);
|
|
110
136
|
}
|
|
111
137
|
}
|
|
138
|
+
|
package/lib/runner/runner.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import {replace} from './replacer.js';
|
|
1
|
+
import {match, replace} from './replacer.js';
|
|
2
2
|
|
|
3
3
|
export const run = (tokens, {fix, fixCount = 10, plugins}) => {
|
|
4
4
|
const places = [];
|
|
5
5
|
|
|
6
6
|
while (--fixCount >= 0) {
|
|
7
7
|
for (const {rule, plugin} of plugins) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
if (match(tokens, {plugin}))
|
|
9
|
+
places.push(...replace(tokens, {
|
|
10
|
+
fix,
|
|
11
|
+
rule,
|
|
12
|
+
plugin,
|
|
13
|
+
}));
|
|
13
14
|
|
|
14
15
|
if (!fix) {
|
|
15
16
|
fixCount = 0;
|