Skip to content
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

Fix false positive for unknown prop in vue/no-undef-properties rule #2186

Merged
merged 4 commits into from May 22, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/rules/no-undef-properties.js
Expand Up @@ -127,6 +127,8 @@ module.exports = {

/** @type { Set<string | ASTNode> } */
this.reported = new Set()

this.hasUnknownProperty = false
}
/**
* Report
Expand All @@ -135,6 +137,7 @@ module.exports = {
* @param {boolean} [options.props]
*/
verifyReferences(references, options) {
if (this.hasUnknownProperty) return
const report = this.report.bind(this)
verifyUndefProperties(this.defineProperties, references, null)

Expand Down Expand Up @@ -206,6 +209,10 @@ module.exports = {
}
})
}

markAsHasUnknownProperty() {
this.hasUnknownProperty = true
}
}

/** @type {Map<ASTNode, VueComponentContext>} */
Expand Down Expand Up @@ -280,6 +287,10 @@ module.exports = {
const ctx = getVueComponentContext(programNode)

for (const prop of props) {
if (prop.type === 'unknown') {
ctx.markAsHasUnknownProperty()
return
}
if (!prop.propName) {
continue
}
Expand Down
47 changes: 47 additions & 0 deletions tests/lib/rules/no-undef-properties.js
Expand Up @@ -6,6 +6,9 @@

const RuleTester = require('eslint').RuleTester
const rule = require('../../../lib/rules/no-undef-properties')
const {
getTypeScriptFixtureTestOptions
} = require('../../test-utils/typescript')

const tester = new RuleTester({
parser: require.resolve('vue-eslint-parser'),
Expand Down Expand Up @@ -535,6 +538,26 @@ tester.run('no-undef-properties', rule, {
},
};
</script>`
},

{
// unknown type
filename: 'test.vue',
code: `
<script setup lang="ts">
import type { Props1 } from './test01';
defineProps<Props1>();
</script>
<template>
<div>{{ foo }}</div>
<div>{{ unknown }}</div>
</template>`,
parserOptions: {
parser: require.resolve('@typescript-eslint/parser')
}
}
],

Expand Down Expand Up @@ -1129,6 +1152,30 @@ tester.run('no-undef-properties', rule, {
column: 46
}
]
},

{
// known type
filename: 'test.vue',
code: `
<script setup lang="ts">
import type { Props1 } from './test01';
defineProps<Props1>();
</script>
<template>
<div>{{ foo }}</div>
<div>{{ unknown }}</div>
</template>`,
...getTypeScriptFixtureTestOptions(),
errors: [
{
message: "'unknown' is not defined.",
line: 11
}
]
}
]
})