Skip to content

Commit

Permalink
Use ESLint string templating
Browse files Browse the repository at this point in the history
  • Loading branch information
louisscruz committed Dec 12, 2017
1 parent 9a7423e commit 37902d4
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
17 changes: 17 additions & 0 deletions docs/rules/boolean-prop-naming.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,20 @@ For supporting "is" naming:
### `message`

The custom message to display upon failure to match the rule. This overrides the default message.

If you choose to use a custom message, you have access to two template variables.

* `propName` – the name of the prop that does not match the pattern
* `pattern` – the pattern against which all prop names are tested

For example, if a prop is named `something`, and if the rule's pattern is set to `"^(is|has)[A-Z]([A-Za-z0-9]?)+"`, you could set the custom message as follows:

```js
message: 'It is better if your prop ({{ propName }}) matches this pattern: ({{ pattern }})'
```

And the failure would look like so:

```
It is better if your prop (something) matches this pattern: (^is[A-Z]([A-Za-z0-9]?)+)
```
6 changes: 4 additions & 2 deletions lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ module.exports = {
const propName = getPropName(propNode);
context.report({
node: propNode,
message: config.message || `Prop name (${propName}) doesn't match rule (${config.rule})`,
message: config.message || 'Prop name ({{ propName }}) doesn\'t match rule ({{ pattern }})',
data: {
component: propName
component: propName,
propName: propName,
pattern: config.rule
}
});
});
Expand Down
19 changes: 17 additions & 2 deletions tests/lib/rules/boolean-prop-naming.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,25 @@ ruleTester.run('boolean-prop-naming', rule, {
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+',
message: 'Boolean prop names must begin with either \'is\' or \'has\'.'
message: 'Boolean prop names must begin with either \'is\' or \'has\''
}],
errors: [{
message: 'Boolean prop names must begin with either \'is\' or \'has\'.'
message: 'Boolean prop names must begin with either \'is\' or \'has\''
}]
}, {
// Custom messages use ESLint string templating.
code: `
class Hello extends React.Component {
render () { return <div />; }
}
Hello.propTypes = {something: PropTypes.bool}
`,
options: [{
rule: '^is[A-Z]([A-Za-z0-9]?)+',
message: 'It is better if your prop ({{ propName }}) matches this pattern: ({{ pattern }})'
}],
errors: [{
message: 'It is better if your prop (something) matches this pattern: (^is[A-Z]([A-Za-z0-9]?)+)'
}]
}]
});

0 comments on commit 37902d4

Please sign in to comment.