Package Exports
- flatlint
- flatlint/with-plugins
Readme
FlatLint
Token-based JavaScript linter that fixes Syntax Errors
Install
npm i flatlintAvailable fixes
Assignment without parentheses after &&
-a && b = c;
+a && (b = c);convert comma to semicolon
-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;convert from to require
-const a = from 'a';
+const a = require('a');add missing curly brace
-function a({b, c) {}
-function a({b, c}) {}add missing round brace
-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')
}add missing assign
-const a 5;
+const a = 5;
-module.exports {};
+module.exports = {};add missing comma
import {
- a
+ a,
b,
} from 'c'; add missing arrow '=>'
-const a = (b, c) {};
+const a = (b, c) => {}; add const to export
-export x = 5;
+export const x = 5; add missing squire brace
-const a = ['hello', 'world';
+const a = ['hello', 'world']; remove useless round brace
-const a = 5);
+const a = 5;
-import a from 'a');
+import a from 'a'; remove useless square brace
-const a = [1, 2, 3]];
+const a = [1, 2, 3]; convert semicolon to comma
const a = {
- b: 'hello';
+ b: 'hello',
} remove useless comma
function x() {
return m;
-},
+} remove invalid character
-const {¬
-····is,¬
-····sArgsStr,¬
-····isTypeParamsStr,¬
-} = require('./is');¬
+const {
+ is,
+ isArgsStr,
+ isTypeParamsStr,
+} = require('./is'); add missing quote
-const a = 'hello
+const a = 'hello'
-fn('hello);
+fn('hello'); Remove useless arrow
-function parse(source) => {
+function parse(source) {
return source;
} Remove useless coma
const a = class {
- b() {},
+ b() {}
} add missing semicolon
-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`, {
plugins: [
['wrap-assignment-in-parens', {
report: () => `Wrap the assignment in parentheses after '&&'`,
replace: () => ({
'__a && __b = __c': '__a && (__b = __c)',
}),
}],
],
});License
MIT