Skip to content

Commit

Permalink
Fix checking for script setup in vue/require-typed-ref (#2427)
Browse files Browse the repository at this point in the history
  • Loading branch information
kovalchukq authored May 9, 2024
1 parent 98e030a commit a3c98d8
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/rules/require-typed-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,23 @@ module.exports = {
return {}
}

const scriptSetup = utils.getScriptSetupElement(context)
if (
scriptSetup &&
!utils.hasAttribute(scriptSetup, 'lang', 'ts') &&
!utils.hasAttribute(scriptSetup, 'lang', 'typescript')
) {
return {}
if (utils.isVueFile(filename)) {
const sourceCode = context.getSourceCode()
const documentFragment =
sourceCode.parserServices.getDocumentFragment &&
sourceCode.parserServices.getDocumentFragment()
if (!documentFragment) {
return {}
}
const scripts = documentFragment.children.filter(
/** @returns {element is VElement} */
(element) => utils.isVElement(element) && element.name === 'script'
)
if (
scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))
) {
return {}
}
}

const defines = iterateDefineRefs(
Expand Down
38 changes: 38 additions & 0 deletions tests/lib/rules/require-typed-ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ tester.run('require-typed-ref', rule, {
`,
languageOptions: { parser: require('vue-eslint-parser') }
},
{
filename: 'test.vue',
code: `
<script>
import { ref } from 'vue'
export default {
setup() {
const count = ref()
}
}
</script>
`,
languageOptions: { parser: require('vue-eslint-parser') }
},
{
filename: 'test.js',
code: `
Expand Down Expand Up @@ -217,6 +231,30 @@ tester.run('require-typed-ref', rule, {
],
languageOptions: { parser: require('vue-eslint-parser') }
},
{
filename: 'test.vue',
code: `
<script lang="ts">
import { ref } from 'vue'
export default {
setup() {
const count = ref()
}
}
</script>
}
`,
errors: [
{
messageId: 'noType',
line: 6,
column: 29,
endLine: 6,
endColumn: 34
}
],
languageOptions: { parser: require('vue-eslint-parser') }
},
{
filename: 'test.ts',
code: `
Expand Down

0 comments on commit a3c98d8

Please sign in to comment.