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: Custom rule & plugin tutorial #17024

Merged
merged 40 commits into from Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 39 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
243e5c3
Start copy edits
bpmutter Feb 18, 2023
afcf0c4
checkpoint
bpmutter Mar 21, 2023
3826d17
docs update
bpmutter Mar 27, 2023
ba78205
docs update
bpmutter Mar 27, 2023
5fa1b69
Add intro
bpmutter Mar 28, 2023
25a2b8b
Copy edits
bpmutter Mar 28, 2023
5287360
remove old draft of tutorial
bpmutter Mar 29, 2023
23e4a51
Fix take ii
bpmutter Mar 29, 2023
32148fc
update tutorial
bpmutter Apr 2, 2023
1049acd
Apply suggestions from code review
bpmutter Apr 7, 2023
d0df234
update src code names
bpmutter Apr 7, 2023
2157d9f
rename tutorial
bpmutter Apr 7, 2023
bad4860
update code examples to match updated code
bpmutter Apr 7, 2023
631d531
additional fixes
bpmutter Apr 7, 2023
63f7d33
implement additional NZ feedback
bpmutter Apr 7, 2023
bce1ec7
Apply suggestions from code review
bpmutter Apr 27, 2023
b898579
draft tutorial changes
bpmutter Apr 27, 2023
365f98d
copy edits
bpmutter Apr 27, 2023
7d28f30
fix lint err
bpmutter Apr 27, 2023
0341b87
Apply suggestions from code review
bpmutter Apr 30, 2023
e8d9662
add file overviews
bpmutter Apr 30, 2023
c5f8c4d
copy edits on step 8 intro
bpmutter Apr 30, 2023
cf08def
Apply suggestions from code review
bpmutter May 3, 2023
dcc249d
implement NZ feedback
bpmutter May 3, 2023
15f2012
Apply suggestions from code review
bpmutter May 10, 2023
6d06023
Apply suggestions from code review
bpmutter May 10, 2023
1541c9b
Apply suggestions from code review
bpmutter May 10, 2023
ea4fb7d
Apply suggestions from code review
bpmutter May 10, 2023
c161d8f
Apply suggestions from code review
bpmutter May 10, 2023
eed6295
update rule name to "enforce-foo-bar"
bpmutter May 10, 2023
e1056de
Apply suggestions from code review
bpmutter May 10, 2023
733c462
implement MD feedback
bpmutter May 25, 2023
07f57b5
Apply suggestions from code review
bpmutter May 26, 2023
ac9e8d8
implement MD feedback
bpmutter May 26, 2023
da582e5
copy edit
bpmutter May 26, 2023
fdf734b
Apply suggestions from code review
bpmutter May 28, 2023
94530df
move tutorial to _examples dir
bpmutter Jun 8, 2023
059b492
undo big change
bpmutter Jun 8, 2023
8d4c159
Fix broken example
bpmutter Jun 8, 2023
5010c9e
Apply suggestions from code review
bpmutter Jun 11, 2023
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
1 change: 1 addition & 0 deletions docs/_examples/custom-rule-tutorial-code/.gitignore
@@ -0,0 +1 @@
node_modules
57 changes: 57 additions & 0 deletions docs/_examples/custom-rule-tutorial-code/enforce-foo-bar.js
@@ -0,0 +1,57 @@
/**
* @fileoverview Rule to enforce that `const foo` is assigned "bar".
* @author Ben Perlmutter
*/

"use strict";

// The enforce-foo-bar rule definition
module.exports = {
meta: {
type: "problem",
docs: {
description: "Enforce that a variable named `foo` can only be assigned a value of 'bar'."
},
fixable: "code",
schema: []
},
create(context) {
return {

// Performs action in the function on every variable declarator
VariableDeclarator(node) {

// Check if a `const` variable declaration
if (node.parent.kind === "const") {

// Check if variable name is `foo`
if (node.id.type === "Identifier" && node.id.name === "foo") {

// Check if value of variable is "bar"
if (node.init && node.init.type === "Literal" && node.init.value !== "bar") {

/*
* Report error to ESLint. Error message uses
* a message placeholder to include the incorrect value
* in the error message.
* Also includes a `fix(fixer)` function that replaces
* any values assigned to `const foo` with "bar".
*/
context.report({
node,
message: 'Value other than "bar" assigned to `const foo`. Unexpected value: {{ notBar }}.',
data: {
notBar: node.init.value
},
fix(fixer) {
return fixer.replaceText(node.init, '"bar"');
}
});
}
}
}
}
};
}
};

@@ -0,0 +1,34 @@
/**
* @fileoverview Tests for enforce-foo-bar.js rule.
* @author Ben Perlmutter
*/
"use strict";

const {RuleTester} = require("eslint");
const fooBarRule = require("./enforce-foo-bar");

const ruleTester = new RuleTester({
// Must use at least ecmaVersion 2015 because
// that's when `const` variable were introduced.
parserOptions: { ecmaVersion: 2015 }
});

// Throws error if the tests in ruleTester.run() do not pass
ruleTester.run(
"enforce-foo-bar", // rule name
fooBarRule, // rule code
{ // checks
// 'valid' checks cases that should pass
valid: [{
code: "const foo = 'bar';",
}],
// 'invalid' checks cases that should not pass
invalid: [{
code: "const foo = 'baz';",
output: 'const foo = "bar";',
errors: 1,
}],
}
);

console.log("All tests passed!");
@@ -0,0 +1,9 @@
/**
* @fileoverview Example an ESLint plugin with a custom rule.
* @author Ben Perlmutter
*/
"use strict";

const fooBarRule = require("./enforce-foo-bar");
const plugin = { rules: { "enforce-foo-bar": fooBarRule } };
module.exports = plugin;
23 changes: 23 additions & 0 deletions docs/_examples/custom-rule-tutorial-code/eslint.config.js
@@ -0,0 +1,23 @@
/**
* @fileoverview Example ESLint config file that uses the custom rule from this tutorial.
* @author Ben Perlmutter
*/
"use strict";

// Import the ESLint plugin
const eslintPluginExample = require("./eslint-plugin-example");

module.exports = [
{
files: ["**/*.js"],
languageOptions: {
sourceType: "commonjs",
ecmaVersion: "latest",
},
// Using the eslint-plugin-example plugin defined locally
plugins: {"example": eslintPluginExample},
rules: {
"example/enforce-foo-bar": "error",
},
}
]
22 changes: 22 additions & 0 deletions docs/_examples/custom-rule-tutorial-code/example.js
@@ -0,0 +1,22 @@
/**
* @fileoverview Example of a file that will fail the custom rule in this tutorial.
* @author Ben Perlmutter
*/
"use strict";

/* eslint-disable no-unused-vars -- Disable other rule causing problem for this file */

// To see the error in the terminal, run the following command:
// npx eslint example.js

// To fix the error, run the following command:
// npx eslint example.js --fix

function correctFooBar() {
const foo = "bar";
}

function incorrectFoo(){
const foo = "baz"; // Problem!
}

22 changes: 22 additions & 0 deletions docs/_examples/custom-rule-tutorial-code/package.json
@@ -0,0 +1,22 @@
{
"name": "eslint-plugin-example",
"version": "1.0.0",
"description": "ESLint plugin for enforce-foo-bar rule.",
"main": "eslint-plugin-example.js",
"keywords": [
"eslint",
"eslintplugin",
"eslint-plugin"
],
"peerDependencies": {
"eslint": ">=8.0.0"
},
"scripts": {
"test": "node enforce-foo-bar.test.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"eslint": "^8.36.0"
}
}
2 changes: 1 addition & 1 deletion docs/src/extend/custom-processors.md
Expand Up @@ -4,7 +4,7 @@ eleventyNavigation:
key: custom processors
parent: create plugins
title: Custom Processors
order: 2
order: 3

---

Expand Down