Skip to content

Commit

Permalink
Merge branch 'main' into nav_reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
bpmutter committed Jan 21, 2023
2 parents c4e996d + 3be0748 commit 583c6da
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 28 deletions.
95 changes: 69 additions & 26 deletions docs/src/integrate/nodejs-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ eleventyNavigation:
parent: extend eslint
title: Node.js API Reference
order: 6

---

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
3 changes: 1 addition & 2 deletions docs/src/rules/space-before-function-paren.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
title: space-before-function-paren
rule_type: layout
related_rules:
- space-after-keywords
- space-return-throw-case
- keyword-spacing
---


Expand Down

0 comments on commit 583c6da

Please sign in to comment.