Skip to content

Chores: Add JSDoc type checking with TypeScript #1206

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 4 commits into from
Jun 12, 2020
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
18 changes: 0 additions & 18 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,6 @@ module.exports = {
'dot-notation': 'error'
},
overrides: [
// Introduce prettier. but ignore files to avoid conflicts with PR.
{
files: [
// https://github.com/vuejs/eslint-plugin-vue/pull/819
'lib/rules/attributes-order.js',
'tests/lib/rules/attributes-order.js'
],
extends: [
'plugin:eslint-plugin/recommended',
'plugin:vue-libs/recommended'
],
rules: {
'prettier/prettier': 'off',

'rest-spread-spacing': 'error',
'no-mixed-operators': 'error'
}
},
{
files: ['lib/rules/*.js'],
rules: {
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
yarn.lock
yarn-error.log
docs/.vuepress/dist
typings/eslint/lib/rules
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"eslint.validate": [
"javascript",
"javascriptreact",
{ "language": "vue", "autoFix": true }
]
"vue"
],
"typescript.tsdk": "node_modules/typescript/lib"
}
7 changes: 7 additions & 0 deletions docs/developer-guide/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,10 @@ Check out an [example rule](https://github.com/vuejs/eslint-plugin-vue/blob/mast
Please be aware that regarding what kind of code examples you'll write in tests, you'll have to accordingly setup the parser in `RuleTester` (you can do it on per test case basis though). [See an example here](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/attribute-hyphenation.js#L19)

If you'll stuck, remember there are plenty of rules you can learn from already, and if you can't find the right solution - don't hesitate to reach out in issues. We're happy to help!

## :white_check_mark: JSDoc type checking with TypeScript

We have type checking enabled via TypeScript and JSDoc.
The command to perform type checking is: `npm run tsc`

This is just to help you write the rules, not to do strict type checking. If you find it difficult to resolve type checking warnings, feel free to suppress warnings using the `// @ts-nocheck` and `// @ts-ignore` comment.
24 changes: 13 additions & 11 deletions lib/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/

module.exports = {
/** @param {string} code */
preprocess(code) {
return [code]
},
Expand All @@ -34,6 +35,7 @@ module.exports = {
disableRuleKeys: new Map()
}
}
/** @type {string[]} */
const usedDisableDirectiveKeys = []
/** @type {Map<string,LintMessage>} */
const unusedDisableDirectiveReports = new Map()
Expand Down Expand Up @@ -88,15 +90,15 @@ module.exports = {
if (state.line.disableAllKeys.size) {
disableDirectiveKeys.push(...state.line.disableAllKeys)
}
if (state.block.disableRuleKeys.has(message.ruleId)) {
disableDirectiveKeys.push(
...state.block.disableRuleKeys.get(message.ruleId)
)
}
if (state.line.disableRuleKeys.has(message.ruleId)) {
disableDirectiveKeys.push(
...state.line.disableRuleKeys.get(message.ruleId)
)
if (message.ruleId) {
const block = state.block.disableRuleKeys.get(message.ruleId)
if (block) {
disableDirectiveKeys.push(...block)
}
const line = state.line.disableRuleKeys.get(message.ruleId)
if (line) {
disableDirectiveKeys.push(...line)
}
}

if (disableDirectiveKeys.length) {
Expand Down Expand Up @@ -153,8 +155,8 @@ function messageToKey(message) {

/**
* Compares the locations of two objects in a source file
* @param {{line: number, column: number}} itemA The first object
* @param {{line: number, column: number}} itemB The second object
* @param {Position} itemA The first object
* @param {Position} itemB The second object
* @returns {number} A value less than 1 if itemA appears before itemB in the source file, greater than 1 if
* itemA appears after itemB in the source file, or 0 if itemA and itemB have the same location.
*/
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/arrow-spacing.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

const { wrapCoreRule } = require('../utils')

// eslint-disable-next-line
// eslint-disable-next-line no-invalid-meta, no-invalid-meta-docs-categories
module.exports = wrapCoreRule(require('eslint/lib/rules/arrow-spacing'))
13 changes: 11 additions & 2 deletions lib/rules/attribute-hyphenation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {
}
]
},

/** @param {RuleContext} context */
create(context) {
const sourceCode = context.getSourceCode()
const option = context.options[0]
Expand All @@ -61,6 +61,10 @@ module.exports = {
useHyphenated ? 'kebab-case' : 'camelCase'
)

/**
* @param {VDirective | VAttribute} node
* @param {string} name
*/
function reportIssue(node, name) {
const text = sourceCode.getText(node.key)

Expand All @@ -78,6 +82,9 @@ module.exports = {
})
}

/**
* @param {string} value
*/
function isIgnoredAttribute(value) {
const isIgnored = ignoredAttributes.some((attr) => {
return value.indexOf(attr) !== -1
Expand All @@ -101,7 +108,9 @@ module.exports = {
const name = !node.directive
? node.key.rawName
: node.key.name.name === 'bind'
? node.key.argument && node.key.argument.rawName
? node.key.argument &&
node.key.argument.type === 'VIdentifier' &&
node.key.argument.rawName
: /* otherwise */ false
if (!name || isIgnoredAttribute(name)) return

Expand Down
Loading