Skip to content

Commit

Permalink
Merge 6ba4150 into 80f3826
Browse files Browse the repository at this point in the history
  • Loading branch information
geshwho committed Apr 3, 2020
2 parents 80f3826 + 6ba4150 commit 1208d4c
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 6 deletions.
5 changes: 4 additions & 1 deletion docs/rules/forbid-component-props.md
Expand Up @@ -35,7 +35,7 @@ The following patterns are **not** considered warnings:

```js
...
"react/forbid-component-props": [<enabled>, { "forbid": [<string>|<object>] }]
"react/forbid-component-props": [<enabled>, { "forbid": [<string>|<object>], "message": <string> }]
...
```

Expand All @@ -51,6 +51,9 @@ Each array element can either be a string with the property name or object speci
}
```

### `message`

A custom message to provide with the error. The default message is ``Prop `${prop}` is forbidden on Components``

### Related rules

Expand Down
5 changes: 4 additions & 1 deletion docs/rules/forbid-dom-props.md
Expand Up @@ -36,14 +36,17 @@ The following patterns are **not** considered warnings:

```js
...
"react/forbid-dom-props": [<enabled>, { "forbid": [<string>] }]
"react/forbid-dom-props": [<enabled>, { "forbid": [<string>], "message": <string> }]
...
```

### `forbid`

An array of strings, with the names of props that are forbidden. The default value of this option `[]`.

### `message`

A custom message to provide with the error. The default message is ``Prop `${prop}` is forbidden on DOM Nodes``

### Related rules

Expand Down
8 changes: 7 additions & 1 deletion lib/rules/forbid-component-props.js
Expand Up @@ -50,6 +50,9 @@ module.exports = {
}
}]
}
},
message: {
type: 'string'
}
}
}]
Expand All @@ -62,6 +65,7 @@ module.exports = {
const whitelist = typeof value === 'string' ? [] : (value.allowedFor || []);
return [propName, whitelist];
}));
const customMessage = configuration.message;

function isForbidden(prop, tagName) {
const whitelist = forbid.get(prop);
Expand All @@ -83,9 +87,11 @@ module.exports = {
return;
}

const errorMessage = customMessage || `Prop \`${prop}\` is forbidden on Components`;

context.report({
node,
message: `Prop \`${prop}\` is forbidden on Components`
message: errorMessage
});
}
};
Expand Down
12 changes: 9 additions & 3 deletions lib/rules/forbid-dom-props.js
Expand Up @@ -36,16 +36,20 @@ module.exports = {
minLength: 1
},
uniqueItems: true
},
message: {
type: 'string'
}
},
additionalProperties: false
}]
},

create(context) {
function isForbidden(prop) {
const configuration = context.options[0] || {};
const configuration = context.options[0] || {};
const customMessage = configuration.message;

function isForbidden(prop) {
const forbid = configuration.forbid || DEFAULTS;
return forbid.indexOf(prop) >= 0;
}
Expand All @@ -64,9 +68,11 @@ module.exports = {
return;
}

const errorMessage = customMessage || `Prop \`${prop}\` is forbidden on DOM Nodes`;

context.report({
node,
message: `Prop \`${prop}\` is forbidden on DOM Nodes`
message: errorMessage
});
}
};
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/forbid-component-props.js
Expand Up @@ -190,5 +190,17 @@ ruleTester.run('forbid-component-props', rule, {
column: 32,
type: 'JSXAttribute'
}]
}, {
code: 'const item = (<Foo className="foo" />);',
options: [{
forbid: [{propName: 'className'}],
message: 'Please use ourCoolClassName instead of ClassName'
}],
errors: [{
message: 'Please use ourCoolClassName instead of ClassName',
line: 1,
column: 20,
type: 'JSXAttribute'
}]
}]
});
16 changes: 16 additions & 0 deletions tests/lib/rules/forbid-dom-props.js
Expand Up @@ -126,5 +126,21 @@ ruleTester.run('forbid-element-props', rule, {
column: 8,
type: 'JSXAttribute'
}]
}, {
code: [
'const First = (props) => (',
' <div className="foo" />',
');'
].join('\n'),
options: [{
forbid: ['className'],
message: 'Please use class instead of ClassName'
}],
errors: [{
message: 'Please use class instead of ClassName',
line: 2,
column: 8,
type: 'JSXAttribute'
}]
}]
});

0 comments on commit 1208d4c

Please sign in to comment.