Skip to content

Commit

Permalink
Change display-name's acceptTranspilerName to ignoreTranspilerName
Browse files Browse the repository at this point in the history
This was a change that has been queued for v4. I considered changing the
default from false to true, but I think it makes more sense to invert
the option to be an ignore option.

Addresses #436
  • Loading branch information
lencioni committed Feb 13, 2016
1 parent 442d20b commit 1bb7811
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 58 deletions.
33 changes: 30 additions & 3 deletions docs/rules/display-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ var Hello = React.createClass({
...
```

### `acceptTranspilerName`
### `ignoreTranspilerName`

When `true` the rule will accept the name set by the transpiler and does not require a `displayName` property in this case.
When `true` the rule will ignore the name set by the transpiler and require a `displayName` property in this case.

The following patterns are considered okay and do not cause warnings:

```js
var Hello = React.createClass({
displayName: 'Hello',

render: function() {
return <div>Hello {this.props.name}</div>;
}
Expand All @@ -54,9 +56,34 @@ export default class Hello extends React.Component {
return <div>Hello {this.props.name}</div>;
}
}
Hello.displayName = 'Hello';
```

```js
export default function Hello({ name }) {
return <div>Hello {name}</div>;
}
Hello.displayName = 'Hello';
```

With the following patterns the transpiler can not assign a name for the component and therefore it will still cause warnings:
The following patterns will cause warnings:

```js
var Hello = React.createClass({
render: function() {
return <div>Hello {this.props.name}</div>;
}
});
module.exports = Hello;
```

```js
export default class Hello extends React.Component {
render() {
return <div>Hello {this.props.name}</div>;
}
}
```

```js
module.exports = React.createClass({
Expand Down
14 changes: 7 additions & 7 deletions lib/rules/display-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = Components.detect(function(context, components, utils) {

var sourceCode = context.getSourceCode();
var config = context.options[0] || {};
var acceptTranspilerName = config.acceptTranspilerName || false;
var ignoreTranspilerName = config.ignoreTranspilerName || false;

var MISSING_MESSAGE = 'Component definition is missing display name';

Expand Down Expand Up @@ -141,21 +141,21 @@ module.exports = Components.detect(function(context, components, utils) {
},

FunctionExpression: function(node) {
if (!acceptTranspilerName || !hasTranspilerName(node)) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
return;
}
markDisplayNameAsDeclared(node);
},

FunctionDeclaration: function(node) {
if (!acceptTranspilerName || !hasTranspilerName(node)) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
return;
}
markDisplayNameAsDeclared(node);
},

ArrowFunctionExpression: function(node) {
if (!acceptTranspilerName || !hasTranspilerName(node)) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
return;
}
markDisplayNameAsDeclared(node);
Expand All @@ -169,14 +169,14 @@ module.exports = Components.detect(function(context, components, utils) {
},

ClassDeclaration: function(node) {
if (!acceptTranspilerName || !hasTranspilerName(node)) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
return;
}
markDisplayNameAsDeclared(node);
},

ObjectExpression: function(node) {
if (!acceptTranspilerName || !hasTranspilerName(node)) {
if (ignoreTranspilerName || !hasTranspilerName(node)) {
// Search for the displayName declaration
node.properties.forEach(function(property) {
if (!property.key || !isDisplayNameDeclaration(property.key)) {
Expand Down Expand Up @@ -205,7 +205,7 @@ module.exports = Components.detect(function(context, components, utils) {
module.exports.schema = [{
type: 'object',
properties: {
acceptTranspilerName: {
ignoreTranspilerName: {
type: 'boolean'
}
},
Expand Down

0 comments on commit 1bb7811

Please sign in to comment.