-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add require-prop-types
rule.
#85
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Prop definitions should be detailed (require-prop-types) | ||
|
||
In committed code, prop definitions should always be as detailed as possible, specifying at least type(s). | ||
|
||
## :book: Rule Details | ||
|
||
This rule enforces that a `props` statement contains type definition. | ||
|
||
:-1: Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: ['status'] | ||
} | ||
``` | ||
|
||
:+1: Examples of **correct** code for this rule: | ||
|
||
```js | ||
export default { | ||
props: { | ||
status: String | ||
} | ||
} | ||
``` | ||
|
||
```js | ||
export default { | ||
props: { | ||
status: { | ||
type: String, | ||
required: true, | ||
validate: function (value) { | ||
return ['syncing', 'synced', 'version-conflict', 'error'].indexOf(value) !== -1 | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
## :wrench: Options | ||
|
||
Nothing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/** | ||
* @fileoverview Prop definitions should be detailed | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
function create (context) { | ||
// ---------------------------------------------------------------------- | ||
// Helpers | ||
// ---------------------------------------------------------------------- | ||
|
||
function objectHasType (node) { | ||
const typeProperty = node.properties | ||
.find(p => | ||
utils.getStaticPropertyName(p.key) === 'type' && | ||
( | ||
p.value.type !== 'ArrayExpression' || | ||
p.value.elements.length > 0 | ||
) | ||
) | ||
return Boolean(typeProperty) | ||
} | ||
|
||
function checkProperties (items) { | ||
for (const cp of items) { | ||
if (cp.type !== 'Property') { | ||
return | ||
} | ||
let hasType = true | ||
if (cp.value.type === 'ObjectExpression') { // foo: { | ||
hasType = objectHasType(cp.value) | ||
} else if (cp.value.type === 'ArrayExpression') { // foo: [ | ||
hasType = cp.value.elements.length > 0 | ||
} else if (cp.value.type === 'FunctionExpression' || cp.value.type === 'ArrowFunctionExpression') { | ||
hasType = false | ||
} | ||
if (!hasType) { | ||
context.report({ | ||
node: cp, | ||
message: 'Prop "{{name}}" should define at least it\'s type.', | ||
data: { | ||
name: cp.key.name | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// ---------------------------------------------------------------------- | ||
// Public | ||
// ---------------------------------------------------------------------- | ||
|
||
return utils.executeOnVue(context, (obj) => { | ||
const node = obj.properties | ||
.find(p => | ||
p.type === 'Property' && | ||
p.key.type === 'Identifier' && | ||
p.key.name === 'props' | ||
) | ||
|
||
if (!node) return | ||
|
||
if (node.value.type === 'ObjectExpression') { | ||
checkProperties(node.value.properties) | ||
} else { | ||
context.report({ | ||
node, | ||
message: 'Props should at least define their types.' | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Prop definitions should be detailed', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
fixable: null, // or "code" or "whitespace" | ||
schema: [ | ||
// fill in your schema | ||
] | ||
}, | ||
|
||
create | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/** | ||
* @fileoverview Prop definitions should be detailed | ||
* @author Armano | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/require-prop-types') | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester() | ||
ruleTester.run('require-prop-types', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
...test(), | ||
foo: String | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module', ecmaFeatures: { experimentalObjectRestSpread: true }} | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo: [String, Number] | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' } | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo: { | ||
type: String | ||
} | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' } | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo: { | ||
['type']: String | ||
} | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' } | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: ['foo'] | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: [{ | ||
message: 'Props should at least define their types.', | ||
line: 3 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.js', | ||
code: ` | ||
new Vue({ | ||
props: ['foo'] | ||
}) | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: [{ | ||
message: 'Props should at least define their types.', | ||
line: 3 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo: { | ||
} | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: [{ | ||
message: 'Prop "foo" should define at least it\'s type.', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo: { | ||
type: [] | ||
} | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: [{ | ||
message: 'Prop "foo" should define at least it\'s type.', | ||
line: 4 | ||
}] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
export default { | ||
props: { | ||
foo() {} | ||
} | ||
} | ||
`, | ||
parserOptions: { ecmaVersion: 6, sourceType: 'module' }, | ||
errors: [{ | ||
message: 'Prop "foo" should define at least it\'s type.', | ||
line: 4 | ||
}] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add unit tests for this method, I just realised they're not there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsnik added
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's sweet memories, I wrote it at eslint/lib/ast-utils.
I hope
eslint/lib/ast-utils
to separate to another package (though it's an item of ESLint 5 wish list).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this method is not same but almost, and ast-utils is not exported than i will have to import file directly what can cause some errors between versions of eslint.