Skip to content

Commit e7306e5

Browse files
authored
no-useless-undefined: Ignore ref(undefined) in Vue project (#1828)
1 parent 2c914d4 commit e7306e5

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

rules/no-useless-undefined.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ const shouldIgnore = node => {
7070
}
7171

7272
return compareFunctionNames.has(name)
73+
// https://vuejs.org/api/reactivity-core.html#ref
74+
|| name === 'ref'
7375
// `set.add(undefined)`
7476
|| name === 'add'
7577
// `map.set(foo, undefined)`

test/no-useless-undefined.mjs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import outdent from 'outdent';
2-
import {getTester} from './utils/test.mjs';
2+
import {getTester, parsers} from './utils/test.mjs';
33

44
const {test} = getTester(import.meta);
55

@@ -436,3 +436,36 @@ test.snapshot({
436436
'foo.notBind(bar, undefined)',
437437
],
438438
});
439+
440+
test.snapshot({
441+
testerOptions: {
442+
parser: parsers.vue,
443+
},
444+
valid: [
445+
outdent`
446+
<script>
447+
import {ref} from 'vue';
448+
449+
export default {
450+
setup() {
451+
return {foo: ref(undefined)};
452+
}
453+
};
454+
</script>
455+
`,
456+
outdent`
457+
<script setup>
458+
import * as vue from 'vue';
459+
const foo = vue.ref(undefined);
460+
</script>
461+
`,
462+
],
463+
invalid: [
464+
outdent`
465+
<script>
466+
import {nextTick} from 'vue';
467+
const foo = nextTick(undefined);
468+
</script>
469+
`,
470+
],
471+
});

test/snapshots/no-useless-undefined.mjs.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,28 @@ Generated by [AVA](https://avajs.dev).
299299
> 1 | foo.notBind(bar, undefined)␊
300300
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
301301
`
302+
303+
## Invalid #1
304+
1 | <script>
305+
2 | import {nextTick} from 'vue';
306+
3 | const foo = nextTick(undefined);
307+
4 | </script>
308+
309+
> Output
310+
311+
`␊
312+
1 | <script>␊
313+
2 | import {nextTick} from 'vue';␊
314+
3 | const foo = nextTick();␊
315+
4 | </script>␊
316+
`
317+
318+
> Error 1/1
319+
320+
`␊
321+
1 | <script>␊
322+
2 | import {nextTick} from 'vue';␊
323+
> 3 | const foo = nextTick(undefined);␊
324+
| ^^^^^^^^^ Do not use useless \`undefined\`.␊
325+
4 | </script>␊
326+
`
106 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)