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 14 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
15 changes: 15 additions & 0 deletions docs/_examples/integration-tutorial-code/.eslintrc.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this file isn't used in any way, so we could remove it?

@@ -0,0 +1,15 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es2021": true
},
"extends": "airbnb-base",
"overrides": [
],
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
}
}
2 changes: 2 additions & 0 deletions docs/_examples/integration-tutorial-code/.gitignore
@@ -0,0 +1,2 @@
node_modules
.DS_Store
@@ -0,0 +1,59 @@
/**
* @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 });
}

// 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) {
if (results.length) {
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 }
@@ -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 3 linting errors, got " + lintResults[0].messages.length);
bpmutter marked this conversation as resolved.
Show resolved Hide resolved
}
const messageRuleIds = new Set()
lintResults[0].messages.forEach(msg => messageRuleIds.add(msg.ruleId));
if(messageRuleIds.size !== 2){
throw new Error("Expected 1 linting rule, got " + messageRuleIds.size);
bpmutter marked this conversation as resolved.
Show resolved Hide resolved
}
if(!messageRuleIds.has("no-console")){
throw new Error("Expected linting rule 'no-console', got " + messageRuleIds);
}
console.log("All tests passed!");
}

testExampleEslintIntegration()
17 changes: 17 additions & 0 deletions docs/_examples/integration-tutorial-code/package.json
@@ -0,0 +1,17 @@
{
"name": "_integration-tutorial-code",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "node example-eslint-integration.test.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"eslint": "^8.39.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the tutorial, eslint should be in dependencies?

"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.27.5"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these in the example project?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing.

i had these and the .eslintrc file b/c i was using it to lint the code i was writing

}
}
@@ -0,0 +1,32 @@
/**
* @fileoverview Example data to lint using ESLint. This file contains a variety of errors.
* @author Ben Perlmutter
*/
v// 'var' should be replaced with 'const' or 'let' (no-var from eslint:recommended)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-var is not in eslint:recommended and it isn't enabled in the hardcoded configuration inside example-eslint-integration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, i worked on this so long ago that i don't remember context on this. deleting this examples from the file.

var x = 10;

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

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

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

// Expected indentation of 2 spaces but found 4 (indent from eslint:recommended)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, indent is not in eslint:recommended and it isn't enabled in the hardcoded configuration inside example-eslint-integration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment as above. don't remember context. deleting the example

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

// 'subtract' is defined but never used (no-unused-vars from custom rules)
function subtract(a, b) {
return a - b;
}
2 changes: 1 addition & 1 deletion docs/src/contribute/index.md
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
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
@@ -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.
bpmutter marked this conversation as resolved.
Show resolved Hide resolved

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

## [Create Integration with the Node.js API Tutorial](integration-tutorial)
bpmutter marked this conversation as resolved.
Show resolved Hide resolved

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.