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: Integration section and tutorial #17132

Merged
merged 19 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions docs/_examples/integration-tutorial-code/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.DS_Store
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* @fileoverview An example of how to integrate ESLint into your own tool
* @author Ben Perlmutter
*/

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

// Create an instance of ESLint with the configuration passed to the function
function createESLintInstance(overrideConfig){
return new ESLint({ useEslintrc: false, overrideConfig: overrideConfig, fix: true });
}

// Lint the specified files and return the error results
async function lintAndFix(eslint, filePaths) {
const results = await eslint.lintFiles(filePaths);

// Apply automatic fixes and output fixed code
await ESLint.outputFixes(results);

return results;
}

// Log results to console if there are any problems
function outputLintingResults(results) {
// Identify the number of problems found
const problems = results.reduce((acc, result) => acc + result.errorCount + result.warningCount, 0);

if (problems > 0) {
console.log("Linting errors found!");
console.log(results);
} else {
console.log("No linting errors found.");
}
return results;
}

// Put previous functions all together
async function lintFiles(filePaths) {

// The ESLint configuration. Alternatively, you could load the configuration
// from a .eslintrc file or just use the default config.
const overrideConfig = {
env: {
es6: true,
node: true,
},
parserOptions: {
ecmaVersion: 2018,
},
rules: {
"no-console": "error",
"no-unused-vars": "warn",
},
};

const eslint = createESLintInstance(overrideConfig);
const results = await lintAndFix(eslint, filePaths);
return outputLintingResults(results);
}

// Export integration
module.exports = { lintFiles }
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @fileoverview Test ESLint integration example code
* @author Ben Perlmutter
*/

const { lintFiles } = require("./example-eslint-integration");

async function testExampleEslintIntegration(){
const filePaths = ["sample-data/test-file.js"];
const lintResults = await lintFiles(filePaths);

// Test cases
if(lintResults[0].messages.length !== 6){
throw new Error("Expected 6 linting problems, got " + lintResults[0].messages.length);
}
const messageRuleIds = new Set()
lintResults[0].messages.forEach(msg => messageRuleIds.add(msg.ruleId));
if(messageRuleIds.size !== 2){
throw new Error("Expected 2 linting rule, got " + messageRuleIds.size);
}
if(!messageRuleIds.has("no-console")){
throw new Error("Expected linting rule 'no-console', got " + messageRuleIds);
}
console.log("All tests passed!");
}

testExampleEslintIntegration()
15 changes: 15 additions & 0 deletions docs/_examples/integration-tutorial-code/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "_integration-tutorial-code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node example-eslint-integration.test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"eslint": "^8.39.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @fileoverview Example data to lint using ESLint. This file contains a variety of errors.
* @author Ben Perlmutter
*/

// Unused variable 'y' (no-unused-vars from configured rules)
const y = 20;

function add(a, b) {
// Unexpected console statement (no-console from configured rules)
console.log('Adding two numbers');
return a + b;
}

// 'result' is assigned a value but never used (no-unused-vars from configured rules)
const result = add(x, 5);

if (x > 5) {
// Unexpected console statement (no-console from configured rules)
console.log('x is greater than 5');
} else {
// Unexpected console statement (no-console from configured rules)
console.log('x is not greater than 5');
}

// 'subtract' is defined but never used (no-unused-vars from configured rules)
function subtract(a, b) {
return a - b;
}
2 changes: 1 addition & 1 deletion docs/src/contribute/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Contribute to ESLint
eleventyNavigation:
key: contribute to eslint
title: Contribute to ESLint
order: 3
order: 4
---

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.
Expand Down
4 changes: 0 additions & 4 deletions docs/src/extend/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,3 @@ This section explains how you can use a custom processor to have ESLint process
## [Share Configurations](shareable-configs)

This section explains how you can bundle and share ESLint configuration in a JavaScript package.

## [Node.js API Reference](../integrate/nodejs-api)

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.
25 changes: 25 additions & 0 deletions docs/src/integrate/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: Integrate ESLint
eleventyNavigation:
key: integrate eslint
title: integrate ESLint
order: 3

---

This guide is intended for those who wish to integrate the functionality of ESLint into other applications by using the ESLint API.

In order to integrate ESLint, it's recommended that:

* You know JavaScript since ESLint is written in JavaScript.
* You have some familiarity with Node.js since ESLint runs on it.

If that sounds like you, then continue reading to get started.

## [Integrate with the Node.js API Tutorial](integration-tutorial)

This tutorial walks you through the process of creating a basic integration with ESLint using the Node.js API.

## [Node.js API Reference](nodejs-api)

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.