|
| 1 | +import outdent from 'outdent'; |
| 2 | +import {getTester, parsers} from './utils/test.mjs'; |
| 3 | + |
| 4 | +const {test} = getTester(import.meta); |
| 5 | + |
| 6 | +test.snapshot({ |
| 7 | + valid: [ |
| 8 | + 'export default function named() {}', |
| 9 | + 'export default class named {}', |
| 10 | + 'export default []', |
| 11 | + 'export default {}', |
| 12 | + 'export default 1', |
| 13 | + 'export default false', |
| 14 | + 'export default 0n', |
| 15 | + 'notExports = class {}', |
| 16 | + 'notModule.exports = class {}', |
| 17 | + 'module.notExports = class {}', |
| 18 | + 'module.exports.foo = class {}', |
| 19 | + 'alert(exports = class {})', |
| 20 | + 'foo = module.exports = class {}', |
| 21 | + ], |
| 22 | + invalid: [ |
| 23 | + 'export default function () {}', |
| 24 | + 'export default class {}', |
| 25 | + 'export default () => {}', |
| 26 | + 'export default function * () {}', |
| 27 | + 'export default async function () {}', |
| 28 | + 'export default async function * () {}', |
| 29 | + 'export default async () => {}', |
| 30 | + |
| 31 | + // `ClassDeclaration` |
| 32 | + { |
| 33 | + code: 'export default class {}', |
| 34 | + filename: '/path/to/foo.js', |
| 35 | + }, |
| 36 | + { |
| 37 | + code: 'export default class extends class {} {}', |
| 38 | + filename: '/path/to/foo.js', |
| 39 | + }, |
| 40 | + { |
| 41 | + code: 'export default class{}', |
| 42 | + filename: '/path/to/foo.js', |
| 43 | + }, |
| 44 | + { |
| 45 | + code: 'export default class {}', |
| 46 | + filename: '/path/to/foo-bar.js', |
| 47 | + }, |
| 48 | + { |
| 49 | + code: 'export default class {}', |
| 50 | + filename: '/path/to/foo_bar.js', |
| 51 | + }, |
| 52 | + { |
| 53 | + code: 'export default class {}', |
| 54 | + filename: '/path/to/foo+bar.js', |
| 55 | + }, |
| 56 | + { |
| 57 | + code: 'export default class {}', |
| 58 | + filename: '/path/to/foo+bar123.js', |
| 59 | + }, |
| 60 | + { |
| 61 | + code: 'export default class {}', |
| 62 | + filename: '/path/to/foo*.js', |
| 63 | + }, |
| 64 | + { |
| 65 | + code: 'export default class {}', |
| 66 | + filename: '/path/to/[foo].js', |
| 67 | + }, |
| 68 | + { |
| 69 | + code: 'export default class {}', |
| 70 | + filename: '/path/to/class.js', |
| 71 | + }, |
| 72 | + { |
| 73 | + code: 'export default class {}', |
| 74 | + filename: '/path/to/foo.helper.js', |
| 75 | + }, |
| 76 | + { |
| 77 | + code: 'export default class {}', |
| 78 | + filename: '/path/to/foo.bar.js', |
| 79 | + }, |
| 80 | + { |
| 81 | + code: 'export default class {}', |
| 82 | + filename: '/path/to/foo.test.js', |
| 83 | + }, |
| 84 | + { |
| 85 | + code: 'export default class {}', |
| 86 | + filename: '/path/to/.foo.js', |
| 87 | + }, |
| 88 | + { |
| 89 | + code: outdent` |
| 90 | + let Foo, Foo_, foo, foo_ |
| 91 | + export default class {} |
| 92 | + `, |
| 93 | + filename: '/path/to/foo.js', |
| 94 | + }, |
| 95 | + |
| 96 | + // `ClassExpression` |
| 97 | + { |
| 98 | + code: outdent` |
| 99 | + let Foo, Foo_, foo, foo_ |
| 100 | + export default (class{}) |
| 101 | + `, |
| 102 | + filename: '/path/to/foo.js', |
| 103 | + }, |
| 104 | + { |
| 105 | + code: 'export default (class extends class {} {})', |
| 106 | + filename: '/path/to/foo.js', |
| 107 | + }, |
| 108 | + { |
| 109 | + code: outdent` |
| 110 | + let Exports, Exports_, exports, exports_ |
| 111 | + exports = class {} |
| 112 | + `, |
| 113 | + filename: '/path/to/exports.js', |
| 114 | + }, |
| 115 | + { |
| 116 | + code: 'module.exports = class {}', |
| 117 | + filename: '/path/to/module.js', |
| 118 | + }, |
| 119 | + |
| 120 | + // `FunctionDeclaration` |
| 121 | + { |
| 122 | + code: 'export default function () {}', |
| 123 | + filename: '/path/to/foo.js', |
| 124 | + }, |
| 125 | + { |
| 126 | + code: 'export default function* () {}', |
| 127 | + filename: '/path/to/foo.js', |
| 128 | + }, |
| 129 | + { |
| 130 | + code: 'export default async function* () {}', |
| 131 | + filename: '/path/to/foo.js', |
| 132 | + }, |
| 133 | + { |
| 134 | + code: 'export default async function*() {}', |
| 135 | + filename: '/path/to/foo.js', |
| 136 | + }, |
| 137 | + { |
| 138 | + code: 'export default async function *() {}', |
| 139 | + filename: '/path/to/foo.js', |
| 140 | + }, |
| 141 | + { |
| 142 | + code: 'export default async function * () {}', |
| 143 | + filename: '/path/to/foo.js', |
| 144 | + }, |
| 145 | + { |
| 146 | + code: 'export default async function * /* comment */ () {}', |
| 147 | + filename: '/path/to/foo.js', |
| 148 | + }, |
| 149 | + { |
| 150 | + code: outdent` |
| 151 | + export default async function * // comment |
| 152 | + () {} |
| 153 | + `, |
| 154 | + filename: '/path/to/foo.js', |
| 155 | + }, |
| 156 | + { |
| 157 | + code: outdent` |
| 158 | + let Foo, Foo_, foo, foo_ |
| 159 | + export default async function * () {} |
| 160 | + `, |
| 161 | + filename: '/path/to/foo.js', |
| 162 | + }, |
| 163 | + |
| 164 | + // `FunctionExpression` |
| 165 | + { |
| 166 | + code: outdent` |
| 167 | + let Foo, Foo_, foo, foo_ |
| 168 | + export default (async function * () {}) |
| 169 | + `, |
| 170 | + filename: '/path/to/foo.js', |
| 171 | + }, |
| 172 | + { |
| 173 | + code: outdent` |
| 174 | + let Exports, Exports_, exports, exports_ |
| 175 | + exports = function() {} |
| 176 | + `, |
| 177 | + filename: '/path/to/exports.js', |
| 178 | + }, |
| 179 | + { |
| 180 | + code: 'module.exports = function() {}', |
| 181 | + filename: '/path/to/module.js', |
| 182 | + }, |
| 183 | + |
| 184 | + // `ArrowFunctionExpression` |
| 185 | + { |
| 186 | + code: 'export default () => {}', |
| 187 | + filename: '/path/to/foo.js', |
| 188 | + }, |
| 189 | + { |
| 190 | + code: 'export default async () => {}', |
| 191 | + filename: '/path/to/foo.js', |
| 192 | + }, |
| 193 | + { |
| 194 | + code: 'export default () => {};', |
| 195 | + filename: '/path/to/foo.js', |
| 196 | + }, |
| 197 | + { |
| 198 | + code: 'export default() => {}', |
| 199 | + filename: '/path/to/foo.js', |
| 200 | + }, |
| 201 | + { |
| 202 | + code: 'export default foo => {}', |
| 203 | + filename: '/path/to/foo.js', |
| 204 | + }, |
| 205 | + { |
| 206 | + code: 'export default (( () => {} ))', |
| 207 | + filename: '/path/to/foo.js', |
| 208 | + }, |
| 209 | + { |
| 210 | + code: '/* comment 1 */ export /* comment 2 */ default /* comment 3 */ () => {}', |
| 211 | + filename: '/path/to/foo.js', |
| 212 | + }, |
| 213 | + { |
| 214 | + code: outdent` |
| 215 | + // comment 1 |
| 216 | + export |
| 217 | + // comment 2 |
| 218 | + default |
| 219 | + // comment 3 |
| 220 | + () => {} |
| 221 | + `, |
| 222 | + filename: '/path/to/foo.js', |
| 223 | + }, |
| 224 | + { |
| 225 | + code: outdent` |
| 226 | + let Foo, Foo_, foo, foo_ |
| 227 | + export default async () => {} |
| 228 | + `, |
| 229 | + filename: '/path/to/foo.js', |
| 230 | + }, |
| 231 | + { |
| 232 | + code: outdent` |
| 233 | + let Exports, Exports_, exports, exports_ |
| 234 | + exports = (( () => {} )) |
| 235 | + `, |
| 236 | + filename: '/path/to/exports.js', |
| 237 | + }, |
| 238 | + { |
| 239 | + code: outdent` |
| 240 | + // comment 1 |
| 241 | + module |
| 242 | + // comment 2 |
| 243 | + .exports |
| 244 | + // comment 3 |
| 245 | + = |
| 246 | + // comment 4 |
| 247 | + () => {}; |
| 248 | + `, |
| 249 | + filename: '/path/to/module.js', |
| 250 | + }, |
| 251 | + { |
| 252 | + code: '(( exports = (( () => {} )) ))', |
| 253 | + filename: '/path/to/foo.js', |
| 254 | + }, |
| 255 | + { |
| 256 | + code: '(( module.exports = (( () => {} )) ))', |
| 257 | + filename: '/path/to/foo.js', |
| 258 | + }, |
| 259 | + { |
| 260 | + code: '(( exports = (( () => {} )) ));', |
| 261 | + filename: '/path/to/foo.js', |
| 262 | + }, |
| 263 | + { |
| 264 | + code: '(( module.exports = (( () => {} )) ));', |
| 265 | + filename: '/path/to/foo.js', |
| 266 | + }, |
| 267 | + ], |
| 268 | +}); |
| 269 | + |
| 270 | +// Decorators |
| 271 | +const decoratorsBeforeExportOptions = { |
| 272 | + parser: parsers.babel, |
| 273 | + parserOptions: { |
| 274 | + babelOptions: { |
| 275 | + parserOpts: { |
| 276 | + plugins: [ |
| 277 | + ['decorators', {decoratorsBeforeExport: true}], |
| 278 | + ], |
| 279 | + }, |
| 280 | + }, |
| 281 | + }, |
| 282 | +}; |
| 283 | +const decoratorsAfterExportOptions = { |
| 284 | + parser: parsers.babel, |
| 285 | + parserOptions: { |
| 286 | + babelOptions: { |
| 287 | + parserOpts: { |
| 288 | + plugins: [ |
| 289 | + ['decorators', {decoratorsBeforeExport: false}], |
| 290 | + ], |
| 291 | + }, |
| 292 | + }, |
| 293 | + }, |
| 294 | +}; |
| 295 | +test.snapshot({ |
| 296 | + valid: [], |
| 297 | + invalid: [ |
| 298 | + { |
| 299 | + code: '@decorator export default class {}', |
| 300 | + filename: '/path/to/foo.js', |
| 301 | + ...decoratorsBeforeExportOptions, |
| 302 | + }, |
| 303 | + { |
| 304 | + code: 'export default @decorator(class {}) class extends class {} {}', |
| 305 | + filename: '/path/to/foo.js', |
| 306 | + ...decoratorsAfterExportOptions, |
| 307 | + }, |
| 308 | + { |
| 309 | + code: 'module.exports = @decorator(class {}) class extends class {} {}', |
| 310 | + filename: '/path/to/foo.js', |
| 311 | + ...decoratorsAfterExportOptions, |
| 312 | + }, |
| 313 | + { |
| 314 | + code: '@decorator @decorator(class {}) export default class {}', |
| 315 | + filename: '/path/to/foo.js', |
| 316 | + ...decoratorsBeforeExportOptions, |
| 317 | + }, |
| 318 | + ], |
| 319 | +}); |
| 320 | + |
0 commit comments