Skip to content

Commit

Permalink
docs update
Browse files Browse the repository at this point in the history
  • Loading branch information
bpmutter committed Jun 8, 2023
1 parent 3826d17 commit ba78205
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 0 deletions.
21 changes: 21 additions & 0 deletions docs/_custom-rule-tutorial-code/.eslintrc.js
@@ -0,0 +1,21 @@
"use strict";

module.exports = {
env: {
browser: true,
commonjs: true,
es2021: true,
},
extends: ["eslint:recommended"],
parserOptions: {
ecmaVersion: "latest",
ecmaFeatures: {
strict: true,
}
},
// Using the eslint-plugin-foo-bar plugin downloaded from npm
plugins: ["foo-bar"],
rules: {
"foo-bar/foo-bar": "error",
},
}
1 change: 1 addition & 0 deletions docs/_custom-rule-tutorial-code/.gitignore
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions docs/_custom-rule-tutorial-code/eslint-plugin-foo-bar.js
@@ -0,0 +1,4 @@
"use strict";

const fooBarRule = require("./foo-bar");
module.exports = { rules: { "foo-bar": fooBarRule } };
18 changes: 18 additions & 0 deletions docs/_custom-rule-tutorial-code/example.js
@@ -0,0 +1,18 @@
"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!
}

43 changes: 43 additions & 0 deletions docs/_custom-rule-tutorial-code/foo-bar.js
@@ -0,0 +1,43 @@
"use strict";

// The foo-bar rule definition
module.exports = {
meta: {
type: "fix",
docs: {
description: "Can only assign 'bar' to `const foo`.",
},
fixable: "code"
},
create: function (context) {
return {
// Performs action in the function on every variable declaration
"VariableDeclaration": function(node) {
// Check if a `const` variable declaration
if(node.kind === "const") {
// Check if variable name is `foo`
if(node.declarations[0].id.name === "foo") {
// Check if value of variable is "bar"
if (node.declarations[0].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.declarations[0].init.value
},
fix(fixer) {
return fixer.replaceText(node.declarations[0].init, '"bar"');
}
});
}
}
}
}
};
}
};
30 changes: 30 additions & 0 deletions docs/_custom-rule-tutorial-code/foo-bar.test.js
@@ -0,0 +1,30 @@
"use strict";

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

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

// Throws error if the tests in ruleTester.run() do not pass
ruleTester.run(
"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 comments on commit ba78205

Please sign in to comment.