FlatLint

Token-based JavaScript linter that fixes Syntax Errors
Install
npm i flatlint
Available fixes
diff
-import {readFile}, fs from 'node:fs';
+import fs, {readFile} from 'node:fs';
&&
</summary>
diff
-a && b = c;
+a && (b = c);
diff
-const a = 5,
+const a = 5;
function x() {
- return m,
+ return m;
}
-import a from 'a',
+import a from 'a';
-const a = 3,
+const a = 3;
module.exports = 2;
diff
-console.log(a, b):
+console.log(a, b);
diff
export const rules = [
- ['apply-nesting': applyNesting],
+ ['apply-nesting', applyNesting],
];
from
to require
</summary>
diff
-const a = from 'a';
+const a = require('a');
diff
-function a({b, c) {}
-function a({b, c}) {}
-const {a = b;
+const {a} = b;
diff
-if a > 5 {
+if (a > 5) {
alert();
}
-if (a.b() {
+if (a.b()) {
}
-a('hello'
+a('hello');
const m = {
- z: z('hello'
+ z: z('hello')
}
-{hello} = world;
+({hello} = world);
-assign(oldPath, currentPath;
+assign(oldPath, currentPath);
diff
-const a 5;
+const a = 5;
-module.exports {};
+module.exports = {};
diff
import {
- a
+ a,
b,
} from 'c';
t.transform('declare-imports-first', {
- 'declare-imports-first': declareImportsFirst
+ 'declare-imports-first': declareImportsFirst,
'convert-esm-to-commonjs': convertEsmToCommonJs,
});
'=>'
</summary>
diff
-const a = (b, c) {};
+const a = (b, c) => {};
const
to export
</summary>
diff
-export x = 5;
+export const x = 5;
diff
-const a = ['hello', 'world';
+const a = ['hello', 'world'];
diff
-const a = 5);
+const a = 5;
-import a from 'a');
+import a from 'a';
if (a) {
-})
+}
diff
-const a = [1, 2, 3]];
+const a = [1, 2, 3];
diff
const a = {
- b: 'hello';
+ b: 'hello',
}
const b = [
1,
- 2;
+ 2,
3,
]
diff
function x() {
return m;
-},
+}
-const expected = [],
+const expected = [];
t.equal(expected, []);
diff
-fn([].);
+fn([].);
diff
-const {¬
-····is,¬
-····sArgsStr,¬
-····isTypeParamsStr,¬
-} = require('./is');¬
+const {
+ is,
+ isArgsStr,
+ isTypeParamsStr,
+} = require('./is');
diff
-const a = 'hello
+const a = 'hello'
-fn('hello);
+fn('hello');
diff
-function parse(source) => {
+function parse(source) {
return source;
}
diff
const a = class {
- b() {},
+ b() {}
}
diff
-const a = 5
+const a = 5;
Template literals
FlatLint uses language similar to 🐊PutoutScript.
It can look similar, but has a couple differences:
- ✅ it may not be valid JavaScript, it can be couple tokens that can be fixed;
- ✅ it counts each symbol as a token;
__a
From __a
to __z
is usually identifiers, but can also be strings if used with quotes '__a'
they can be single or double,
it can be only one quote '__a
- this is valid, since FlatLint is tokens based.
__array
Collects everything that looks like array elements, it can start from squire brace [__array;
, but that's not important
to end with it, since it used to fix error patterns.
__args
Collects arguments of function when exists.
__expr
Collects everything that looks like expression.
API
import {lint, plugins} from 'flatlint/with-plugins';
const [code] = flatlint(`a && b = c`, {
plugins,
});
// returns
`
a && (b = c);
`;
Without fix
:
import {lint, plugins} from 'flatlint/with-plugins';
const [, places] = flatlint(`a && b = c`, {
fix: false,
plugins,
});
// returns
[{
column: 1,
line: 1,
message: `Wrap the assignment in parentheses after '&&'`,
rule: 'wrap-assignment-in-parens',
}];
When you want to use custom plugins:
import {lint} from 'flatlint';
const [code] = lint(`a && b = c`, {
startLine: 1,
plugins: [
['wrap-assignment-in-parens', {
report: () => `Wrap the assignment in parentheses after '&&'`,
replace: () => ({
'__a && __b = __c': '__a && (__b = __c)',
}),
}],
],
});
License
MIT