|
| 1 | +/** |
| 2 | + * @fileoverview Enforces component's data property to be a function. |
| 3 | + * @author Armano |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../../../lib/rules/no-shared-component-data') |
| 12 | + |
| 13 | +const RuleTester = require('eslint').RuleTester |
| 14 | + |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | +// Tests |
| 17 | +// ------------------------------------------------------------------------------ |
| 18 | + |
| 19 | +const ruleTester = new RuleTester() |
| 20 | +ruleTester.run('no-shared-component-data', rule, { |
| 21 | + |
| 22 | + valid: [ |
| 23 | + { |
| 24 | + filename: 'test.js', |
| 25 | + code: ` |
| 26 | + new Vue({ |
| 27 | + data: function () { |
| 28 | + return { |
| 29 | + foo: 'bar' |
| 30 | + } |
| 31 | + } |
| 32 | + }) |
| 33 | + ` |
| 34 | + }, |
| 35 | + { |
| 36 | + filename: 'test.js', |
| 37 | + code: ` |
| 38 | + new Vue({ |
| 39 | + data: { |
| 40 | + foo: 'bar' |
| 41 | + } |
| 42 | + }) |
| 43 | + ` |
| 44 | + }, |
| 45 | + { |
| 46 | + filename: 'test.js', |
| 47 | + code: ` |
| 48 | + Vue.component('some-comp', { |
| 49 | + data: function () { |
| 50 | + return { |
| 51 | + foo: 'bar' |
| 52 | + } |
| 53 | + } |
| 54 | + }) |
| 55 | + `, |
| 56 | + parserOptions: { ecmaVersion: 6 } |
| 57 | + }, |
| 58 | + { |
| 59 | + filename: 'test.vue', |
| 60 | + code: ` |
| 61 | + export default { |
| 62 | + data: function () { |
| 63 | + return { |
| 64 | + foo: 'bar' |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + `, |
| 69 | + parserOptions: { ecmaVersion: 6, sourceType: 'module' } |
| 70 | + }, |
| 71 | + { |
| 72 | + filename: 'test.vue', |
| 73 | + code: ` |
| 74 | + export default { |
| 75 | + ...foo |
| 76 | + } |
| 77 | + `, |
| 78 | + parserOptions: { ecmaVersion: 6, sourceType: 'module', ecmaFeatures: { experimentalObjectRestSpread: true }} |
| 79 | + }, |
| 80 | + { |
| 81 | + filename: 'test.vue', |
| 82 | + code: ` |
| 83 | + export default { |
| 84 | + data |
| 85 | + } |
| 86 | + `, |
| 87 | + parserOptions: { ecmaVersion: 6, sourceType: 'module' } |
| 88 | + } |
| 89 | + ], |
| 90 | + |
| 91 | + invalid: [ |
| 92 | + { |
| 93 | + filename: 'test.js', |
| 94 | + code: ` |
| 95 | + Vue.component('some-comp', { |
| 96 | + data: { |
| 97 | + foo: 'bar' |
| 98 | + } |
| 99 | + }) |
| 100 | + `, |
| 101 | + parserOptions: { ecmaVersion: 6 }, |
| 102 | + errors: [{ |
| 103 | + message: '`data` property in component must be a function', |
| 104 | + line: 3 |
| 105 | + }] |
| 106 | + }, |
| 107 | + { |
| 108 | + filename: 'test.vue', |
| 109 | + code: ` |
| 110 | + export default { |
| 111 | + data: { |
| 112 | + foo: 'bar' |
| 113 | + } |
| 114 | + } |
| 115 | + `, |
| 116 | + parserOptions: { ecmaVersion: 6, sourceType: 'module' }, |
| 117 | + errors: [{ |
| 118 | + message: '`data` property in component must be a function', |
| 119 | + line: 3 |
| 120 | + }] |
| 121 | + } |
| 122 | + ] |
| 123 | +}) |
0 commit comments