Skip to content

Commit

Permalink
Merge pull request #10 from kyaido/option_message
Browse files Browse the repository at this point in the history
Add custom message option
  • Loading branch information
kyaido authored Sep 22, 2021
2 parents 8524486 + 3c0d29a commit b9a23a6
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 65 deletions.
7 changes: 7 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
env: {
es6: true,
node: true,
},
extends: ['eslint:recommended', 'prettier'],
};
23 changes: 11 additions & 12 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ name: Node.js CI

on:
push:
branches: [ master ]
branches: [master]
pull_request:
branches: [ master ]
branches: [master]

jobs:
build:

runs-on: ubuntu-latest

strategy:
Expand All @@ -20,12 +19,12 @@ jobs:
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
3 changes: 3 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"singleQuote": true
}
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ An ESLint micro plugin.
$ npm install eslint-plugin-literal-blacklist --save-dev
```


## Usage

You can put string or object in an array.

```
// .eslintrc
{
"plugins": [
"literal-blacklist"
],
"rules": {
"literal-blacklist/literal-blacklist": [2, ["put", "your", "rule"]]
"literal-blacklist/literal-blacklist": [2, ["put", "your", "string", "or", {
term: "object",
message: "custom message",
}]]
}
}
```


## Supported Rules

### `literal-blacklist`
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use strict';

module.exports.rules = {
'literal-blacklist': require('./rules/literal-blacklist.js')
};
'literal-blacklist': require('./rules/literal-blacklist.js'),
};
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{
"name": "eslint-plugin-literal-blacklist",
"description": "An ESLint micro plugin",
"version": "0.1.3",
"version": "1.0.0",
"author": "kyaido",
"bugs": {
"url": "https://github.com/kyaido/eslint-plugin-literal-blacklist/issues"
},
"devDependencies": {
"eslint": "^7.32.0",
"mocha": "^9.1.1"
"eslint-config-prettier": "^8.3.0",
"mocha": "^9.1.1",
"prettier": "^2.4.1"
},
"homepage": "https://github.com/kyaido/eslint-plugin-literal-blacklist",
"keywords": [
Expand All @@ -23,6 +25,8 @@
"url": "https://github.com/kyaido/eslint-plugin-literal-blacklist.git"
},
"scripts": {
"test": "mocha tests/**/*.js"
"test": "mocha tests/**/*.js",
"format": "prettier --write .",
"lint": "eslint --fix ."
}
}
73 changes: 50 additions & 23 deletions rules/literal-blacklist.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,56 @@
'use strict';

module.exports = function(context) {
module.exports = {
meta: {
schema: [
{
type: 'array',
items: {
oneOf: [
{
type: 'string',
},
{
type: 'object',
properties: {
term: {
type: 'string',
},
message: {
type: 'string',
},
},
additionalProperties: false,
},
],
},
uniqueItems: true,
},
],
},

let options = [];
if (Array.isArray(context.options[0])) {
options = context.options[0];
}
create: (context) => {
let options = [];
if (Array.isArray(context.options[0])) {
options = context.options[0];
}

return {
'Literal': node => {
let message = null;
let value = String(node.value);
return {
Literal: (node) => {
const value = String(node.value);

options.forEach(option => {
if(value.indexOf(option) !== -1) {
message = `You should not use "${option}".`;
context.report({node: node, message: message});
}
});
}
};
};
options.forEach((option) => {
const isStringOption = typeof option === 'string';
const term = isStringOption ? option : option.term;

module.exports.schema = [{
type: 'array',
items: { type: 'string' },
uniqueItems: true
}];
if (value.indexOf(term) !== -1) {
const message = isStringOption
? `You should not use '${term}'.`
: option.message;
context.report({ node, message });
}
});
},
};
},
};
53 changes: 32 additions & 21 deletions tests/literal-blacklist.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,47 @@ const tester = new RuleTester();

tester.run('literal-blacklist', require('../rules/literal-blacklist'), {
valid: [
{ code: 'var https = "https:";',
options: [['http:']]
},
{ code: 'var http = "https:";',
options: [['http:']]
},
{ code: 'http();',
options: [['http:']
]}
{ code: 'var https = "https:";', options: [['http:']] },
{ code: 'var http = "https:";', options: [['http:']] },
{ code: 'http();', options: [['http:']] },
],
invalid: [
{ code: 'var http = "http://example.com";',
{
code: 'var http = "http://example.com";',
options: [['http:']],
errors: ['You should not use "http:".']
errors: [`You should not use 'http:'.`],
},
{ code: 'document.body.innerHTML("<script src=\'http://example.com\'></script>");',
{
code: 'document.body.innerHTML("<script src=\'http://example.com\'></script>");',
options: [['http:']],
errors: ['You should not use "http:".']
errors: [`You should not use 'http:'.`],
},
{ code: 'var obj = { url: "http://example.com" };',
{
code: 'var obj = { url: "http://example.com" };',
options: [['http:']],
errors: ['You should not use "http:".']
errors: [`You should not use 'http:'.`],
},
{ code: 'var arr = [ "https://example.com", "http://example.com" ];',
{
code: 'var arr = [ "https://example.com", "http://example.com" ];',
options: [['http:']],
errors: ['You should not use "http:".']
errors: [`You should not use 'http:'.`],
},
{ code: '(function() { return "http://example.com" })();',
{
code: '(function() { return "http://example.com" })();',
options: [['http:']],
errors: ['You should not use "http:".']
}
]
errors: [`You should not use 'http:'.`],
},
{
code: 'var http = "http://example.com";',
options: [
[
{
term: 'http:',
message: `You should use 'https:' instead of 'http:.'`,
},
],
],
errors: [`You should use 'https:' instead of 'http:.'`],
},
],
});

0 comments on commit b9a23a6

Please sign in to comment.