Skip to content

Commit

Permalink
Fix false positive for unknown prop in vue/no-undef-properties rule (
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed May 22, 2023
1 parent 30931f0 commit d93f2c6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
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
}
]
}
]
})

0 comments on commit d93f2c6

Please sign in to comment.