flatlint
v4.0.0
Published
JavaScript tokens-based linter
Maintainers
Readme
FlatLint

Token-based JavaScript linter that fixes Syntax Errors
Install
npm i flatlintAvailable fixes
-import {readFile}, fs from 'node:fs';
+import fs, {readFile} from 'node:fs';-import fs form 'node:fs';
+import fs from 'node:fs';-a && b = c;
+a && (b = c);-import a from 'a' assert {type: 'json'}
+import a from 'a' with {type: 'json'}-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;-console.log(a, b):
+console.log(a, b);export const rules = [
- ['apply-nesting': applyNesting],
+ ['apply-nesting', applyNesting],
];-import {simpleImport: _simpleImport} from './simple-import.js';
+import {simpleImport as _simpleImport} from './simple-import.js';-const a = from 'a';
+const a = require('a');-function a({b, c) {}
-function a({b, c}) {}
-const {a = b;
+const {a} = b;-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);-const a 5;
+const a = 5;
-module.exports {};
+module.exports = {};import {
- a
+ a,
b,
} from 'c';
t.transform('declare-imports-first', {
- 'declare-imports-first': declareImportsFirst
+ 'declare-imports-first': declareImportsFirst,
'convert-esm-to-commonjs': convertEsmToCommonJs,
});-const a = (b, c) {};
+const a = (b, c) => {};-export x = 5;
+export const x = 5;-const a = ['hello', 'world';
+const a = ['hello', 'world'];-const a = 5);
+const a = 5;
-import a from 'a');
+import a from 'a';
if (a) {
-})
+}-const a = [1, 2, 3]];
+const a = [1, 2, 3];const a = {
- b: 'hello';
+ b: 'hello',
}
const b = [
1,
- 2;
+ 2,
3,
]function x() {
return m;
-},
+}
-const expected = [],
+const expected = [];
t.equal(expected, []);-fn([]);
+fn([].);-const {¬
-····is,¬
-····sArgsStr,¬
-····isTypeParamsStr,¬
-} = require('./is');¬
+const {
+ is,
+ isArgsStr,
+ isTypeParamsStr,
+} = require('./is');-const a = 'hello
+const a = 'hello'
-fn('hello);
+fn('hello');-function parse(source) => {
+function parse(source) {
return source;
}-import {readFile, readdir} = from 'node:fs/promises';
+import {readFile, readdir} from 'node:fs/promises';const a = class {
- b() {},
+ b() {}
}-const a = 5
+const a = 5; if (a < 0)
console.log('hello');
- else (a > 3)
+ else if (a > 3)
console.log('world');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
