Skip to content

Commit

Permalink
Merge pull request #440 from lencioni/display-name-accept-transpiler-…
Browse files Browse the repository at this point in the history
…name-change-a-roo

Change display-name's acceptTranspilerName to ignoreTranspilerName
  • Loading branch information
yannickcr committed Feb 13, 2016
2 parents 442d20b + 1bb7811 commit e716f99
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
Loading

0 comments on commit e716f99

Please sign in to comment.