|
| 1 | +/** |
| 2 | + * @fileoverview Disallow usage of `this` in template. |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../../../lib/rules/no-this-in-template') |
| 12 | + |
| 13 | +const RuleTester = require('eslint').RuleTester |
| 14 | + |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | +// Tests |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +const ruleTester = new RuleTester({ |
| 20 | + parser: 'vue-eslint-parser', |
| 21 | + parserOptions: { ecmaVersion: 2015 } |
| 22 | +}) |
| 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: [ |
| 36 | + { |
| 37 | + code: '<template><div>{{ this.foo }}</div></template>', |
| 38 | + errors: [{ |
| 39 | + message: "Unexpected usage of 'this'.", |
| 40 | + type: 'ThisExpression' |
| 41 | + }] |
| 42 | + }, |
| 43 | + { |
| 44 | + code: '<template><div :class="this.foo"></div></template>', |
| 45 | + errors: [{ |
| 46 | + message: "Unexpected usage of 'this'.", |
| 47 | + type: 'ThisExpression' |
| 48 | + }] |
| 49 | + }, |
| 50 | + { |
| 51 | + code: '<template><div :class="{foo: this.foo}"></div></template>', |
| 52 | + errors: [{ |
| 53 | + message: "Unexpected usage of 'this'.", |
| 54 | + type: 'ThisExpression' |
| 55 | + }] |
| 56 | + }, |
| 57 | + { |
| 58 | + code: '<template><div :class="{foo: this.foo()}"></div></template>', |
| 59 | + errors: [{ |
| 60 | + message: "Unexpected usage of 'this'.", |
| 61 | + type: 'ThisExpression' |
| 62 | + }] |
| 63 | + }, |
| 64 | + { |
| 65 | + code: '<template><div v-if="this.foo"></div></template>', |
| 66 | + errors: [{ |
| 67 | + message: "Unexpected usage of 'this'.", |
| 68 | + type: 'ThisExpression' |
| 69 | + }] |
| 70 | + }, |
| 71 | + { |
| 72 | + code: '<template><div v-for="foo in this.bar"></div></template>', |
| 73 | + errors: [{ |
| 74 | + message: "Unexpected usage of 'this'.", |
| 75 | + type: 'ThisExpression' |
| 76 | + }] |
| 77 | + } |
| 78 | + ] |
| 79 | +}) |
0 commit comments