After looking at the sparse docs for the ESLint Node.js API, I have this code:
const { ESLint } = require('eslint')
const ESLINT_CONFIG = require('../eslint.code.json')
async function lint(path) {
// 1. Create an instance.
const eslint = new ESLint({
fix: true,
overrideConfig: ESLINT_CONFIG,
useEslintrc: false,
})
// 2. Lint files.
const results = await eslint.lintFiles([path])
// 3. Format the results.
const formatter = await eslint.loadFormatter('stylish')
const resultText = formatter.format(results)
console.log(resultText)
}
lint('example.ts')
My project has a .eslintrc.json
, but I want to override it completely (i.e. not use it), so I created a second one, eslint.code.json
, which is minimal:
{
"env": {
"browser": true,
"es2021": true
},
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"project": ["./tsconfig.json"]
},
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint",
"import",
"simple-import-sort",
"sort-exports",
"typescript-sort-keys",
"sort-keys",
"prettier"
],
"extends": ["prettier", "next"],
"rules": {
"curly": 2,
"@typescript-eslint/quotes": [
"error",
"single",
{
"avoidEscape": true,
"allowTemplateLiterals": true
}
],
"padding-line-between-statements": "off",
"@typescript-eslint/padding-line-between-statements": [
"error",
{ "blankLine": "always", "prev": "*", "next": "function" },
{ "blankLine": "always", "prev": "*", "next": "block" },
{ "blankLine": "always", "prev": "*", "next": "return" },
{ "blankLine": "always", "prev": "*", "next": "type" }
]
}
}
My tsconfig.json
is like this:
{
"compilerOptions": {
"target": "es5",
"declaration": true,
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"module": "commonjs",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "react",
"incremental": true,
"outDir": "dist",
"baseUrl": ".",
"declarationMap": true
},
"exclude": ["node_modules", "dist"]
}
I am not sure I need to use my tsconfig at all, but there it is anyways.
So when I run it on an example file such as this:
function tanh(x) {
return x.clamp(-15, 15).tanh()
}
function artanh(x: TorchTensor) {
x = x.clamp(-1 + 1e-7, 1 - 1e-7)
return torch
.log(1 + x)
.sub(torch.log(1 - x))
.mul(0.5)
}
I would expect it to put a space between the two functions (because of the eslint config). Here is my package.json:
{
"main": "src/index.js",
"files": [
"dist",
"src"
],
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.48.1",
"@typescript-eslint/parser": "^5.48.1",
"eslint": "8.31.0",
"eslint-config-next": "13.1.2",
"eslint-config-prettier": "^8.6.0",
"eslint-config-standard-with-typescript": "^27.0.1",
"eslint-import-resolver-typescript": "^3.5.3",
"eslint-plugin-import": "^2.27.4",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-simple-import-sort": "^8.0.0",
"eslint-plugin-sort-exports": "^0.8.0",
"eslint-plugin-sort-keys": "^2.3.5",
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"prettier": "^2.8.2"
}
}
I am getting this error though:
node:internal/errors:491
ErrorCaptureStackTrace(err);
^
TypeError [ERR_INVALID_ARG_VALUE]: Failed to load plugin '@typescript-eslint' declared in 'CLIOptions': The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received ''
at new NodeError (node:internal/errors:400:5)
at createRequire (node:internal/modules/cjs/loader:1333:13)
at Object.resolve (/exampleproj/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:2325:16)
at ModuleResolver.resolve (/exampleproj/node_modules/@rushstack/eslint-patch/lib/modern-module-resolution.js:210:48)
at ConfigArrayFactory._loadPlugin (/exampleproj/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3392:33)
at ConfigArrayFactory._loadPlugin (/exampleproj/node_modules/@rushstack/eslint-patch/lib/modern-module-resolution.js:219:43)
at /exampleproj/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3283:33
at Array.reduce (<anonymous>)
at ConfigArrayFactory._loadPlugins (/exampleproj/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3279:22)
at ConfigArrayFactory._normalizeObjectConfigDataBody (/exampleproj/node_modules/@eslint/eslintrc/dist/eslintrc.cjs:3079:44) {
code: 'ERR_INVALID_ARG_VALUE'
}
What am I doing wrong? How can I simply format a string? Basically these are my related questions:
- What am I doing wrong?
- Am I setting the config correctly?
- What is
stylish
, I couldn’t figure out what I need to set for that? (prettier?)