flatlint 1.3.0 → 1.4.1
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 +10 -0
- package/README.md +9 -0
- package/lib/compare/compare.js +26 -1
- 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 +5 -1
- package/lib/tokenizer/index.js +43 -4
- package/lib/traverser/traverser.js +15 -2
- package/lib/{compare → types}/types.js +13 -0
- package/package.json +4 -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
|
-
|
|
6
|
+
isQuote,
|
|
7
|
+
isStringLiteral,
|
|
8
|
+
} from '../types/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/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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {compare} from '../compare/compare.js';
|
|
2
2
|
import {traverse} from '../traverser/traverser.js';
|
|
3
|
-
import {is} from '../
|
|
3
|
+
import {is} from '../types/types.js';
|
|
4
4
|
import {prepare} from '../tokenizer/index.js';
|
|
5
5
|
|
|
6
6
|
const {entries} = Object;
|
|
@@ -66,6 +66,10 @@ function findVarsWays(tokens) {
|
|
|
66
66
|
if (is(value))
|
|
67
67
|
ways[value] = index;
|
|
68
68
|
},
|
|
69
|
+
StringLiteral({value, index}) {
|
|
70
|
+
if (is(value))
|
|
71
|
+
ways[value] = index;
|
|
72
|
+
},
|
|
69
73
|
});
|
|
70
74
|
|
|
71
75
|
return ways;
|
package/lib/tokenizer/index.js
CHANGED
|
@@ -1,13 +1,52 @@
|
|
|
1
1
|
import tokenize from 'js-tokens';
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
isNewLine,
|
|
4
|
+
isStringLiteral,
|
|
5
|
+
Punctuator,
|
|
6
|
+
StringLiteral,
|
|
7
|
+
} from '#types';
|
|
3
8
|
|
|
4
9
|
const isString = (a) => typeof a === 'string';
|
|
5
10
|
|
|
11
|
+
const closeQuotes = (tokens) => {
|
|
12
|
+
const n = tokens.length;
|
|
13
|
+
|
|
14
|
+
for (let i = 0; i < n; i++) {
|
|
15
|
+
const token = tokens[i];
|
|
16
|
+
|
|
17
|
+
if (isStringLiteral(token)) {
|
|
18
|
+
const {closed, value} = token;
|
|
19
|
+
|
|
20
|
+
const quote = Punctuator(value.at(0));
|
|
21
|
+
const newTokens = [];
|
|
22
|
+
|
|
23
|
+
if (closed) {
|
|
24
|
+
const literal = StringLiteral(value.slice(1, -1));
|
|
25
|
+
newTokens.push(quote, literal, quote);
|
|
26
|
+
} else if (value.endsWith(';')) {
|
|
27
|
+
const literal = StringLiteral(value.slice(1, -1));
|
|
28
|
+
const semicolon = Punctuator(';');
|
|
29
|
+
|
|
30
|
+
newTokens.push(quote, literal, semicolon);
|
|
31
|
+
} else {
|
|
32
|
+
const literal = StringLiteral(value.slice(1));
|
|
33
|
+
newTokens.push(quote, literal);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
tokens.splice(i, 1, ...newTokens);
|
|
37
|
+
++i;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
|
|
6
42
|
export const prepare = (a) => {
|
|
7
|
-
if (isString(a))
|
|
8
|
-
return
|
|
43
|
+
if (!isString(a))
|
|
44
|
+
return a;
|
|
45
|
+
|
|
46
|
+
const array = Array.from(tokenize(a));
|
|
47
|
+
closeQuotes(array);
|
|
9
48
|
|
|
10
|
-
return
|
|
49
|
+
return array;
|
|
11
50
|
};
|
|
12
51
|
|
|
13
52
|
export const parse = (source) => {
|
|
@@ -1,13 +1,26 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
isIdentifier,
|
|
3
|
+
isStringLiteral,
|
|
4
|
+
} from '../types/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
|
};
|
|
@@ -2,11 +2,22 @@ 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);
|
|
8
9
|
export const isPunctuator = ({type}) => type === 'Punctuator';
|
|
9
10
|
|
|
11
|
+
export const StringLiteral = (value) => ({
|
|
12
|
+
type: 'StringLiteral',
|
|
13
|
+
value,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
export const Punctuator = (value) => ({
|
|
17
|
+
type: 'Punctuator',
|
|
18
|
+
value,
|
|
19
|
+
});
|
|
20
|
+
|
|
10
21
|
export const is = (str, array = ALL) => {
|
|
11
22
|
for (const item of array) {
|
|
12
23
|
if (check(str, item))
|
|
@@ -18,6 +29,7 @@ export const is = (str, array = ALL) => {
|
|
|
18
29
|
|
|
19
30
|
const LINKED_NODE = /^__[a-z]$/;
|
|
20
31
|
const ANY = '__';
|
|
32
|
+
const QUOTE = /^['"]$/;
|
|
21
33
|
|
|
22
34
|
const ALL = [ANY, LINKED_NODE];
|
|
23
35
|
|
|
@@ -29,3 +41,4 @@ function check(str, item) {
|
|
|
29
41
|
}
|
|
30
42
|
|
|
31
43
|
export const isId = (a) => LINKED_NODE.test(a);
|
|
44
|
+
export const isQuote = (a) => QUOTE.test(a);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "flatlint",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.1",
|
|
4
4
|
"description": "JavaScript tokens-based linter",
|
|
5
5
|
"main": "lib/flatlint.js",
|
|
6
6
|
"type": "module",
|
|
@@ -11,6 +11,9 @@
|
|
|
11
11
|
"#test": {
|
|
12
12
|
"default": "./lib/test/test.js"
|
|
13
13
|
},
|
|
14
|
+
"#types": {
|
|
15
|
+
"default": "./lib/types/types.js"
|
|
16
|
+
},
|
|
14
17
|
"#flatlint": {
|
|
15
18
|
"default": "./lib/flatlint.js"
|
|
16
19
|
}
|