flatlint 1.3.0 → 1.4.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 +9 -0
- package/lib/compare/compare.js +25 -0
- package/lib/compare/types.js +3 -0
- package/lib/plugins/add-missing-quote/index.js +9 -0
- package/lib/plugins/remove-useless-round-brace/index.js +1 -0
- package/lib/plugins.js +2 -0
- package/lib/runner/replacer.js +4 -0
- package/lib/tokenizer/index.js +48 -4
- package/lib/traverser/traverser.js +15 -2
- package/package.json +1 -1
package/ChangeLog
CHANGED
package/README.md
CHANGED
package/lib/compare/compare.js
CHANGED
|
@@ -3,11 +3,14 @@ import {
|
|
|
3
3
|
isId,
|
|
4
4
|
isIdentifier,
|
|
5
5
|
isPunctuator,
|
|
6
|
+
isQuote,
|
|
7
|
+
isStringLiteral,
|
|
6
8
|
} from './types.js';
|
|
7
9
|
|
|
8
10
|
export const compare = (source, template) => {
|
|
9
11
|
const templateTokens = prepare(template);
|
|
10
12
|
const tokens = prepare(source);
|
|
13
|
+
|
|
11
14
|
const n = tokens.length - 1;
|
|
12
15
|
const templateTokensLength = templateTokens.length;
|
|
13
16
|
let isEqual = false;
|
|
@@ -17,6 +20,12 @@ export const compare = (source, template) => {
|
|
|
17
20
|
for (let index = 0; index < n; index++) {
|
|
18
21
|
for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
|
|
19
22
|
const currentTokenIndex = index + templateIndex;
|
|
23
|
+
|
|
24
|
+
if (currentTokenIndex > n) {
|
|
25
|
+
isEqual = false;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
|
|
20
29
|
const templateToken = templateTokens[templateIndex];
|
|
21
30
|
const currentToken = tokens[currentTokenIndex];
|
|
22
31
|
|
|
@@ -40,7 +49,9 @@ export const compare = (source, template) => {
|
|
|
40
49
|
const comparators = [
|
|
41
50
|
equal,
|
|
42
51
|
equalId,
|
|
52
|
+
equalStr,
|
|
43
53
|
equalAny,
|
|
54
|
+
equalQuote,
|
|
44
55
|
];
|
|
45
56
|
|
|
46
57
|
function compareAll(a, b) {
|
|
@@ -66,6 +77,20 @@ function equalAny(a, b) {
|
|
|
66
77
|
return b.value === '__';
|
|
67
78
|
}
|
|
68
79
|
|
|
80
|
+
function equalQuote(a, b) {
|
|
81
|
+
if (!isPunctuator(a))
|
|
82
|
+
return false;
|
|
83
|
+
|
|
84
|
+
return isQuote(b.value);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function equalStr(a, b) {
|
|
88
|
+
if (!isStringLiteral(a))
|
|
89
|
+
return false;
|
|
90
|
+
|
|
91
|
+
return isId(b.value);
|
|
92
|
+
}
|
|
93
|
+
|
|
69
94
|
function equalId(a, b) {
|
|
70
95
|
if (!isIdentifier(b))
|
|
71
96
|
return false;
|
package/lib/compare/types.js
CHANGED
|
@@ -2,6 +2,7 @@ const isString = (a) => typeof a === 'string';
|
|
|
2
2
|
|
|
3
3
|
export const isWhiteSpace = ({type}) => type === 'WhiteSpace';
|
|
4
4
|
export const isIdentifier = ({type}) => type === 'IdentifierName';
|
|
5
|
+
export const isStringLiteral = ({type}) => type === 'StringLiteral';
|
|
5
6
|
export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
|
|
6
7
|
export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
|
|
7
8
|
export const notWhiteSpace = (a) => !isWhiteSpace(a);
|
|
@@ -18,6 +19,7 @@ export const is = (str, array = ALL) => {
|
|
|
18
19
|
|
|
19
20
|
const LINKED_NODE = /^__[a-z]$/;
|
|
20
21
|
const ANY = '__';
|
|
22
|
+
const QUOTE = /^['"]$/;
|
|
21
23
|
|
|
22
24
|
const ALL = [ANY, LINKED_NODE];
|
|
23
25
|
|
|
@@ -29,3 +31,4 @@ function check(str, item) {
|
|
|
29
31
|
}
|
|
30
32
|
|
|
31
33
|
export const isId = (a) => LINKED_NODE.test(a);
|
|
34
|
+
export const isQuote = (a) => QUOTE.test(a);
|
package/lib/plugins.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import * as wrapAssignmentInParens from './plugins/wrap-assignment-in-parens/index.js';
|
|
2
2
|
import * as addMissingRoundBraces from './plugins/add-missing-round-braces/index.js';
|
|
3
|
+
import * as addMissingQuote from './plugins/add-missing-quote/index.js';
|
|
3
4
|
import * as convertCommaToSemicolon from './plugins/convert-comma-to-semicolon/index.js';
|
|
4
5
|
import * as removeUselessRoundBrace from './plugins/remove-useless-round-brace/index.js';
|
|
5
6
|
|
|
6
7
|
export const plugins = [
|
|
7
8
|
['wrap-assignment-in-parens', wrapAssignmentInParens],
|
|
8
9
|
['add-missing-round-braces', addMissingRoundBraces],
|
|
10
|
+
['add-missing-quote', addMissingQuote],
|
|
9
11
|
['convert-comma-to-semicolon', convertCommaToSemicolon],
|
|
10
12
|
['remove-useless-round-brace', removeUselessRoundBrace],
|
|
11
13
|
];
|
package/lib/runner/replacer.js
CHANGED
package/lib/tokenizer/index.js
CHANGED
|
@@ -1,13 +1,57 @@
|
|
|
1
1
|
import tokenize from 'js-tokens';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isNewLine,
|
|
4
|
+
isStringLiteral,
|
|
5
|
+
} from '../compare/types.js';
|
|
3
6
|
|
|
4
7
|
const isString = (a) => typeof a === 'string';
|
|
5
8
|
|
|
9
|
+
const closeQuotes = (tokens) => {
|
|
10
|
+
const n = tokens.length;
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < n; i++) {
|
|
13
|
+
const token = tokens[i];
|
|
14
|
+
|
|
15
|
+
if (isStringLiteral(token)) {
|
|
16
|
+
const {closed, value} = token;
|
|
17
|
+
|
|
18
|
+
const quote = {
|
|
19
|
+
type: 'Punctuator',
|
|
20
|
+
value: value.at(0),
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const newTokens = [];
|
|
24
|
+
|
|
25
|
+
if (closed) {
|
|
26
|
+
const literal = {
|
|
27
|
+
value: value.slice(1, -1),
|
|
28
|
+
type: 'StringLiteral',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
newTokens.push(quote, literal, quote);
|
|
32
|
+
} else {
|
|
33
|
+
const literal = {
|
|
34
|
+
value: value.slice(1),
|
|
35
|
+
type: 'StringLiteral',
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
newTokens.push(quote, literal);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
tokens.splice(i, 1, ...newTokens);
|
|
42
|
+
++i;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
|
|
6
47
|
export const prepare = (a) => {
|
|
7
|
-
if (isString(a))
|
|
8
|
-
return
|
|
48
|
+
if (!isString(a))
|
|
49
|
+
return a;
|
|
50
|
+
|
|
51
|
+
const array = Array.from(tokenize(a));
|
|
52
|
+
closeQuotes(array);
|
|
9
53
|
|
|
10
|
-
return
|
|
54
|
+
return array;
|
|
11
55
|
};
|
|
12
56
|
|
|
13
57
|
export const parse = (source) => {
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
isIdentifier,
|
|
3
|
+
isStringLiteral,
|
|
4
|
+
} from '../compare/types.js';
|
|
2
5
|
|
|
3
6
|
const maybeCall = (fn, a) => fn?.(a);
|
|
4
7
|
|
|
5
8
|
export const traverse = (tokens, visitors = {}) => {
|
|
6
9
|
for (const [index, token] of tokens.entries()) {
|
|
7
|
-
if (isIdentifier(token))
|
|
10
|
+
if (isIdentifier(token)) {
|
|
8
11
|
maybeCall(visitors.Identifier, {
|
|
9
12
|
...token,
|
|
10
13
|
index,
|
|
11
14
|
});
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (isStringLiteral(token)) {
|
|
19
|
+
maybeCall(visitors.StringLiteral, {
|
|
20
|
+
...token,
|
|
21
|
+
index,
|
|
22
|
+
});
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
12
25
|
}
|
|
13
26
|
};
|