Skip to content

Commit e1314bf

Browse files
bpmutternzakassnitin315mdjermanovic
authored
docs: Integration section and tutorial (#17132)
* docs: Integration section and tutorial * add navigation and landing pages for integration section * copy edits * working sample rule * complete integration tutorial draft * remove TODO * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <[email protected]> * update source code * update tutorial * copy / linting edits * lint fix * Apply suggestions from code review Co-authored-by: Nicholas C. Zakas <[email protected]> * implement nz feedback * Copy edit * Apply suggestions from code review Co-authored-by: Nitin Kumar <[email protected]> * Apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * Apply suggestions from code review Co-authored-by: Milos Djermanovic <[email protected]> * implement MD feedback * copy edit --------- Co-authored-by: Nicholas C. Zakas <[email protected]> Co-authored-by: Nitin Kumar <[email protected]> Co-authored-by: Milos Djermanovic <[email protected]>
1 parent 19a8c5d commit e1314bf

File tree

12 files changed

+405
-9
lines changed

12 files changed

+405
-9
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* @fileoverview An example of how to integrate ESLint into your own tool
3+
* @author Ben Perlmutter
4+
*/
5+
6+
const { ESLint } = require("eslint");
7+
8+
// Create an instance of ESLint with the configuration passed to the function
9+
function createESLintInstance(overrideConfig){
10+
return new ESLint({ useEslintrc: false, overrideConfig: overrideConfig, fix: true });
11+
}
12+
13+
// Lint the specified files and return the error results
14+
async function lintAndFix(eslint, filePaths) {
15+
const results = await eslint.lintFiles(filePaths);
16+
17+
// Apply automatic fixes and output fixed code
18+
await ESLint.outputFixes(results);
19+
20+
return results;
21+
}
22+
23+
// Log results to console if there are any problems
24+
function outputLintingResults(results) {
25+
// Identify the number of problems found
26+
const problems = results.reduce((acc, result) => acc + result.errorCount + result.warningCount, 0);
27+
28+
if (problems > 0) {
29+
console.log("Linting errors found!");
30+
console.log(results);
31+
} else {
32+
console.log("No linting errors found.");
33+
}
34+
return results;
35+
}
36+
37+
// Put previous functions all together
38+
async function lintFiles(filePaths) {
39+
40+
// The ESLint configuration. Alternatively, you could load the configuration
41+
// from a .eslintrc file or just use the default config.
42+
const overrideConfig = {
43+
env: {
44+
es6: true,
45+
node: true,
46+
},
47+
parserOptions: {
48+
ecmaVersion: 2018,
49+
},
50+
rules: {
51+
"no-console": "error",
52+
"no-unused-vars": "warn",
53+
},
54+
};
55+
56+
const eslint = createESLintInstance(overrideConfig);
57+
const results = await lintAndFix(eslint, filePaths);
58+
return outputLintingResults(results);
59+
}
60+
61+
// Export integration
62+
module.exports = { lintFiles }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @fileoverview Test ESLint integration example code
3+
* @author Ben Perlmutter
4+
*/
5+
6+
const { lintFiles } = require("./example-eslint-integration");
7+
8+
async function testExampleEslintIntegration(){
9+
const filePaths = ["sample-data/test-file.js"];
10+
const lintResults = await lintFiles(filePaths);
11+
12+
// Test cases
13+
if(lintResults[0].messages.length !== 6){
14+
throw new Error("Expected 6 linting problems, got " + lintResults[0].messages.length);
15+
}
16+
const messageRuleIds = new Set()
17+
lintResults[0].messages.forEach(msg => messageRuleIds.add(msg.ruleId));
18+
if(messageRuleIds.size !== 2){
19+
throw new Error("Expected 2 linting rule, got " + messageRuleIds.size);
20+
}
21+
if(!messageRuleIds.has("no-console")){
22+
throw new Error("Expected linting rule 'no-console', got " + messageRuleIds);
23+
}
24+
console.log("All tests passed!");
25+
}
26+
27+
testExampleEslintIntegration()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "_integration-tutorial-code",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "node example-eslint-integration.test.js"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"eslint": "^8.39.0"
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @fileoverview Example data to lint using ESLint. This file contains a variety of errors.
3+
* @author Ben Perlmutter
4+
*/
5+
6+
// Unused variable 'y' (no-unused-vars from configured rules)
7+
const y = 20;
8+
9+
function add(a, b) {
10+
// Unexpected console statement (no-console from configured rules)
11+
console.log('Adding two numbers');
12+
return a + b;
13+
}
14+
15+
// 'result' is assigned a value but never used (no-unused-vars from configured rules)
16+
const result = add(x, 5);
17+
18+
if (x > 5) {
19+
// Unexpected console statement (no-console from configured rules)
20+
console.log('x is greater than 5');
21+
} else {
22+
// Unexpected console statement (no-console from configured rules)
23+
console.log('x is not greater than 5');
24+
}
25+
26+
// 'subtract' is defined but never used (no-unused-vars from configured rules)
27+
function subtract(a, b) {
28+
return a - b;
29+
}

docs/src/contribute/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Contribute to ESLint
33
eleventyNavigation:
44
key: contribute to eslint
55
title: Contribute to ESLint
6-
order: 3
6+
order: 4
77
---
88

99
One of the great things about open source projects is that anyone can contribute in any number of meaningful ways. ESLint couldn't exist without the help of the many contributors it's had since the project began, and we want you to feel like you can contribute and make a difference as well.

docs/src/extend/index.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,3 @@ This section explains how you can use a custom processor to have ESLint process
4848
## [Share Configurations](shareable-configs)
4949

5050
This section explains how you can bundle and share ESLint configuration in a JavaScript package.
51-
52-
## [Node.js API Reference](../integrate/nodejs-api)
53-
54-
If you're interested in writing a tool that uses ESLint, then you can use the Node.js API to get programmatic access to functionality.

docs/src/integrate/index.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Integrate ESLint
3+
eleventyNavigation:
4+
key: integrate eslint
5+
title: integrate ESLint
6+
order: 3
7+
8+
---
9+
10+
This guide is intended for those who wish to integrate the functionality of ESLint into other applications by using the ESLint API.
11+
12+
In order to integrate ESLint, it's recommended that:
13+
14+
* You know JavaScript since ESLint is written in JavaScript.
15+
* You have some familiarity with Node.js since ESLint runs on it.
16+
17+
If that sounds like you, then continue reading to get started.
18+
19+
## [Integrate with the Node.js API Tutorial](integration-tutorial)
20+
21+
This tutorial walks you through the process of creating a basic integration with ESLint using the Node.js API.
22+
23+
## [Node.js API Reference](nodejs-api)
24+
25+
If you're interested in writing a tool that uses ESLint, then you can use the Node.js API to get programmatic access to functionality.

0 commit comments

Comments
 (0)