Skip to content

Commit

Permalink
ES6-ify void-dom-elements-no-children rule and test
Browse files Browse the repository at this point in the history
  • Loading branch information
dfilipidisz committed Jun 25, 2017
1 parent 61b65a0 commit ae92a73
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 59 deletions.
24 changes: 12 additions & 12 deletions lib/rules/void-dom-elements-no-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
*/
'use strict';

var has = require('has');
const has = require('has');

var Components = require('../util/Components');
const Components = require('../util/Components');

// ------------------------------------------------------------------------------
// Helpers
// ------------------------------------------------------------------------------

// Using an object here to avoid array scan. We should switch to Set once
// support is good enough.
var VOID_DOM_ELEMENTS = {
const VOID_DOM_ELEMENTS = {
area: true,
base: true,
br: true,
Expand Down Expand Up @@ -56,10 +56,10 @@ module.exports = {
schema: []
},

create: Components.detect(function(context, components, utils) {
create: Components.detect((context, components, utils) => {
return {
JSXElement: function(node) {
var elementName = node.openingElement.name.name;
const elementName = node.openingElement.name.name;

if (!isVoidDOMElement(elementName)) {
// e.g. <div />
Expand All @@ -74,9 +74,9 @@ module.exports = {
});
}

var attributes = node.openingElement.attributes;
const attributes = node.openingElement.attributes;

var hasChildrenAttributeOrDanger = attributes.some(function(attribute) {
const hasChildrenAttributeOrDanger = attributes.some((attribute) => {
if (!attribute.name) {
return false;
}
Expand All @@ -102,8 +102,8 @@ module.exports = {
return;
}

var args = node.arguments;
var elementName = args[0].value;
const args = node.arguments;
const elementName = args[0].value;

if (!isVoidDOMElement(elementName)) {
// e.g. React.createElement('div');
Expand All @@ -114,7 +114,7 @@ module.exports = {
return;
}

var firstChild = args[2];
const firstChild = args[2];
if (firstChild) {
// e.g. React.createElement('br', undefined, 'Foo')
context.report({
Expand All @@ -123,9 +123,9 @@ module.exports = {
});
}

var props = args[1].properties;
const props = args[1].properties;

var hasChildrenPropOrDanger = props.some(function(prop) {
const hasChildrenPropOrDanger = props.some((prop) => {
if (!prop.key) {
return false;
}
Expand Down
91 changes: 44 additions & 47 deletions tests/lib/rules/void-dom-elements-no-children.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
// Requirements
// -----------------------------------------------------------------------------

var rule = require('../../../lib/rules/void-dom-elements-no-children');
var RuleTester = require('eslint').RuleTester;
const rule = require('../../../lib/rules/void-dom-elements-no-children');
const RuleTester = require('eslint').RuleTester;

var parserOptions = {
const parserOptions = {
ecmaVersion: 8,
sourceType: 'module',
ecmaFeatures: {
Expand All @@ -29,7 +29,7 @@ function errorMessage(elementName) {
// Tests
// -----------------------------------------------------------------------------

var ruleTester = new RuleTester({parserOptions});
const ruleTester = new RuleTester({parserOptions});
ruleTester.run('void-dom-elements-no-children', rule, {
valid: [
{
Expand All @@ -49,39 +49,39 @@ ruleTester.run('void-dom-elements-no-children', rule, {
},
{
code: 'React.createElement("div", { dangerouslySetInnerHTML: { __html: "Foo" } });'
}, {
code: 'document.createElement("img")'
}, {
},
{
code: 'document.createElement("img");'
},
{
code: 'React.createElement("img");'
}, {
code: [
'const props = {}',
'React.createElement("img", props)'
].join('\n')
code: `
const props = {};
React.createElement("img", props);
`
}, {
code: [
'import React from "react";',
'const { createElement } = React;',
'createElement("div")'
].join('\n')
code: `
import React, {createElement} from "react";
createElement("div");
`
}, {
code: [
'import React from "react";',
'const { createElement } = React;',
'createElement("img")'
].join('\n')
code: `
import React, {createElement} from "react";
createElement("img");
`
}, {
code: [
'import React, {createElement, PureComponent} from \'react\';',
'class Button extends PureComponent {',
' handleClick(ev) {',
' ev.preventDefault();',
' }',
' render() {',
' return <div onClick={this.handleClick}>Hello</div>;',
' }',
'}'
].join('\n')
code: `
import React, {createElement, PureComponent} from "react";
class Button extends PureComponent {
handleClick(ev) {
ev.preventDefault();
}
render() {
return <div onClick={this.handleClick}>Hello</div>;
}
}
`
}
],
invalid: [
Expand Down Expand Up @@ -114,29 +114,26 @@ ruleTester.run('void-dom-elements-no-children', rule, {
errors: [{message: errorMessage('br')}]
},
{
code: [
'import React from "react";',
'const createElement = React.createElement;',
'createElement("img", {}, "Foo");'
].join('\n'),
code: `
import React, {createElement} from "react";
createElement("img", {}, "Foo");
`,
errors: [{message: errorMessage('img')}],
parser: 'babel-eslint'
},
{
code: [
'import React from "react";',
'const createElement = React.createElement;',
'createElement("img", { children: "Foo" });'
].join('\n'),
code: `
import React, {createElement} from "react";
createElement("img", { children: "Foo" });
`,
errors: [{message: errorMessage('img')}],
parser: 'babel-eslint'
},
{
code: [
'import React from "react";',
'const createElement = React.createElement;',
'createElement("img", { dangerouslySetInnerHTML: { __html: "Foo" } });'
].join('\n'),
code: `
import React, {createElement} from "react";
createElement("img", { dangerouslySetInnerHTML: { __html: "Foo" } });
`,
errors: [{message: errorMessage('img')}],
parser: 'babel-eslint'
}
Expand Down

0 comments on commit ae92a73

Please sign in to comment.