Skip to content

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 1 commit into from
Aug 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/rules/require-prop-types.md
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.
4 changes: 2 additions & 2 deletions lib/rules/name-property-casing.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ function create (context) {

return utils.executeOnVue(context, (obj) => {
const node = obj.properties
.filter(item => (
.find(item => (
item.type === 'Property' &&
item.key.name === 'name' &&
item.value.type === 'Literal'
))[0]
))

if (!node) return

Expand Down
94 changes: 94 additions & 0 deletions lib/rules/require-prop-types.js
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
}
46 changes: 44 additions & 2 deletions lib/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,18 +272,60 @@ module.exports = {
return members.reverse()
},

/**
* Gets the property name of a given node.
* @param {ASTNode} node - The node to get.
* @return {string|null} The property name if static. Otherwise, null.
*/
getStaticPropertyName (node) {
Copy link
Member

@michalsnik michalsnik Jul 23, 2017

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@michalsnik added

Copy link
Member

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).

Copy link
Contributor Author

@armano2 armano2 Jul 23, 2017

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.

let prop
switch (node && node.type) {
case 'Property':
case 'MethodDefinition':
prop = node.key
break
case 'MemberExpression':
prop = node.property
break
case 'Literal':
case 'TemplateLiteral':
case 'Identifier':
prop = node
break
// no default
}

switch (prop && prop.type) {
case 'Literal':
return String(prop.value)
case 'TemplateLiteral':
if (prop.expressions.length === 0 && prop.quasis.length === 1) {
return prop.quasis[0].value.cooked
}
break
case 'Identifier':
if (!node.computed) {
return prop.name
}
break
// no default
}

return null
},

/**
* Get all computed properties by looking at all component's properties
* @param {ObjectExpression} Object with component definition
* @return {Array} Array of computed properties in format: [{key: String, value: ASTNode}]
*/
getComputedProperties (componentObject) {
const computedPropertiesNode = componentObject.properties
.filter(p =>
.find(p =>
p.key.type === 'Identifier' &&
p.key.name === 'computed' &&
p.value.type === 'ObjectExpression'
)[0]
)

if (!computedPropertiesNode) { return [] }

Expand Down
150 changes: 150 additions & 0 deletions tests/lib/rules/require-prop-types.js
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
}]
}
]
})
Loading