eslint-plugin-marlint
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -15,3 +15,3 @@ # Limit usage of dangerouslySetInnerHTML (limited-danger) | ||
```js | ||
<div dangerouslySetInnerHTML={{ __html: "" }} />; | ||
<div dangerouslySetInnerHTML={{ __html: '' }} /> | ||
``` | ||
@@ -22,3 +22,3 @@ | ||
```js | ||
<script dangerouslySetInnerHTML={{ __html: "" }} />; | ||
<script dangerouslySetInnerHTML={{ __html: '' }} /> | ||
``` | ||
@@ -45,2 +45,2 @@ | ||
* [dangerouslySetInnerHTML](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml) | ||
- [dangerouslySetInnerHTML](https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml) |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
@@ -12,3 +12,3 @@ //------------------------------------------------------------------------------ | ||
const requireIndex = require("requireindex"); | ||
const requireIndex = require('requireindex'); | ||
@@ -20,2 +20,2 @@ //------------------------------------------------------------------------------ | ||
// import all rules in lib/rules | ||
module.exports.rules = requireIndex(__dirname + "/rules"); | ||
module.exports.rules = requireIndex(__dirname + '/rules'); |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
@@ -12,3 +12,3 @@ // ------------------------------------------------------------------------------ | ||
const DEFAULTS = ["script", "style"]; | ||
const DEFAULTS = ['script', 'style']; | ||
@@ -20,83 +20,83 @@ //------------------------------------------------------------------------------ | ||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: "Limit usage of dangerouslySetInnerHTML", | ||
category: "Best Practices", | ||
recommended: true | ||
}, | ||
schema: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
allowedTagNames: { | ||
type: "array", | ||
items: { | ||
type: "string" | ||
} | ||
} | ||
}, | ||
additionalProperties: true | ||
} | ||
] | ||
}, | ||
meta: { | ||
docs: { | ||
description: 'Limit usage of dangerouslySetInnerHTML', | ||
category: 'Best Practices', | ||
recommended: true, | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowedTagNames: { | ||
type: 'array', | ||
items: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
additionalProperties: true, | ||
}, | ||
], | ||
}, | ||
create: function(context) { | ||
// variables should be defined here | ||
create: function(context) { | ||
// variables should be defined here | ||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
//---------------------------------------------------------------------- | ||
// Helpers | ||
//---------------------------------------------------------------------- | ||
/** | ||
* Checks if a node name match the JSX tag convention. | ||
* @param {String} name - Name of the node to check. | ||
* @returns {boolean} Whether or not the node name match the JSX tag convention. | ||
*/ | ||
const tagConvention = /^[a-z]|-/; | ||
function isTagName(name) { | ||
return tagConvention.test(name); | ||
} | ||
/** | ||
* Checks if a node name match the JSX tag convention. | ||
* @param {String} name - Name of the node to check. | ||
* @returns {boolean} Whether or not the node name match the JSX tag convention. | ||
*/ | ||
const tagConvention = /^[a-z]|-/; | ||
function isTagName(name) { | ||
return tagConvention.test(name); | ||
} | ||
/** | ||
* Checks if a node name is allowed to have dangerous attribute. | ||
* @param {String} tagName - JSX tag name | ||
* @returns {boolean} Whether or not tag name is allowed to have dangerous attribute | ||
*/ | ||
function isAllowedTagName(name) { | ||
const config = context.options[0] || {}; | ||
const allowedTagNames = config.allowedTagNames || DEFAULTS; | ||
return allowedTagNames.indexOf(name) !== -1; | ||
} | ||
/** | ||
* Checks if a node name is allowed to have dangerous attribute. | ||
* @param {String} tagName - JSX tag name | ||
* @returns {boolean} Whether or not tag name is allowed to have dangerous attribute | ||
*/ | ||
function isAllowedTagName(name) { | ||
const config = context.options[0] || {}; | ||
const allowedTagNames = config.allowedTagNames || DEFAULTS; | ||
return allowedTagNames.indexOf(name) !== -1; | ||
} | ||
/** | ||
* Checks if a JSX attribute is dangerous. | ||
* @param {String} name - Name of the attribute to check. | ||
* @returns {boolean} Whether or not the attribute is dangerous. | ||
*/ | ||
function isDangerous(name) { | ||
return name === "dangerouslySetInnerHTML"; | ||
} | ||
/** | ||
* Checks if a JSX attribute is dangerous. | ||
* @param {String} name - Name of the attribute to check. | ||
* @returns {boolean} Whether or not the attribute is dangerous. | ||
*/ | ||
function isDangerous(name) { | ||
return name === 'dangerouslySetInnerHTML'; | ||
} | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
//---------------------------------------------------------------------- | ||
// Public | ||
//---------------------------------------------------------------------- | ||
return { | ||
JSXAttribute: function(node) { | ||
const tagName = node.parent.name.name; | ||
const attributeName = node.name.name; | ||
return { | ||
JSXAttribute: function(node) { | ||
const tagName = node.parent.name.name; | ||
const attributeName = node.name.name; | ||
if (isAllowedTagName(tagName)) { | ||
return; | ||
} | ||
if (isAllowedTagName(tagName)) { | ||
return; | ||
} | ||
if (isTagName(tagName) && isDangerous(attributeName)) { | ||
context.report({ | ||
node, | ||
message: `Cannot use dangerouslySetInnerHTML in ${tagName}` | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
if (isTagName(tagName) && isDangerous(attributeName)) { | ||
context.report({ | ||
node, | ||
message: `Cannot use dangerouslySetInnerHTML in ${tagName}`, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
{ | ||
"name": "eslint-plugin-marlint", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Traveloka custom eslint plugin", | ||
@@ -24,6 +24,3 @@ "keywords": [ | ||
"eslint": "^3.9.1", | ||
"husky": "^0.14.3", | ||
"lint-staged": "^5.0.0", | ||
"mocha": "^4.1.0", | ||
"prettier": "^1.16.4" | ||
"mocha": "^4.1.0" | ||
}, | ||
@@ -45,3 +42,3 @@ "engines": { | ||
"license": "MIT", | ||
"gitHead": "aca5783a4711b656cae0c0593bc3344f39a10565" | ||
"gitHead": "f8ce7c160bb4f6b86e41b419a856a8af87e70d1d" | ||
} |
@@ -45,2 +45,2 @@ # eslint-plugin-marlint | ||
* [limited-danger](docs/rules/limited-danger.md) | ||
- [limited-danger](docs/rules/limited-danger.md) |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
"use strict"; | ||
'use strict'; | ||
@@ -12,12 +12,12 @@ //------------------------------------------------------------------------------ | ||
const rule = require("../../../lib/rules/limited-danger"); | ||
const RuleTester = require("eslint").RuleTester; | ||
const rule = require('../../../lib/rules/limited-danger'); | ||
const RuleTester = require('eslint').RuleTester; | ||
const parserOptions = { | ||
ecmaVersion: 8, | ||
sourceType: "module", | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true | ||
} | ||
ecmaVersion: 8, | ||
sourceType: 'module', | ||
ecmaFeatures: { | ||
experimentalObjectRestSpread: true, | ||
jsx: true, | ||
}, | ||
}; | ||
@@ -30,15 +30,15 @@ | ||
const ruleTester = new RuleTester({ parserOptions }); | ||
ruleTester.run("limited-danger", rule, { | ||
valid: [ | ||
{ code: '<div>{"html"}</div>' }, | ||
{ code: '<style dangerouslySetInnerHTML={{ __html: "css" }} />' }, | ||
{ code: '<script dangerouslySetInnerHTML={{ __html: "js" }} />' } | ||
], | ||
ruleTester.run('limited-danger', rule, { | ||
valid: [ | ||
{ code: '<div>{"html"}</div>' }, | ||
{ code: '<style dangerouslySetInnerHTML={{ __html: "css" }} />' }, | ||
{ code: '<script dangerouslySetInnerHTML={{ __html: "js" }} />' }, | ||
], | ||
invalid: [ | ||
{ | ||
code: '<div dangerouslySetInnerHTML={{ __html: "html" }} />', | ||
errors: [{ message: "Cannot use dangerouslySetInnerHTML in div" }] | ||
} | ||
] | ||
invalid: [ | ||
{ | ||
code: '<div dangerouslySetInnerHTML={{ __html: "html" }} />', | ||
errors: [{ message: 'Cannot use dangerouslySetInnerHTML in div' }], | ||
}, | ||
], | ||
}); |
8388
3