Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change display-name's acceptTranspilerName to ignoreTranspilerName #440

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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