1
1
/**
2
- * @fileoverview Disallow usage of `this` in template.
2
+ * @fileoverview enforce usage of `this` in template.
3
3
* @author Armano
4
4
*/
5
5
'use strict'
@@ -21,59 +21,77 @@ const ruleTester = new RuleTester({
21
21
parserOptions : { ecmaVersion : 2015 }
22
22
} )
23
23
24
- ruleTester . run ( 'no-this-in-template' , rule , {
25
- valid : [
26
- '' ,
27
- '<template></template>' ,
28
- '<template><div></div></template>' ,
29
- '<template><div>{{ foo.bar }}</div></template>' ,
30
- '<template><div v-for="foo in bar">{{ foo }}</div></template>' ,
31
- '<template><div v-if="foo">{{ foo }}</div></template>' ,
32
- '<template><div :class="foo">{{ foo }}</div></template>' ,
33
- '<template><div :class="{this: foo}">{{ foo }}</div></template>'
34
- ] ,
35
- invalid : [
24
+ function createValidTests ( prefix , options ) {
25
+ return [
36
26
{
37
- code : '<template><div>{{ this.foo }}</div></template>' ,
38
- errors : [ {
39
- message : "Unexpected usage of 'this'." ,
40
- type : 'ThisExpression'
41
- } ]
27
+ code : `<template><div>{{ ${ prefix } foo.bar }}</div></template><!-- ${ options . join ( '' ) } -->` ,
28
+ options
42
29
} ,
43
30
{
44
- code : '<template><div :class="this.foo"></div></template>' ,
45
- errors : [ {
46
- message : "Unexpected usage of 'this'." ,
47
- type : 'ThisExpression'
48
- } ]
31
+ code : `<template><div v-for="foo in ${ prefix } bar">{{ foo }}</div></template><!-- ${ options . join ( '' ) } -->` ,
32
+ options
49
33
} ,
50
34
{
51
- code : '<template><div :class="{foo: this.foo}"></div></template>' ,
52
- errors : [ {
53
- message : "Unexpected usage of 'this'." ,
54
- type : 'ThisExpression'
55
- } ]
35
+ code : `<template><div v-if="${ prefix } foo">{{ ${ prefix } foo }}</div></template><!-- ${ options . join ( '' ) } -->` ,
36
+ options
56
37
} ,
57
38
{
58
- code : '<template><div :class="{foo: this.foo()}"></div></template>' ,
59
- errors : [ {
60
- message : "Unexpected usage of 'this'." ,
61
- type : 'ThisExpression'
62
- } ]
39
+ code : `<template><div :class="${ prefix } foo">{{ ${ prefix } foo }}</div></template><!-- ${ options . join ( '' ) } -->` ,
40
+ options
63
41
} ,
64
42
{
65
- code : '<template><div v-if="this.foo"></div></template>' ,
66
- errors : [ {
67
- message : "Unexpected usage of 'this'." ,
68
- type : 'ThisExpression'
69
- } ]
43
+ code : `<template><div :class="{this: ${ prefix } foo}">{{ ${ prefix } foo }}</div></template><!-- ${ options . join ( '' ) } -->` ,
44
+ options
70
45
} ,
71
46
{
72
- code : '<template><div v-for="foo in this.bar"></div></template>' ,
73
- errors : [ {
74
- message : "Unexpected usage of 'this'." ,
75
- type : 'ThisExpression'
76
- } ]
47
+ code : `<template><div v-for="bar in ${ prefix } foo" v-if="bar">{{ bar }}</div></template><!-- ${ options . join ( '' ) } -->` ,
48
+ options
77
49
}
78
50
]
51
+ }
52
+
53
+ function createInvalidTests ( prefix , options , message , type ) {
54
+ return [
55
+ {
56
+ code : `<template><div>{{ ${ prefix } foo }}</div></template><!-- ${ options . join ( '' ) } -->` ,
57
+ errors : [ { message, type } ] ,
58
+ options
59
+ } ,
60
+ {
61
+ code : `<template><div :class="${ prefix } foo"></div></template><!-- ${ options . join ( '' ) } -->` ,
62
+ errors : [ { message, type } ] ,
63
+ options
64
+ } ,
65
+ {
66
+ code : `<template><div :class="{foo: ${ prefix } foo}"></div></template><!-- ${ options . join ( '' ) } -->` ,
67
+ errors : [ { message, type } ] ,
68
+ options
69
+ } ,
70
+ {
71
+ code : `<template><div :class="{foo: ${ prefix } foo()}"></div></template><!-- ${ options . join ( '' ) } -->` ,
72
+ errors : [ { message, type } ] ,
73
+ options
74
+ } ,
75
+ {
76
+ code : `<template><div v-if="${ prefix } foo"></div></template><!-- ${ options . join ( '' ) } -->` ,
77
+ errors : [ { message, type } ] ,
78
+ options
79
+ } ,
80
+ {
81
+ code : `<template><div v-for="foo in ${ prefix } bar"></div></template><!-- ${ options . join ( '' ) } -->` ,
82
+ errors : [ { message, type } ] ,
83
+ options
84
+ }
85
+ ]
86
+ }
87
+
88
+ ruleTester . run ( 'no-this-in-template' , rule , {
89
+ valid : [ '' , '<template></template>' , '<template><div></div></template>' ]
90
+ . concat ( createValidTests ( '' , [ ] ) )
91
+ . concat ( createValidTests ( '' , [ 'never' ] ) )
92
+ . concat ( createValidTests ( 'this.' , [ 'always' ] ) ) ,
93
+ invalid : [ ]
94
+ . concat ( createInvalidTests ( 'this.' , [ ] , "Unexpected usage of 'this'." , 'ThisExpression' ) )
95
+ . concat ( createInvalidTests ( 'this.' , [ 'never' ] , "Unexpected usage of 'this'." , 'ThisExpression' ) )
96
+ . concat ( createInvalidTests ( '' , [ 'always' ] , "Expected 'this'." , 'Identifier' ) )
79
97
} )
0 commit comments