-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
"use strict"; | ||
|
||
const fooBarRule = require("./foo-bar"); | ||
module.exports = { rules: { "foo-bar": fooBarRule } }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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! | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"'); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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!"); |