flatlint 1.0.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/LICENSE +21 -0
- package/README.md +64 -0
- package/example/1.js +1 -0
- package/lib/compare/compare.js +99 -0
- package/lib/compare/types.js +31 -0
- package/lib/plugins/wrap-assignment-in-parens/index.js +9 -0
- package/lib/plugins.js +5 -0
- package/lib/runner/replacer.js +88 -0
- package/lib/runner/runner.js +15 -0
- package/lib/tklint.js +31 -0
- package/lib/tokenizer/index.js +38 -0
- package/lib/traverser/traverser.js +13 -0
- package/lib/with-plugins.js +7 -0
- package/package.json +95 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) coderaiser
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# TKLint
|
|
2
|
+
|
|
3
|
+
Token-based JavaScript linter that fixes Syntax Errors
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
npm i tklint
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## API
|
|
12
|
+
|
|
13
|
+
```js
|
|
14
|
+
import {lint, plugins} from 'tklint/with-plugins';
|
|
15
|
+
|
|
16
|
+
const [code] = tklint(`a && b = c`, {
|
|
17
|
+
plugins,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
// returns
|
|
21
|
+
`
|
|
22
|
+
a && (b = c);
|
|
23
|
+
`;
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Without `fix`:
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import {lint, plugins} from 'tklint/with-plugins';
|
|
30
|
+
|
|
31
|
+
const [, places] = tklint(`a && b = c`, {
|
|
32
|
+
fix: false,
|
|
33
|
+
plugins,
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// returns
|
|
37
|
+
[{
|
|
38
|
+
column: 1,
|
|
39
|
+
line: 1,
|
|
40
|
+
message: `Wrap the assignment in parentheses after '&&'`,
|
|
41
|
+
rule: 'wrap-assignment-in-parens',
|
|
42
|
+
}];
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
When you want to use custom plugins:
|
|
46
|
+
|
|
47
|
+
```js
|
|
48
|
+
import {lint} from 'tklint';
|
|
49
|
+
|
|
50
|
+
const [code] = lint(`a && b = c`, {
|
|
51
|
+
plugins: [
|
|
52
|
+
['wrap-assignment-in-parens', {
|
|
53
|
+
report: () => `Wrap the assignment in parentheses after '&&'`,
|
|
54
|
+
replace: () => ({
|
|
55
|
+
'__a && __b = __c': '__a && (__b = __c)',
|
|
56
|
+
}),
|
|
57
|
+
}],
|
|
58
|
+
],
|
|
59
|
+
});
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## License
|
|
63
|
+
|
|
64
|
+
MIT
|
package/example/1.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import {prepare} from '../tokenizer/index.js';
|
|
2
|
+
import {
|
|
3
|
+
isId,
|
|
4
|
+
isIdentifier,
|
|
5
|
+
isNewLine,
|
|
6
|
+
isPunctuator,
|
|
7
|
+
isWhiteSpace,
|
|
8
|
+
} from './types.js';
|
|
9
|
+
|
|
10
|
+
export const compare = (source, template) => {
|
|
11
|
+
const templateTokens = prepare(template);
|
|
12
|
+
const tokens = prepare(source);
|
|
13
|
+
const n = tokens.length - 1;
|
|
14
|
+
const templateTokensLength = templateTokens.length;
|
|
15
|
+
let isEqual = false;
|
|
16
|
+
let start = 0;
|
|
17
|
+
let end = 0;
|
|
18
|
+
|
|
19
|
+
for (let index = 0; index < n; index++) {
|
|
20
|
+
let tokenDelta = 0;
|
|
21
|
+
let templateDelta = 0;
|
|
22
|
+
|
|
23
|
+
for (let templateIndex = 0; templateIndex < templateTokensLength; templateIndex++) {
|
|
24
|
+
const currentTokenIndex = index + templateIndex - tokenDelta;
|
|
25
|
+
|
|
26
|
+
if (currentTokenIndex === n + 1)
|
|
27
|
+
break;
|
|
28
|
+
|
|
29
|
+
const templateToken = templateTokens[templateIndex - templateDelta];
|
|
30
|
+
const currentToken = tokens[currentTokenIndex];
|
|
31
|
+
|
|
32
|
+
if (isWhiteSpace(currentToken)) {
|
|
33
|
+
++templateDelta;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
if (isWhiteSpace(templateToken)) {
|
|
38
|
+
++tokenDelta;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (isNewLine(currentToken))
|
|
43
|
+
continue;
|
|
44
|
+
|
|
45
|
+
if (!compareAll(currentToken, templateToken)) {
|
|
46
|
+
isEqual = false;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
isEqual = true;
|
|
51
|
+
start = index;
|
|
52
|
+
end = currentTokenIndex + tokenDelta + templateDelta;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (isEqual)
|
|
56
|
+
return [true, start, ++end];
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return [false];
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const comparators = [
|
|
63
|
+
equal,
|
|
64
|
+
equalId,
|
|
65
|
+
equalAny,
|
|
66
|
+
];
|
|
67
|
+
|
|
68
|
+
function compareAll(a, b) {
|
|
69
|
+
for (const currentCompare of comparators) {
|
|
70
|
+
if (currentCompare(a, b))
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function equal(a, b) {
|
|
78
|
+
return a.type === b.type && a.value === b.value;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function equalAny(a, b) {
|
|
82
|
+
if (!isIdentifier(b))
|
|
83
|
+
return false;
|
|
84
|
+
|
|
85
|
+
if (isPunctuator(a))
|
|
86
|
+
return false;
|
|
87
|
+
|
|
88
|
+
return b.value === '__';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function equalId(a, b) {
|
|
92
|
+
if (!isIdentifier(b))
|
|
93
|
+
return false;
|
|
94
|
+
|
|
95
|
+
if (isPunctuator(a))
|
|
96
|
+
return false;
|
|
97
|
+
|
|
98
|
+
return isId(b.value);
|
|
99
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const isString = (a) => typeof a === 'string';
|
|
2
|
+
|
|
3
|
+
export const isWhiteSpace = ({type}) => type === 'WhiteSpace';
|
|
4
|
+
export const isIdentifier = ({type}) => type === 'IdentifierName';
|
|
5
|
+
export const isNumericLiteral = ({type}) => type === 'NumericLiteral';
|
|
6
|
+
export const isNewLine = ({type}) => type === 'LineTerminatorSequence';
|
|
7
|
+
export const notWhiteSpace = (a) => !isWhiteSpace(a);
|
|
8
|
+
export const isPunctuator = ({type}) => type === 'Punctuator';
|
|
9
|
+
|
|
10
|
+
export const is = (str, array = ALL) => {
|
|
11
|
+
for (const item of array) {
|
|
12
|
+
if (check(str, item))
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
return false;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const LINKED_NODE = /^__[a-z]$/;
|
|
20
|
+
const ANY = '__';
|
|
21
|
+
|
|
22
|
+
const ALL = [ANY, LINKED_NODE];
|
|
23
|
+
|
|
24
|
+
function check(str, item) {
|
|
25
|
+
if (isString(item))
|
|
26
|
+
return str === item;
|
|
27
|
+
|
|
28
|
+
return item.test(str);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const isId = (a) => LINKED_NODE.test(a);
|
package/lib/plugins.js
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import {compare} from '../compare/compare.js';
|
|
2
|
+
import {traverse} from '../traverser/traverser.js';
|
|
3
|
+
import {is} from '../compare/types.js';
|
|
4
|
+
import {prepare} from '../tokenizer/index.js';
|
|
5
|
+
|
|
6
|
+
const {entries} = Object;
|
|
7
|
+
|
|
8
|
+
export const replace = (tokens, {fix, rule, plugin}) => {
|
|
9
|
+
const places = [];
|
|
10
|
+
|
|
11
|
+
for (const [from, to] of entries(plugin.replace())) {
|
|
12
|
+
const [ok, start, end] = compare(tokens, from);
|
|
13
|
+
|
|
14
|
+
if (!ok)
|
|
15
|
+
continue;
|
|
16
|
+
|
|
17
|
+
if (fix) {
|
|
18
|
+
runReplace(tokens, {
|
|
19
|
+
from,
|
|
20
|
+
to,
|
|
21
|
+
start,
|
|
22
|
+
end,
|
|
23
|
+
});
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const {line, column} = tokens[start];
|
|
28
|
+
const message = plugin.report();
|
|
29
|
+
|
|
30
|
+
places.push({
|
|
31
|
+
rule,
|
|
32
|
+
message,
|
|
33
|
+
line,
|
|
34
|
+
column,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return places;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
function runReplace(tokens, {from, to, start, end}) {
|
|
42
|
+
const current = tokens.slice(start, end);
|
|
43
|
+
|
|
44
|
+
to = prepare(to);
|
|
45
|
+
from = prepare(from);
|
|
46
|
+
|
|
47
|
+
const waysFrom = findVarsWays(from);
|
|
48
|
+
const values = getValues(current, waysFrom);
|
|
49
|
+
|
|
50
|
+
const waysTo = findVarsWays(to);
|
|
51
|
+
|
|
52
|
+
setValues({
|
|
53
|
+
values,
|
|
54
|
+
waysTo,
|
|
55
|
+
to,
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
tokens.splice(start, end - start, ...to);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function findVarsWays(tokens) {
|
|
62
|
+
const ways = {};
|
|
63
|
+
|
|
64
|
+
traverse(tokens, {
|
|
65
|
+
Identifier({value, index}) {
|
|
66
|
+
if (is(value))
|
|
67
|
+
ways[value] = index;
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
return ways;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function getValues(tokens, waysFrom) {
|
|
75
|
+
const values = {};
|
|
76
|
+
|
|
77
|
+
for (const [name, index] of entries(waysFrom)) {
|
|
78
|
+
values[name] = tokens[index];
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return values;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function setValues({to, waysTo, values}) {
|
|
85
|
+
for (const [name, index] of entries(waysTo)) {
|
|
86
|
+
to[index] = values[name];
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {replace} from './replacer.js';
|
|
2
|
+
|
|
3
|
+
export const run = (tokens, {fix, plugins}) => {
|
|
4
|
+
const places = [];
|
|
5
|
+
|
|
6
|
+
for (const {rule, plugin} of plugins) {
|
|
7
|
+
places.push(...replace(tokens, {
|
|
8
|
+
fix,
|
|
9
|
+
rule,
|
|
10
|
+
plugin,
|
|
11
|
+
}));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return [places];
|
|
15
|
+
};
|
package/lib/tklint.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {loadPlugins} from '@putout/engine-loader';
|
|
2
|
+
import {run} from './runner/runner.js';
|
|
3
|
+
import {parse} from './tokenizer/index.js';
|
|
4
|
+
|
|
5
|
+
export function lint(source, overrides = {}) {
|
|
6
|
+
const {fix = true, plugins: pluginNames = []} = overrides;
|
|
7
|
+
const plugins = loadPlugins({
|
|
8
|
+
pluginNames,
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
const tokens = parse(source);
|
|
12
|
+
|
|
13
|
+
const [places] = run(tokens, {
|
|
14
|
+
plugins,
|
|
15
|
+
fix,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
if (!fix)
|
|
19
|
+
return [source, places];
|
|
20
|
+
|
|
21
|
+
return [
|
|
22
|
+
print(tokens),
|
|
23
|
+
places,
|
|
24
|
+
];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function print(tokens) {
|
|
28
|
+
return tokens
|
|
29
|
+
.map((token) => token.value)
|
|
30
|
+
.join('');
|
|
31
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import tokenize from 'js-tokens';
|
|
2
|
+
import {isNewLine} from '../compare/types.js';
|
|
3
|
+
|
|
4
|
+
const isString = (a) => typeof a === 'string';
|
|
5
|
+
|
|
6
|
+
export const prepare = (a) => {
|
|
7
|
+
if (isString(a))
|
|
8
|
+
return Array.from(tokenize(a));
|
|
9
|
+
|
|
10
|
+
return a;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const parse = (source) => {
|
|
14
|
+
return getTokensWithLocation(prepare(source));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function getTokensWithLocation(tokens) {
|
|
18
|
+
let line = 1;
|
|
19
|
+
let column = 1;
|
|
20
|
+
const result = [];
|
|
21
|
+
|
|
22
|
+
for (const token of tokens) {
|
|
23
|
+
if (isNewLine(token)) {
|
|
24
|
+
++line;
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
result.push({
|
|
29
|
+
...token,
|
|
30
|
+
line,
|
|
31
|
+
column,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
column += token.value.length;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {isIdentifier} from '../compare/types.js';
|
|
2
|
+
|
|
3
|
+
const maybeCall = (fn, a) => fn?.(a);
|
|
4
|
+
|
|
5
|
+
export const traverse = (tokens, visitors = {}) => {
|
|
6
|
+
for (const [index, token] of tokens.entries()) {
|
|
7
|
+
if (isIdentifier(token))
|
|
8
|
+
maybeCall(visitors.Identifier, {
|
|
9
|
+
...token,
|
|
10
|
+
index,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "flatlint",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "JavaScript tokens-based linter",
|
|
5
|
+
"main": "lib/flatlint.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
"./with-plugins": "./lib/with-plugins.js"
|
|
9
|
+
},
|
|
10
|
+
"imports": {
|
|
11
|
+
"#test": {
|
|
12
|
+
"default": "./lib/test/test.js"
|
|
13
|
+
},
|
|
14
|
+
"#flatlint": {
|
|
15
|
+
"default": "./lib/flatlint.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"lint": "madrun lint",
|
|
20
|
+
"fresh:lint": "madrun fresh:lint",
|
|
21
|
+
"lint:fresh": "madrun lint:fresh",
|
|
22
|
+
"fix:lint": "madrun fix:lint",
|
|
23
|
+
"test": "madrun test",
|
|
24
|
+
"watch:test": "madrun watch:test",
|
|
25
|
+
"watch:tape": "madrun watch:tape",
|
|
26
|
+
"watch:lint": "madrun watch:lint",
|
|
27
|
+
"watcher": "madrun watcher",
|
|
28
|
+
"coverage": "madrun coverage",
|
|
29
|
+
"report": "madrun report"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/coderaiser/flatlint.git"
|
|
34
|
+
},
|
|
35
|
+
"keywords": [
|
|
36
|
+
"flatlint",
|
|
37
|
+
"scripts",
|
|
38
|
+
"package",
|
|
39
|
+
"npm",
|
|
40
|
+
"npm run",
|
|
41
|
+
"npm-scripts",
|
|
42
|
+
"tool",
|
|
43
|
+
"cli",
|
|
44
|
+
"command",
|
|
45
|
+
"task",
|
|
46
|
+
"parallel",
|
|
47
|
+
"serial",
|
|
48
|
+
"run",
|
|
49
|
+
"tool",
|
|
50
|
+
"commandline",
|
|
51
|
+
"command"
|
|
52
|
+
],
|
|
53
|
+
"author": "coderaiser <mnemonic.enemy@gmail.com> (http://coderaiser.github.io/)",
|
|
54
|
+
"bugs": {
|
|
55
|
+
"url": "https://github.com/coderaiser/flatlint/issues"
|
|
56
|
+
},
|
|
57
|
+
"homepage": "https://github.com/coderaiser/flatlint",
|
|
58
|
+
"license": "MIT",
|
|
59
|
+
"engines": {
|
|
60
|
+
"node": ">=18"
|
|
61
|
+
},
|
|
62
|
+
"dependencies": {
|
|
63
|
+
"@putout/engine-loader": "^15.0.1",
|
|
64
|
+
"@putout/formatter-dump": "^5.0.0",
|
|
65
|
+
"all-object-keys": "^2.0.0",
|
|
66
|
+
"enquirer": "^2.3.6",
|
|
67
|
+
"find-up": "^7.0.0",
|
|
68
|
+
"jessy": "^3.0.0",
|
|
69
|
+
"js-tokens": "^9.0.1",
|
|
70
|
+
"mapsome": "^1.0.0",
|
|
71
|
+
"montag": "^1.0.0",
|
|
72
|
+
"once": "^1.4.0",
|
|
73
|
+
"putout": "^37.0.0",
|
|
74
|
+
"try-catch": "^3.0.0",
|
|
75
|
+
"try-to-catch": "^3.0.0",
|
|
76
|
+
"yargs-parser": "^21.0.0"
|
|
77
|
+
},
|
|
78
|
+
"devDependencies": {
|
|
79
|
+
"@putout/formatter-json": "^2.0.0",
|
|
80
|
+
"@putout/test": "^11.1.0",
|
|
81
|
+
"c8": "^10.1.2",
|
|
82
|
+
"escover": "^4.0.0",
|
|
83
|
+
"eslint": "^9.7.0",
|
|
84
|
+
"eslint-plugin-putout": "^23.1.0",
|
|
85
|
+
"madrun": "^10.2.2",
|
|
86
|
+
"mock-import": "^4.0.2",
|
|
87
|
+
"mock-require": "^3.0.3",
|
|
88
|
+
"nodemon": "^3.0.1",
|
|
89
|
+
"runsome": "^1.0.0",
|
|
90
|
+
"supertape": "^10.0.0"
|
|
91
|
+
},
|
|
92
|
+
"publishConfig": {
|
|
93
|
+
"access": "public"
|
|
94
|
+
}
|
|
95
|
+
}
|