|
| 1 | +/** |
| 2 | + * @fileoverview Enforces the order of meta properties |
| 3 | + */ |
| 4 | + |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +const rule = require('../../../lib/rules/meta-property-ordering'); |
| 12 | +const RuleTester = require('eslint').RuleTester; |
| 13 | + |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +// Tests |
| 16 | +// ------------------------------------------------------------------------------ |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {string[]} order |
| 20 | + * @returns {string} |
| 21 | + */ |
| 22 | +function getMessage (order) { |
| 23 | + return `The meta properties should be placed in a consistent order: [${order.join(', ')}].`; |
| 24 | +} |
| 25 | + |
| 26 | +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); |
| 27 | +ruleTester.run('test-case-property-ordering', rule, { |
| 28 | + valid: [ |
| 29 | + ` |
| 30 | + module.exports = { |
| 31 | + meta: {type, docs, fixable, schema, messages}, |
| 32 | + create() {}, |
| 33 | + };`, |
| 34 | + |
| 35 | + ` |
| 36 | + module.exports = { |
| 37 | + meta: {docs, schema, messages}, |
| 38 | + create() {}, |
| 39 | + };`, |
| 40 | + |
| 41 | + ` |
| 42 | + module.exports = { |
| 43 | + meta: {docs, messages, foo, bar}, |
| 44 | + create() {}, |
| 45 | + };`, |
| 46 | + |
| 47 | + ` |
| 48 | + module.exports = { |
| 49 | + meta: { |
| 50 | + type: 'problem', |
| 51 | + docs: {}, |
| 52 | + fixable: 'code', |
| 53 | + schema: [], |
| 54 | + messages: {} |
| 55 | + }, |
| 56 | + create() {}, |
| 57 | + };`, |
| 58 | + { |
| 59 | + code: ` |
| 60 | + module.exports = { |
| 61 | + meta: {schema, docs, fixable}, |
| 62 | + create() {}, |
| 63 | + };`, |
| 64 | + options: [['schema', 'docs']], |
| 65 | + }, |
| 66 | + ` |
| 67 | + module.exports = { |
| 68 | + meta: {}, |
| 69 | + create() {}, |
| 70 | + };`, |
| 71 | + ], |
| 72 | + |
| 73 | + invalid: [ |
| 74 | + { |
| 75 | + code: ` |
| 76 | + module.exports = { |
| 77 | + meta: { |
| 78 | + docs, |
| 79 | + fixable, |
| 80 | + type: 'problem', |
| 81 | + }, |
| 82 | + create() {}, |
| 83 | + };`, |
| 84 | + |
| 85 | + output: ` |
| 86 | + module.exports = { |
| 87 | + meta: { |
| 88 | + type: 'problem', |
| 89 | + docs, |
| 90 | + fixable, |
| 91 | + }, |
| 92 | + create() {}, |
| 93 | + };`, |
| 94 | + errors: [{ message: getMessage(['type', 'docs', 'fixable']) }], |
| 95 | + }, |
| 96 | + { |
| 97 | + code: ` |
| 98 | + module.exports = { |
| 99 | + meta: {schema, fixable, type, docs}, |
| 100 | + create() {}, |
| 101 | + };`, |
| 102 | + |
| 103 | + output: ` |
| 104 | + module.exports = { |
| 105 | + meta: {type, docs, fixable, schema}, |
| 106 | + create() {}, |
| 107 | + };`, |
| 108 | + errors: [ |
| 109 | + { message: getMessage(['type', 'docs', 'fixable', 'schema']) }, |
| 110 | + { message: getMessage(['type', 'docs', 'fixable', 'schema']) }, |
| 111 | + ], |
| 112 | + }, |
| 113 | + |
| 114 | + { |
| 115 | + code: ` |
| 116 | + module.exports = { |
| 117 | + meta: {fixable, fooooooooo, doc, type}, |
| 118 | + create() {}, |
| 119 | + };`, |
| 120 | + |
| 121 | + output: ` |
| 122 | + module.exports = { |
| 123 | + meta: {type, doc, fixable, fooooooooo}, |
| 124 | + create() {}, |
| 125 | + };`, |
| 126 | + options: [['type', 'doc', 'fixable']], |
| 127 | + errors: [ |
| 128 | + { message: getMessage(['type', 'doc', 'fixable']) }, |
| 129 | + { message: getMessage(['type', 'doc', 'fixable']) }, |
| 130 | + ], |
| 131 | + }, |
| 132 | + ], |
| 133 | +}); |
0 commit comments