Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add example for nodejs lintText api #16789

Merged
merged 2 commits into from Jan 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
95 changes: 69 additions & 26 deletions docs/src/integrate/nodejs-api.md
Expand Up @@ -5,7 +5,6 @@ eleventyNavigation:
parent: developer guide
title: Node.js API
order: 9

---

While ESLint is designed to be run on the command line, it's possible to use ESLint programmatically through the Node.js API. The purpose of the Node.js API is to allow plugin and tool authors to use the ESLint functionality directly, without going through the command line interface.
Expand All @@ -24,48 +23,92 @@ Here's a simple example of using the `ESLint` class:
const { ESLint } = require("eslint");

(async function main() {
// 1. Create an instance.
const eslint = new ESLint();
// 1. Create an instance.
const eslint = new ESLint();

// 2. Lint files.
const results = await eslint.lintFiles(["lib/**/*.js"]);
// 2. Lint files.
const results = await eslint.lintFiles(["lib/**/*.js"]);

// 3. Format the results.
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);
// 3. Format the results.
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);

// 4. Output it.
console.log(resultText);
// 4. Output it.
console.log(resultText);
})().catch((error) => {
process.exitCode = 1;
console.error(error);
process.exitCode = 1;
console.error(error);
});
```

And here is an example that autofixes lint problems:
Here's an example that autofixes lint problems:

```js
const { ESLint } = require("eslint");

(async function main() {
// 1. Create an instance with the `fix` option.
const eslint = new ESLint({ fix: true });
// 1. Create an instance with the `fix` option.
const eslint = new ESLint({ fix: true });

// 2. Lint files. This doesn't modify target files.
const results = await eslint.lintFiles(["lib/**/*.js"]);

// 3. Modify the files with the fixed code.
await ESLint.outputFixes(results);

// 4. Format the results.
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);

// 5. Output it.
console.log(resultText);
})().catch((error) => {
process.exitCode = 1;
console.error(error);
});
```

// 2. Lint files. This doesn't modify target files.
const results = await eslint.lintFiles(["lib/**/*.js"]);
And here is an example of using the `ESLint` class with `lintText` API:

```js
const { ESLint } = require("eslint");

const testCode = `
const name = "eslint";
if(true) {
console.log("constant condition warning")
};
`;

(async function main() {
// 1. Create an instance
const eslint = new ESLint({
useEslintrc: false,
overrideConfig: {
extends: ["eslint:recommended"],
parserOptions: {
sourceType: "module",
ecmaVersion: "latest",
},
env: {
es2022: true,
node: true,
},
},
});

// 3. Modify the files with the fixed code.
await ESLint.outputFixes(results);
// 2. Lint text.
const results = await eslint.lintText(testCode);

// 4. Format the results.
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);
// 3. Format the results.
const formatter = await eslint.loadFormatter("stylish");
const resultText = formatter.format(results);

// 5. Output it.
console.log(resultText);
// 4. Output it.
console.log(resultText);
})().catch((error) => {
process.exitCode = 1;
console.error(error);
process.exitCode = 1;
console.error(error);
});
```

Expand Down