Skip to content

Commit 5c1f029

Browse files
committed
Add auto fix for no-unknown-property
1 parent 3a1b6ac commit 5c1f029

File tree

4 files changed

+22
-4
lines changed

4 files changed

+22
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Finally, enable all of the rules that you would like to use.
144144
* [no-multi-comp](docs/rules/no-multi-comp.md): Prevent multiple component definition per file
145145
* [no-set-state](docs/rules/no-set-state.md): Prevent usage of `setState`
146146
* [no-string-refs](docs/rules/no-string-refs.md): Prevent using string references in `ref` attribute.
147-
* [no-unknown-property](docs/rules/no-unknown-property.md): Prevent usage of unknown DOM property
147+
* [no-unknown-property](docs/rules/no-unknown-property.md): Prevent usage of unknown DOM property (fixable)
148148
* [prefer-es6-class](docs/rules/prefer-es6-class.md): Enforce ES5 or ES6 class for React Components
149149
* [prop-types](docs/rules/prop-types.md): Prevent missing props validation in a React component definition
150150
* [react-in-jsx-scope](docs/rules/react-in-jsx-scope.md): Prevent missing `React` when using JSX

docs/rules/no-unknown-property.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
In JSX all DOM properties and attributes should be camelCased to be consistent with standard JavaScript style. This can be a possible source of error if you are used to write plain HTML.
44

5+
**Fixable:** This rule is automatically fixable using the `--fix` flag on the command line.
6+
57
## Rule Details
68

79
The following patterns are considered warnings:

lib/rules/no-unknown-property.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,16 @@ module.exports = function(context) {
116116
if (!isTagName(node) || !standardName) {
117117
return;
118118
}
119-
context.report(node, UNKNOWN_MESSAGE, {
120-
name: name,
121-
standardName: standardName
119+
context.report({
120+
node: node,
121+
message: UNKNOWN_MESSAGE,
122+
data: {
123+
name: name,
124+
standardName: standardName
125+
},
126+
fix: function(fixer) {
127+
return fixer.replaceText(node.name, standardName);
128+
}
122129
});
123130
}
124131
};

tests/lib/rules/no-unknown-property.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,38 +40,47 @@ ruleTester.run('no-unknown-property', rule, {
4040
],
4141
invalid: [{
4242
code: '<div class="bar"></div>;',
43+
output: '<div className="bar"></div>;',
4344
errors: [{message: 'Unknown property \'class\' found, use \'className\' instead'}],
4445
parserOptions: parserOptions
4546
}, {
4647
code: '<div for="bar"></div>;',
48+
output: '<div htmlFor="bar"></div>;',
4749
errors: [{message: 'Unknown property \'for\' found, use \'htmlFor\' instead'}],
4850
parserOptions: parserOptions
4951
}, {
5052
code: '<div accept-charset="bar"></div>;',
53+
output: '<div acceptCharset="bar"></div>;',
5154
errors: [{message: 'Unknown property \'accept-charset\' found, use \'acceptCharset\' instead'}],
5255
parserOptions: parserOptions
5356
}, {
5457
code: '<div http-equiv="bar"></div>;',
58+
output: '<div httpEquiv="bar"></div>;',
5559
errors: [{message: 'Unknown property \'http-equiv\' found, use \'httpEquiv\' instead'}],
5660
parserOptions: parserOptions
5761
}, {
5862
code: '<div accesskey="bar"></div>;',
63+
output: '<div accessKey="bar"></div>;',
5964
errors: [{message: 'Unknown property \'accesskey\' found, use \'accessKey\' instead'}],
6065
parserOptions: parserOptions
6166
}, {
6267
code: '<div onclick="bar"></div>;',
68+
output: '<div onClick="bar"></div>;',
6369
errors: [{message: 'Unknown property \'onclick\' found, use \'onClick\' instead'}],
6470
parserOptions: parserOptions
6571
}, {
6672
code: '<div onmousedown="bar"></div>;',
73+
output: '<div onMouseDown="bar"></div>;',
6774
errors: [{message: 'Unknown property \'onmousedown\' found, use \'onMouseDown\' instead'}],
6875
parserOptions: parserOptions
6976
}, {
7077
code: '<use xlink:href="bar" />;',
78+
output: '<use xlinkHref="bar" />;',
7179
errors: [{message: 'Unknown property \'xlink:href\' found, use \'xlinkHref\' instead'}],
7280
parserOptions: parserOptions
7381
}, {
7482
code: '<rect clip-path="bar" />;',
83+
output: '<rect clipPath="bar" />;',
7584
errors: [{message: 'Unknown property \'clip-path\' found, use \'clipPath\' instead'}],
7685
parserOptions: parserOptions
7786
}]

0 commit comments

Comments
 (0)