From 3be07488ee7b6a9591d169be9648fbd36b32105e Mon Sep 17 00:00:00 2001 From: Siva K <60533560+siva-kannan3@users.noreply.github.com> Date: Sat, 21 Jan 2023 00:13:58 +0530 Subject: [PATCH] docs: add example for nodejs lintText api (#16789) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #16666 * docs: add example for nodejs lintText api * Update docs/src/integrate/nodejs-api.md Co-authored-by: 唯然 Co-authored-by: 唯然 --- docs/src/integrate/nodejs-api.md | 95 +++++++++++++++++++++++--------- 1 file changed, 69 insertions(+), 26 deletions(-) diff --git a/docs/src/integrate/nodejs-api.md b/docs/src/integrate/nodejs-api.md index b817c23fa53..28d93d7c46e 100644 --- a/docs/src/integrate/nodejs-api.md +++ b/docs/src/integrate/nodejs-api.md @@ -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. @@ -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); }); ```