|
| 1 | +/** |
| 2 | + * @fileoverview Disallows the usage of `meta.replacedBy` property |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../../../lib/rules/no-meta-replaced-by'); |
| 12 | +const RuleTester = require('../eslint-rule-tester').RuleTester; |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Tests |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +const valid = [ |
| 19 | + 'module.exports = {};', |
| 20 | + ` |
| 21 | + module.exports = { |
| 22 | + create(context) {}, |
| 23 | + }; |
| 24 | + `, |
| 25 | + ` |
| 26 | + module.exports = { |
| 27 | + meta: {}, |
| 28 | + create(context) {}, |
| 29 | + }; |
| 30 | + `, |
| 31 | + ` |
| 32 | + module.exports = { |
| 33 | + meta: { |
| 34 | + deprecated: true, |
| 35 | + }, |
| 36 | + create(context) {}, |
| 37 | + }; |
| 38 | + `, |
| 39 | + { |
| 40 | + code: ` |
| 41 | + module.exports = { |
| 42 | + meta: { |
| 43 | + deprecated: { |
| 44 | + replacedBy: [ |
| 45 | + { |
| 46 | + rule: { |
| 47 | + name: 'foo', |
| 48 | + }, |
| 49 | + }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + }, |
| 53 | + create(context) {}, |
| 54 | + }; |
| 55 | + `, |
| 56 | + errors: 0, |
| 57 | + }, |
| 58 | +]; |
| 59 | + |
| 60 | +const invalid = [ |
| 61 | + { |
| 62 | + code: ` |
| 63 | + module.exports = { |
| 64 | + meta: { |
| 65 | + replacedBy: ['the-new-rule'], |
| 66 | + }, |
| 67 | + create(context) {}, |
| 68 | + }; |
| 69 | + `, |
| 70 | + errors: [ |
| 71 | + { |
| 72 | + messageId: 'useNewFormat', |
| 73 | + line: 4, |
| 74 | + endLine: 4, |
| 75 | + }, |
| 76 | + ], |
| 77 | + }, |
| 78 | + { |
| 79 | + code: ` |
| 80 | + const meta = { |
| 81 | + replacedBy: null, |
| 82 | + }; |
| 83 | +
|
| 84 | + module.exports = { |
| 85 | + meta, |
| 86 | + create(context) {}, |
| 87 | + }; |
| 88 | + `, |
| 89 | + errors: [ |
| 90 | + { |
| 91 | + messageId: 'useNewFormat', |
| 92 | + line: 3, |
| 93 | + endLine: 3, |
| 94 | + }, |
| 95 | + ], |
| 96 | + }, |
| 97 | + { |
| 98 | + code: ` |
| 99 | + const spread = { |
| 100 | + replacedBy: null, |
| 101 | + }; |
| 102 | +
|
| 103 | + module.exports = { |
| 104 | + meta: { |
| 105 | + ...spread, |
| 106 | + }, |
| 107 | + create(context) {}, |
| 108 | + }; |
| 109 | + `, |
| 110 | + errors: [{ messageId: 'useNewFormat' }], |
| 111 | + }, |
| 112 | +]; |
| 113 | + |
| 114 | +const testToESM = (test) => { |
| 115 | + if (typeof test === 'string') { |
| 116 | + return test.replace('module.exports =', 'export default'); |
| 117 | + } |
| 118 | + |
| 119 | + const code = test.code.replace('module.exports =', 'export default'); |
| 120 | + |
| 121 | + return { |
| 122 | + ...test, |
| 123 | + code, |
| 124 | + }; |
| 125 | +}; |
| 126 | + |
| 127 | +new RuleTester({ |
| 128 | + languageOptions: { sourceType: 'commonjs' }, |
| 129 | +}).run('no-meta-replaced-by', rule, { |
| 130 | + valid, |
| 131 | + invalid, |
| 132 | +}); |
| 133 | + |
| 134 | +new RuleTester({ |
| 135 | + languageOptions: { sourceType: 'module' }, |
| 136 | +}).run('no-meta-replaced-by', rule, { |
| 137 | + valid: valid.map(testToESM), |
| 138 | + invalid: invalid.map(testToESM), |
| 139 | +}); |
0 commit comments