|
| 1 | +/** |
| 2 | + * @fileoverview Keep order of properties in components |
| 3 | + * @author Michał Sajnóg |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const defaultOrder = [ |
| 8 | + ['name', 'delimiters', 'functional', 'model'], |
| 9 | + ['components', 'directives', 'filters'], |
| 10 | + ['parent', 'mixins', 'extends', 'provide', 'inject'], |
| 11 | + 'el', |
| 12 | + 'template', |
| 13 | + 'props', |
| 14 | + 'propsData', |
| 15 | + 'data', |
| 16 | + 'computed', |
| 17 | + 'watch', |
| 18 | + 'LIFECYCLE_HOOKS', |
| 19 | + 'methods', |
| 20 | + 'render', |
| 21 | + 'renderError' |
| 22 | +] |
| 23 | + |
| 24 | +const groups = { |
| 25 | + LIFECYCLE_HOOKS: [ |
| 26 | + 'beforeCreate', |
| 27 | + 'created', |
| 28 | + 'beforeMount', |
| 29 | + 'mounted', |
| 30 | + 'beforeUpdate', |
| 31 | + 'updated', |
| 32 | + 'activated', |
| 33 | + 'deactivated', |
| 34 | + 'beforeDestroy', |
| 35 | + 'destroyed' |
| 36 | + ] |
| 37 | +} |
| 38 | + |
| 39 | +function isComponentFile (node, path) { |
| 40 | + const isVueFile = path.endsWith('.vue') || path.endsWith('.jsx') |
| 41 | + return isVueFile && node.declaration.type === 'ObjectExpression' |
| 42 | +} |
| 43 | + |
| 44 | +function isVueComponent (node) { |
| 45 | + const callee = node.callee |
| 46 | + |
| 47 | + const isFullVueComponent = node.type === 'CallExpression' && |
| 48 | + callee.type === 'MemberExpression' && |
| 49 | + callee.object.type === 'Identifier' && |
| 50 | + callee.object.name === 'Vue' && |
| 51 | + callee.property.type === 'Identifier' && |
| 52 | + callee.property.name === 'component' && |
| 53 | + node.arguments.length && |
| 54 | + node.arguments.slice(-1)[0].type === 'ObjectExpression' |
| 55 | + |
| 56 | + const isDestructedVueComponent = callee.type === 'Identifier' && |
| 57 | + callee.name === 'component' |
| 58 | + |
| 59 | + return isFullVueComponent || isDestructedVueComponent |
| 60 | +} |
| 61 | + |
| 62 | +function isVueInstance (node) { |
| 63 | + const callee = node.callee |
| 64 | + return node.type === 'NewExpression' && |
| 65 | + callee.type === 'Identifier' && |
| 66 | + callee.name === 'Vue' && |
| 67 | + node.arguments.length && |
| 68 | + node.arguments[0].type === 'ObjectExpression' |
| 69 | +} |
| 70 | + |
| 71 | +function getOrderMap (order) { |
| 72 | + const orderMap = new Map() |
| 73 | + |
| 74 | + order.forEach((property, i) => { |
| 75 | + if (Array.isArray(property)) { |
| 76 | + property.forEach(p => orderMap.set(p, i)) |
| 77 | + } else { |
| 78 | + orderMap.set(property, i) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + return orderMap |
| 83 | +} |
| 84 | + |
| 85 | +function checkOrder (propertiesNodes, orderMap, context) { |
| 86 | + const properties = propertiesNodes.map(property => property.key) |
| 87 | + |
| 88 | + properties.forEach((property, i) => { |
| 89 | + const propertiesAbove = properties.slice(0, i) |
| 90 | + const unorderedProperties = propertiesAbove |
| 91 | + .filter(p => orderMap.get(p.name) > orderMap.get(property.name)) |
| 92 | + .sort((p1, p2) => orderMap.get(p1.name) > orderMap.get(p2.name)) |
| 93 | + |
| 94 | + const firstUnorderedProperty = unorderedProperties[0] |
| 95 | + |
| 96 | + if (firstUnorderedProperty) { |
| 97 | + const line = firstUnorderedProperty.loc.start.line |
| 98 | + context.report({ |
| 99 | + node: property, |
| 100 | + message: `The "${property.name}" property should be above the "${firstUnorderedProperty.name}" property on line ${line}.` |
| 101 | + }) |
| 102 | + } |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +function create (context) { |
| 107 | + const options = context.options[0] || {} |
| 108 | + const order = options.order || defaultOrder |
| 109 | + const filePath = context.getFilename() |
| 110 | + |
| 111 | + const extendedOrder = order.map(property => groups[property] || property) |
| 112 | + const orderMap = getOrderMap(extendedOrder) |
| 113 | + |
| 114 | + return { |
| 115 | + ExportDefaultDeclaration (node) { |
| 116 | + // export default {} in .vue || .jsx |
| 117 | + if (!isComponentFile(node, filePath)) return |
| 118 | + checkOrder(node.declaration.properties, orderMap, context) |
| 119 | + }, |
| 120 | + CallExpression (node) { |
| 121 | + // Vue.component('xxx', {}) || component('xxx', {}) |
| 122 | + if (!isVueComponent(node)) return |
| 123 | + checkOrder(node.arguments.slice(-1)[0].properties, orderMap, context) |
| 124 | + }, |
| 125 | + NewExpression (node) { |
| 126 | + // new Vue({}) |
| 127 | + if (!isVueInstance(node)) return |
| 128 | + checkOrder(node.arguments[0].properties, orderMap, context) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// ------------------------------------------------------------------------------ |
| 134 | +// Rule Definition |
| 135 | +// ------------------------------------------------------------------------------ |
| 136 | + |
| 137 | +module.exports = { |
| 138 | + create, |
| 139 | + meta: { |
| 140 | + docs: { |
| 141 | + description: 'Keep order of properties in components', |
| 142 | + category: 'Best Practices', |
| 143 | + recommended: false |
| 144 | + }, |
| 145 | + fixable: null, |
| 146 | + schema: [] |
| 147 | + } |
| 148 | +} |
0 commit comments