Skip to content

Commit 7a790bc

Browse files
authored
Fix iterateProperties to support arrow functions (#1064)
* Fix iterateProperties to support arrow functions * indent mistake * add no-dupe-keys test cases using arrow function * add no-reserved-keys no-template-shadow test cases using arrow function Co-authored-by: maddocnc <[email protected]>
1 parent 3cc5ac0 commit 7a790bc

File tree

4 files changed

+398
-0
lines changed

4 files changed

+398
-0
lines changed

Diff for: lib/utils/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,8 @@ module.exports = {
682682
yield * this.iterateObjectExpression(item.value, name)
683683
} else if (item.value.type === 'FunctionExpression') {
684684
yield * this.iterateFunctionExpression(item.value, name)
685+
} else if (item.value.type === 'ArrowFunctionExpression') {
686+
yield * this.iterateArrowFunctionExpression(item.value, name)
685687
}
686688
}
687689
},
@@ -734,6 +736,24 @@ module.exports = {
734736
}
735737
},
736738

739+
/**
740+
* Return generator with all elements inside ArrowFunctionExpression
741+
* @param {ASTNode} node Node to check
742+
* @param {string} groupName Name of parent group
743+
*/
744+
* iterateArrowFunctionExpression (node, groupName) {
745+
assert(node.type === 'ArrowFunctionExpression')
746+
if (node.body.type === 'BlockStatement') {
747+
for (const item of node.body.body) {
748+
if (item.type === 'ReturnStatement' && item.argument && item.argument.type === 'ObjectExpression') {
749+
yield * this.iterateObjectExpression(item.argument, groupName)
750+
}
751+
}
752+
} else if (node.body.type === 'ObjectExpression') {
753+
yield * this.iterateObjectExpression(node.body, groupName)
754+
}
755+
},
756+
737757
/**
738758
* Find all functions which do not always return values
739759
* @param {boolean} treatUndefinedAsUnspecified

Diff for: tests/lib/rules/no-dupe-keys.js

+186
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,58 @@ ruleTester.run('no-dupe-keys', rule, {
4545
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
4646
},
4747

48+
{
49+
filename: 'test.vue',
50+
code: `
51+
export default {
52+
props: ['foo'],
53+
computed: {
54+
bar () {
55+
}
56+
},
57+
data: () => {
58+
return {
59+
dat: null
60+
}
61+
},
62+
data: () => {
63+
return
64+
},
65+
methods: {
66+
_foo () {},
67+
test () {
68+
}
69+
}
70+
}
71+
`,
72+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
73+
},
74+
75+
{
76+
filename: 'test.vue',
77+
code: `
78+
export default {
79+
props: ['foo'],
80+
computed: {
81+
bar () {
82+
}
83+
},
84+
data: () => ({
85+
dat: null
86+
}),
87+
data: () => {
88+
return
89+
},
90+
methods: {
91+
_foo () {},
92+
test () {
93+
}
94+
}
95+
}
96+
`,
97+
parserOptions: { ecmaVersion: 6, sourceType: 'module' }
98+
},
99+
48100
{
49101
filename: 'test.vue',
50102
code: `
@@ -82,6 +134,78 @@ ruleTester.run('no-dupe-keys', rule, {
82134
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
83135
},
84136

137+
{
138+
filename: 'test.vue',
139+
code: `
140+
export default {
141+
...foo(),
142+
props: {
143+
...foo(),
144+
foo: String
145+
},
146+
computed: {
147+
...mapGetters({
148+
test: 'getTest'
149+
}),
150+
bar: {
151+
get () {
152+
}
153+
}
154+
},
155+
data: {
156+
...foo(),
157+
dat: null
158+
},
159+
methods: {
160+
...foo(),
161+
test () {
162+
}
163+
},
164+
data: () => {
165+
return {
166+
...dat
167+
}
168+
},
169+
}
170+
`,
171+
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
172+
},
173+
174+
{
175+
filename: 'test.vue',
176+
code: `
177+
export default {
178+
...foo(),
179+
props: {
180+
...foo(),
181+
foo: String
182+
},
183+
computed: {
184+
...mapGetters({
185+
test: 'getTest'
186+
}),
187+
bar: {
188+
get () {
189+
}
190+
}
191+
},
192+
data: {
193+
...foo(),
194+
dat: null
195+
},
196+
methods: {
197+
...foo(),
198+
test () {
199+
}
200+
},
201+
data: () => ({
202+
...dat
203+
}),
204+
}
205+
`,
206+
parserOptions: { ecmaVersion: 2018, sourceType: 'module' }
207+
},
208+
85209
{
86210
filename: 'test.js',
87211
code: `
@@ -136,6 +260,68 @@ ruleTester.run('no-dupe-keys', rule, {
136260
line: 14
137261
}]
138262
},
263+
{
264+
filename: 'test.vue',
265+
code: `
266+
export default {
267+
props: ['foo'],
268+
computed: {
269+
foo () {
270+
}
271+
},
272+
data: () => {
273+
return {
274+
foo: null
275+
}
276+
},
277+
methods: {
278+
foo () {
279+
}
280+
}
281+
}
282+
`,
283+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
284+
errors: [{
285+
message: 'Duplicated key \'foo\'.',
286+
line: 5
287+
}, {
288+
message: 'Duplicated key \'foo\'.',
289+
line: 10
290+
}, {
291+
message: 'Duplicated key \'foo\'.',
292+
line: 14
293+
}]
294+
},
295+
{
296+
filename: 'test.vue',
297+
code: `
298+
export default {
299+
props: ['foo'],
300+
computed: {
301+
foo () {
302+
}
303+
},
304+
data: () => ({
305+
foo: null
306+
}),
307+
methods: {
308+
foo () {
309+
}
310+
}
311+
}
312+
`,
313+
parserOptions: { ecmaVersion: 6, sourceType: 'module' },
314+
errors: [{
315+
message: 'Duplicated key \'foo\'.',
316+
line: 5
317+
}, {
318+
message: 'Duplicated key \'foo\'.',
319+
line: 9
320+
}, {
321+
message: 'Duplicated key \'foo\'.',
322+
line: 12
323+
}]
324+
},
139325
{
140326
filename: 'test.vue',
141327
code: `

Diff for: tests/lib/rules/no-reserved-keys.js

+76
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,50 @@ ruleTester.run('no-reserved-keys', rule, {
4545
}
4646
`,
4747
parserOptions
48+
},
49+
{
50+
filename: 'test.vue',
51+
code: `
52+
export default {
53+
props: ['foo'],
54+
computed: {
55+
bar () {
56+
}
57+
},
58+
data: () => {
59+
return {
60+
dat: null
61+
}
62+
},
63+
methods: {
64+
_foo () {},
65+
test () {
66+
}
67+
}
68+
}
69+
`,
70+
parserOptions
71+
},
72+
{
73+
filename: 'test.vue',
74+
code: `
75+
export default {
76+
props: ['foo'],
77+
computed: {
78+
bar () {
79+
}
80+
},
81+
data: () => ({
82+
dat: null
83+
}),
84+
methods: {
85+
_foo () {},
86+
test () {
87+
}
88+
}
89+
}
90+
`,
91+
parserOptions
4892
}
4993
],
5094

@@ -79,6 +123,38 @@ ruleTester.run('no-reserved-keys', rule, {
79123
line: 4
80124
}]
81125
},
126+
{
127+
filename: 'test.js',
128+
code: `
129+
new Vue({
130+
data: () => {
131+
return {
132+
_foo: String
133+
}
134+
}
135+
})
136+
`,
137+
parserOptions: { ecmaVersion: 6 },
138+
errors: [{
139+
message: "Keys starting with with '_' are reserved in '_foo' group.",
140+
line: 5
141+
}]
142+
},
143+
{
144+
filename: 'test.js',
145+
code: `
146+
new Vue({
147+
data: () => ({
148+
_foo: String
149+
})
150+
})
151+
`,
152+
parserOptions: { ecmaVersion: 6 },
153+
errors: [{
154+
message: "Keys starting with with '_' are reserved in '_foo' group.",
155+
line: 4
156+
}]
157+
},
82158
{
83159
filename: 'test.js',
84160
code: `

0 commit comments

Comments
 (0)