Skip to content

Commit 3be0748

Browse files
docs: add example for nodejs lintText api (#16789)
Fixes #16666 * docs: add example for nodejs lintText api * Update docs/src/integrate/nodejs-api.md Co-authored-by: 唯然 <[email protected]> Co-authored-by: 唯然 <[email protected]>
1 parent ce4f5ff commit 3be0748

File tree

1 file changed

+69
-26
lines changed

1 file changed

+69
-26
lines changed

docs/src/integrate/nodejs-api.md

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ eleventyNavigation:
55
parent: developer guide
66
title: Node.js API
77
order: 9
8-
98
---
109

1110
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.
@@ -24,48 +23,92 @@ Here's a simple example of using the `ESLint` class:
2423
const { ESLint } = require("eslint");
2524

2625
(async function main() {
27-
// 1. Create an instance.
28-
const eslint = new ESLint();
26+
// 1. Create an instance.
27+
const eslint = new ESLint();
2928

30-
// 2. Lint files.
31-
const results = await eslint.lintFiles(["lib/**/*.js"]);
29+
// 2. Lint files.
30+
const results = await eslint.lintFiles(["lib/**/*.js"]);
3231

33-
// 3. Format the results.
34-
const formatter = await eslint.loadFormatter("stylish");
35-
const resultText = formatter.format(results);
32+
// 3. Format the results.
33+
const formatter = await eslint.loadFormatter("stylish");
34+
const resultText = formatter.format(results);
3635

37-
// 4. Output it.
38-
console.log(resultText);
36+
// 4. Output it.
37+
console.log(resultText);
3938
})().catch((error) => {
40-
process.exitCode = 1;
41-
console.error(error);
39+
process.exitCode = 1;
40+
console.error(error);
4241
});
4342
```
4443

45-
And here is an example that autofixes lint problems:
44+
Here's an example that autofixes lint problems:
4645

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

5049
(async function main() {
51-
// 1. Create an instance with the `fix` option.
52-
const eslint = new ESLint({ fix: true });
50+
// 1. Create an instance with the `fix` option.
51+
const eslint = new ESLint({ fix: true });
52+
53+
// 2. Lint files. This doesn't modify target files.
54+
const results = await eslint.lintFiles(["lib/**/*.js"]);
55+
56+
// 3. Modify the files with the fixed code.
57+
await ESLint.outputFixes(results);
58+
59+
// 4. Format the results.
60+
const formatter = await eslint.loadFormatter("stylish");
61+
const resultText = formatter.format(results);
62+
63+
// 5. Output it.
64+
console.log(resultText);
65+
})().catch((error) => {
66+
process.exitCode = 1;
67+
console.error(error);
68+
});
69+
```
5370

54-
// 2. Lint files. This doesn't modify target files.
55-
const results = await eslint.lintFiles(["lib/**/*.js"]);
71+
And here is an example of using the `ESLint` class with `lintText` API:
72+
73+
```js
74+
const { ESLint } = require("eslint");
75+
76+
const testCode = `
77+
const name = "eslint";
78+
if(true) {
79+
console.log("constant condition warning")
80+
};
81+
`;
82+
83+
(async function main() {
84+
// 1. Create an instance
85+
const eslint = new ESLint({
86+
useEslintrc: false,
87+
overrideConfig: {
88+
extends: ["eslint:recommended"],
89+
parserOptions: {
90+
sourceType: "module",
91+
ecmaVersion: "latest",
92+
},
93+
env: {
94+
es2022: true,
95+
node: true,
96+
},
97+
},
98+
});
5699

57-
// 3. Modify the files with the fixed code.
58-
await ESLint.outputFixes(results);
100+
// 2. Lint text.
101+
const results = await eslint.lintText(testCode);
59102

60-
// 4. Format the results.
61-
const formatter = await eslint.loadFormatter("stylish");
62-
const resultText = formatter.format(results);
103+
// 3. Format the results.
104+
const formatter = await eslint.loadFormatter("stylish");
105+
const resultText = formatter.format(results);
63106

64-
// 5. Output it.
65-
console.log(resultText);
107+
// 4. Output it.
108+
console.log(resultText);
66109
})().catch((error) => {
67-
process.exitCode = 1;
68-
console.error(error);
110+
process.exitCode = 1;
111+
console.error(error);
69112
});
70113
```
71114

0 commit comments

Comments
 (0)