|
11 | 11 | const rule = require('../../../lib/rules/require-direct-export')
|
12 | 12 | const RuleTester = require('eslint').RuleTester
|
13 | 13 |
|
14 |
| -const parserOptions = { |
15 |
| - ecmaVersion: 2018, |
16 |
| - sourceType: 'module', |
17 |
| - ecmaFeatures: { jsx: true } |
18 |
| -} |
19 |
| - |
20 | 14 | // ------------------------------------------------------------------------------
|
21 | 15 | // Tests
|
22 | 16 | // ------------------------------------------------------------------------------
|
23 | 17 |
|
24 |
| -const ruleTester = new RuleTester() |
| 18 | +const ruleTester = new RuleTester({ |
| 19 | + parserOptions: { |
| 20 | + ecmaVersion: 2018, |
| 21 | + sourceType: 'module', |
| 22 | + ecmaFeatures: { jsx: true } |
| 23 | + } |
| 24 | +}) |
25 | 25 | ruleTester.run('require-direct-export', rule, {
|
26 | 26 |
|
27 | 27 | valid: [
|
28 | 28 | {
|
29 | 29 | filename: 'test.vue',
|
30 | 30 | code: ''
|
31 | 31 | },
|
| 32 | + { |
| 33 | + filename: 'test.vue', |
| 34 | + code: `export default {}` |
| 35 | + }, |
| 36 | + { |
| 37 | + filename: 'test.vue', |
| 38 | + code: `export default {}`, |
| 39 | + options: [{ disallowFunctionalComponentFunction: true }] |
| 40 | + }, |
| 41 | + { |
| 42 | + filename: 'test.js', |
| 43 | + code: `export default Foo` |
| 44 | + }, |
32 | 45 | {
|
33 | 46 | filename: 'test.vue',
|
34 | 47 | code: `
|
35 |
| - export default {} |
36 |
| - `, |
37 |
| - parserOptions |
| 48 | + import { h } from 'vue' |
| 49 | + export default function (props) { |
| 50 | + return h('div', \`Hello! \${props.name}\`) |
| 51 | + } |
| 52 | + ` |
| 53 | + }, |
| 54 | + { |
| 55 | + filename: 'test.vue', |
| 56 | + code: ` |
| 57 | + import { h } from 'vue' |
| 58 | + export default function Component () { |
| 59 | + return h('div') |
| 60 | + } |
| 61 | + ` |
| 62 | + }, |
| 63 | + { |
| 64 | + filename: 'test.vue', |
| 65 | + code: ` |
| 66 | + import { h } from 'vue' |
| 67 | + export default (props) => { |
| 68 | + return h('div', \`Hello! \${props.name}\`) |
| 69 | + } |
| 70 | + ` |
| 71 | + }, |
| 72 | + { |
| 73 | + filename: 'test.vue', |
| 74 | + code: ` |
| 75 | + import { h } from 'vue' |
| 76 | + export default props => h('div', props.msg) |
| 77 | + ` |
38 | 78 | }
|
39 | 79 | ],
|
40 | 80 |
|
41 | 81 | invalid: [
|
42 |
| - |
43 | 82 | {
|
44 | 83 | filename: 'test.vue',
|
45 | 84 | code: `
|
46 |
| - const A = {}; |
47 |
| - export default A`, |
48 |
| - parserOptions, |
| 85 | + const A = {}; |
| 86 | + export default A`, |
49 | 87 | errors: [{
|
50 | 88 | message: 'Expected the component literal to be directly exported.',
|
51 | 89 | type: 'ExportDefaultDeclaration',
|
52 | 90 | line: 3
|
53 | 91 | }]
|
| 92 | + }, |
| 93 | + { |
| 94 | + filename: 'test.vue', |
| 95 | + code: ` |
| 96 | + function A(props) { |
| 97 | + return h('div', props.msg) |
| 98 | + }; |
| 99 | + export default A`, |
| 100 | + errors: [{ |
| 101 | + message: 'Expected the component literal to be directly exported.', |
| 102 | + type: 'ExportDefaultDeclaration', |
| 103 | + line: 5 |
| 104 | + }] |
| 105 | + }, |
| 106 | + { |
| 107 | + filename: 'test.vue', |
| 108 | + code: `export default function NoReturn() {}`, |
| 109 | + errors: [{ |
| 110 | + message: 'Expected the component literal to be directly exported.', |
| 111 | + type: 'ExportDefaultDeclaration', |
| 112 | + line: 1 |
| 113 | + }] |
| 114 | + }, |
| 115 | + { |
| 116 | + filename: 'test.vue', |
| 117 | + code: `export default function () {}`, |
| 118 | + errors: [{ |
| 119 | + message: 'Expected the component literal to be directly exported.', |
| 120 | + type: 'ExportDefaultDeclaration', |
| 121 | + line: 1 |
| 122 | + }] |
| 123 | + }, |
| 124 | + { |
| 125 | + filename: 'test.vue', |
| 126 | + code: `export default () => {}`, |
| 127 | + errors: [{ |
| 128 | + message: 'Expected the component literal to be directly exported.', |
| 129 | + type: 'ExportDefaultDeclaration', |
| 130 | + line: 1 |
| 131 | + }] |
| 132 | + }, |
| 133 | + { |
| 134 | + filename: 'test.vue', |
| 135 | + code: `export default () => { |
| 136 | + const foo = () => { |
| 137 | + return b |
| 138 | + } |
| 139 | + }`, |
| 140 | + errors: [{ |
| 141 | + message: 'Expected the component literal to be directly exported.', |
| 142 | + type: 'ExportDefaultDeclaration', |
| 143 | + line: 1 |
| 144 | + }] |
| 145 | + }, |
| 146 | + { |
| 147 | + filename: 'test.vue', |
| 148 | + code: `export default () => { |
| 149 | + return |
| 150 | + }`, |
| 151 | + errors: [{ |
| 152 | + message: 'Expected the component literal to be directly exported.', |
| 153 | + type: 'ExportDefaultDeclaration', |
| 154 | + line: 1 |
| 155 | + }] |
| 156 | + }, |
| 157 | + { |
| 158 | + filename: 'test.vue', |
| 159 | + code: ` |
| 160 | + function A(props) { |
| 161 | + return h('div', props.msg) |
| 162 | + }; |
| 163 | + export default A`, |
| 164 | + options: [{ disallowFunctionalComponentFunction: true }], |
| 165 | + errors: [{ |
| 166 | + message: 'Expected the component literal to be directly exported.', |
| 167 | + type: 'ExportDefaultDeclaration', |
| 168 | + line: 5 |
| 169 | + }] |
| 170 | + }, |
| 171 | + { |
| 172 | + filename: 'test.vue', |
| 173 | + code: ` |
| 174 | + import { h } from 'vue' |
| 175 | + export default function (props) { |
| 176 | + return h('div', \`Hello! \${props.name}\`) |
| 177 | + } |
| 178 | + `, |
| 179 | + options: [{ disallowFunctionalComponentFunction: true }], |
| 180 | + errors: ['Expected the component literal to be directly exported.'] |
| 181 | + }, |
| 182 | + { |
| 183 | + filename: 'test.vue', |
| 184 | + code: ` |
| 185 | + import { h } from 'vue' |
| 186 | + export default function Component () { |
| 187 | + return h('div') |
| 188 | + } |
| 189 | + `, |
| 190 | + options: [{ disallowFunctionalComponentFunction: true }], |
| 191 | + errors: ['Expected the component literal to be directly exported.'] |
| 192 | + }, |
| 193 | + { |
| 194 | + filename: 'test.vue', |
| 195 | + code: ` |
| 196 | + import { h } from 'vue' |
| 197 | + export default (props) => { |
| 198 | + return h('div', \`Hello! \${props.name}\`) |
| 199 | + } |
| 200 | + `, |
| 201 | + options: [{ disallowFunctionalComponentFunction: true }], |
| 202 | + errors: ['Expected the component literal to be directly exported.'] |
| 203 | + }, |
| 204 | + { |
| 205 | + filename: 'test.vue', |
| 206 | + code: ` |
| 207 | + import { h } from 'vue' |
| 208 | + export default props => h('div', props.msg) |
| 209 | + `, |
| 210 | + options: [{ disallowFunctionalComponentFunction: true }], |
| 211 | + errors: ['Expected the component literal to be directly exported.'] |
54 | 212 | }
|
55 | 213 | ]
|
56 | 214 | })
|
0 commit comments