Skip to content

Commit ff97a0b

Browse files
committed
Exclude JavaScript components in vue/require-typed-object-prop
1 parent 7f1a598 commit ff97a0b

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

lib/rules/require-typed-object-prop.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,30 @@ module.exports = {
129129
},
130130
/** @param {RuleContext} context */
131131
create(context) {
132+
const filename = context.getFilename()
133+
if (!utils.isVueFile(filename) && !utils.isTypeScriptFile(filename)) {
134+
return {}
135+
}
136+
137+
if (utils.isVueFile(filename)) {
138+
const sourceCode = context.getSourceCode()
139+
const documentFragment =
140+
sourceCode.parserServices.getDocumentFragment &&
141+
sourceCode.parserServices.getDocumentFragment()
142+
if (!documentFragment) {
143+
return {}
144+
}
145+
const scripts = documentFragment.children.filter(
146+
/** @returns {element is VElement} */
147+
(element) => utils.isVElement(element) && element.name === 'script'
148+
)
149+
if (
150+
scripts.every((script) => !utils.hasAttribute(script, 'lang', 'ts'))
151+
) {
152+
return {}
153+
}
154+
}
155+
132156
return utils.compositingVisitors(
133157
utils.defineScriptSetupVisitor(context, {
134158
onDefinePropsEnter(_node, props) {

tests/lib/rules/require-typed-object-prop.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,33 @@ ruleTester.run('require-typed-object-prop', rule, {
298298
});
299299
</script>
300300
`
301+
},
302+
// JavaScript components
303+
{
304+
filename: 'test.vue',
305+
code: `
306+
<script setup>
307+
defineProps({ foo: Object });
308+
</script>
309+
`
310+
},
311+
{
312+
filename: 'test.vue',
313+
code: `
314+
<script>
315+
export default Vue.extend({
316+
props: { foo: { type: Object } }
317+
});
318+
</script>
319+
`
320+
},
321+
{
322+
filename: 'test.js',
323+
code: `
324+
export default Vue.extend({
325+
props: { foo: Object }
326+
});
327+
`
301328
}
302329
],
303330
invalid: [

0 commit comments

Comments
 (0)