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
Changes from 14 commits
4b38679
bee32d5
15eb25b
f3c38f2
6c755b7
dc96c19
9123a30
93df76c
c54bc59
f5adc9a
752bc25
315f14b
9ad7693
1716a0a
2df027b
e9e5b2d
a39512a
735dca2
01dbfde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module.exports = { | ||
"env": { | ||
"browser": true, | ||
"commonjs": true, | ||
"es2021": true | ||
}, | ||
"extends": "airbnb-base", | ||
"overrides": [ | ||
], | ||
"parserOptions": { | ||
"ecmaVersion": "latest" | ||
}, | ||
"rules": { | ||
} | ||
} |
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,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 } |
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 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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per the tutorial, |
||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-plugin-import": "^2.27.5" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need these in the example project? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
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. | ||
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. |
There was a problem hiding this comment.
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?