Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️New: Add rule no-template-shadow. #158

Merged
merged 6 commits into from
Jul 30, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add rule no-template-shadow.
fixes #101
armano2 committed Sep 17, 2017

Unverified

This user has not yet uploaded their public signing key.
commit 2d15606c761d8cac7192a6aaa37d6ddf0b2dcfac
46 changes: 46 additions & 0 deletions docs/rules/no-template-shadow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Disallow variable declarations from shadowing variables declared in the outer scope. (no-template-shadow)

`no-shadow` should report variable definitions of v-for directives or scope attributes if those shadows the variables in parent scopes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no-template-shadow


## :book: Rule Details

This rule aims to eliminate shadowed variable declarations of v-for directives or scope attributes.

:-1: Examples of **incorrect** code for this rule:

```html
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should present two or three cases here. First - a simple template example, and second with shadowed data or method property declarations. e.g.:
1.

<template>
   <div>
     <div v-for="i in 5">
       <div v-for="i in 10"></div>
     </div>
   </div>
 </template>
<template>
   <div>
     <div v-for="i in 5"></div>
   </div>
 </template>
<script>
  export default {
    data () {
      return {
        i: 10
      }
    }
  }
</script>

<template>
<div>
<div v-for="i in 5">
<div v-for="i in 5"></div>
</div>
</div>
</template>
<script>
export default {
data: {
i: 7
}
}
</script>
```

:+1: Examples of **correct** code for this rule:

```html
<template>
<div v-for="i in 5"></div>
<div v-for="i in 5"></div>
</template>
<script>
export default {
computed: {
f () { }
}
}
</script>
```

## :wrench: Options

Nothing.
78 changes: 78 additions & 0 deletions lib/rules/no-template-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* @fileoverview Disallow variable declarations from shadowing variables declared in the outer scope.
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

const GROUP_NAMES = ['props', 'computed', 'data', 'methods']

module.exports = {
meta: {
docs: {
description: 'Disallow variable declarations from shadowing variables declared in the outer scope.',
category: 'Possible Errors',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should belong to essential group

recommended: false
},
fixable: null,
schema: []
},

create (context) {
const jsVars = new Set()
let scope = {
parent: null,
nodes: []
}

// ----------------------------------------------------------------------
// Public
// ----------------------------------------------------------------------

utils.registerTemplateBodyVisitor(context, {
VElement (node) {
scope = {
parent: scope,
nodes: scope.nodes.slice() // make copy
}
if (node.variables) {
for (const variable of node.variables) {
const varNode = variable.id
const name = varNode.name
if (scope.nodes.some(node => node.name === name) || jsVars.has(name)) {
context.report({
node: varNode,
loc: varNode.loc,
message: "Variable '{{name}}' is already declared in the upper scope.",
data: {
name
}
})
} else {
scope.nodes.push(varNode)
}
}
}
},
'VElement:exit' (node) {
scope = scope.parent
}
})

return utils.executeOnVue(context, (obj) => {
const properties = Array.from(utils.iterateProperties(obj, new Set(GROUP_NAMES)))
for (const node of properties) {
jsVars.add(node.name)
}
})
}
}
127 changes: 127 additions & 0 deletions tests/lib/rules/no-template-shadow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/**
* @fileoverview Disallow variable declarations from shadowing variables declared in the outer scope.
* @author Armano
*/
'use strict'

// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/no-template-shadow')
const RuleTester = require('eslint').RuleTester

// ------------------------------------------------------------------------------
// Tests
// ------------------------------------------------------------------------------

const ruleTester = new RuleTester({
parser: 'vue-eslint-parser',
parserOptions: {
ecmaVersion: 2015,
sourceType: 'module'
}
})

ruleTester.run('no-template-shadow', rule, {

valid: [
'',
'<template><div></div></template>',
'<template><div v-for="i in 5"></div></template>',
'<template><div v-for="i in 5"><div v-for="b in 5"></div></div></template>',
'<template><div v-for="i in 5"></div><div v-for="i in 5"></div></template>',
{
filename: 'test.vue',
code: `<template>
<div v-for="i in 5"></div>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add case with i in f, wdyt?

<div v-for="i in 5"></div>
</template>
<script>
export default {
computed: {
f () { }
}
}
</script>`
}
],

invalid: [
{
filename: 'test.vue',
code: '<template><div v-for="i in 5"><div v-for="i in 5"></div></div></template>',
errors: [{
message: "Variable 'i' is already declared in the upper scope.",
type: 'Identifier'
}]
},
{
filename: 'test.vue',
code: `<template>
<div v-for="i in 5">
<div v-for="i in 5"></div>
</div>
</template>
<script>
export default {
data: {
i: 7
}
}
</script>`,
errors: [{
message: "Variable 'i' is already declared in the upper scope.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add line on which the following error is expected

type: 'Identifier'
}, {
message: "Variable 'i' is already declared in the upper scope.",
type: 'Identifier'
}]
},
{
filename: 'test.vue',
code: `<template>
<div v-for="i in 5"></div>
<div v-for="i in 5"></div>
</template>
<script>
export default {
data: {
i: 7
}
}
</script>`,
errors: [{
message: "Variable 'i' is already declared in the upper scope.",
type: 'Identifier'
}, {
message: "Variable 'i' is already declared in the upper scope.",
type: 'Identifier'
}]
},
{
filename: 'test.vue',
code: `<template>
<div v-for="i in 5"></div>
<div v-for="f in 5"></div>
</template>
<script>
export default {
computed: {
i () { }
},
methods: {
f () { }
}
}
</script>`,
errors: [{
message: "Variable 'i' is already declared in the upper scope.",
type: 'Identifier'
}, {
message: "Variable 'f' is already declared in the upper scope.",
type: 'Identifier'
}]
}
]
})