Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New: Add
vue/name-property-required
rule
- Loading branch information
1 parent
0faf4c7
commit 9ca878f
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,44 @@ | ||
/** | ||
* @fileoverview Require a name property in Vue components | ||
* @author LukeeeeBennett | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
function isNameProperty (node) { | ||
return node.type === 'Property' && node.key.name === 'name' | ||
} | ||
|
||
function hasTruthyLiteralValue (node) { | ||
return node.value.type === 'Literal' && node.value.value | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'require a name property in Vue components', | ||
category: undefined, | ||
url: 'https://eslint.vuejs.org/rules/name-property-required.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
|
||
create (context) { | ||
return utils.executeOnVue(context, component => { | ||
const isValid = component.properties.some(property => { | ||
return isNameProperty(property) && | ||
hasTruthyLiteralValue(property) | ||
}) | ||
|
||
if (isValid) return | ||
|
||
context.report({ | ||
node: component, | ||
message: 'Required name property is undefined.' | ||
}) | ||
}) | ||
} | ||
} |
This file contains 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,96 @@ | ||
/** | ||
* @fileoverview Require a name property in Vue components | ||
* @author LukeeeeBennett | ||
*/ | ||
'use strict' | ||
|
||
const rule = require('../../../lib/rules/name-property-required') | ||
const RuleTester = require('eslint').RuleTester | ||
|
||
const parserOptions = { | ||
ecmaVersion: 2018, | ||
sourceType: 'module' | ||
} | ||
|
||
const ruleTester = new RuleTester() | ||
ruleTester.run('name-property-required', rule, { | ||
|
||
valid: [ | ||
{ | ||
filename: 'ValidComponent.vue', | ||
code: ` | ||
export default { | ||
name: 'IssaName' | ||
} | ||
`, | ||
parserOptions | ||
} | ||
], | ||
|
||
invalid: [ | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is undefined.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
nameNot: 'IssaNameNot' | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is undefined.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
name: '' | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is undefined.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
name: undefined | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is undefined.', | ||
type: 'ObjectExpression' | ||
}] | ||
}, | ||
{ | ||
filename: 'InvalidComponent.vue', | ||
code: ` | ||
export default { | ||
name: null | ||
} | ||
`, | ||
parserOptions, | ||
errors: [{ | ||
message: 'Required name property is undefined.', | ||
type: 'ObjectExpression' | ||
}] | ||
} | ||
] | ||
}) |