Skip to content

Commit 37902d4

Browse files
committed
Use ESLint string templating
1 parent 9a7423e commit 37902d4

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

docs/rules/boolean-prop-naming.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,20 @@ For supporting "is" naming:
6969
### `message`
7070

7171
The custom message to display upon failure to match the rule. This overrides the default message.
72+
73+
If you choose to use a custom message, you have access to two template variables.
74+
75+
* `propName` – the name of the prop that does not match the pattern
76+
* `pattern` – the pattern against which all prop names are tested
77+
78+
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:
79+
80+
```js
81+
message: 'It is better if your prop ({{ propName }}) matches this pattern: ({{ pattern }})'
82+
```
83+
84+
And the failure would look like so:
85+
86+
```
87+
It is better if your prop (something) matches this pattern: (^is[A-Z]([A-Za-z0-9]?)+)
88+
```

lib/rules/boolean-prop-naming.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,11 @@ module.exports = {
135135
const propName = getPropName(propNode);
136136
context.report({
137137
node: propNode,
138-
message: config.message || `Prop name (${propName}) doesn't match rule (${config.rule})`,
138+
message: config.message || 'Prop name ({{ propName }}) doesn\'t match rule ({{ pattern }})',
139139
data: {
140-
component: propName
140+
component: propName,
141+
propName: propName,
142+
pattern: config.rule
141143
}
142144
});
143145
});

tests/lib/rules/boolean-prop-naming.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,25 @@ ruleTester.run('boolean-prop-naming', rule, {
644644
`,
645645
options: [{
646646
rule: '^is[A-Z]([A-Za-z0-9]?)+',
647-
message: 'Boolean prop names must begin with either \'is\' or \'has\'.'
647+
message: 'Boolean prop names must begin with either \'is\' or \'has\''
648648
}],
649649
errors: [{
650-
message: 'Boolean prop names must begin with either \'is\' or \'has\'.'
650+
message: 'Boolean prop names must begin with either \'is\' or \'has\''
651+
}]
652+
}, {
653+
// Custom messages use ESLint string templating.
654+
code: `
655+
class Hello extends React.Component {
656+
render () { return <div />; }
657+
}
658+
Hello.propTypes = {something: PropTypes.bool}
659+
`,
660+
options: [{
661+
rule: '^is[A-Z]([A-Za-z0-9]?)+',
662+
message: 'It is better if your prop ({{ propName }}) matches this pattern: ({{ pattern }})'
663+
}],
664+
errors: [{
665+
message: 'It is better if your prop (something) matches this pattern: (^is[A-Z]([A-Za-z0-9]?)+)'
651666
}]
652667
}]
653668
});

0 commit comments

Comments
 (0)