diff --git a/lib/rules/define-macros-order.js b/lib/rules/define-macros-order.js index 7c915ca80..f24c58af7 100644 --- a/lib/rules/define-macros-order.js +++ b/lib/rules/define-macros-order.js @@ -104,7 +104,7 @@ function create(context) { const order = (options[0] && options[0].order) || DEFAULT_ORDER /** @type {boolean} */ const defineExposeLast = (options[0] && options[0].defineExposeLast) || false - /** @type {Map} */ + /** @type {Map} */ const macrosNodes = new Map() /** @type {ASTNode} */ let defineExposeNode @@ -112,19 +112,21 @@ function create(context) { return utils.compositingVisitors( utils.defineScriptSetupVisitor(context, { onDefinePropsExit(node) { - macrosNodes.set(MACROS_PROPS, getDefineMacrosStatement(node)) + macrosNodes.set(MACROS_PROPS, [getDefineMacrosStatement(node)]) }, onDefineEmitsExit(node) { - macrosNodes.set(MACROS_EMITS, getDefineMacrosStatement(node)) + macrosNodes.set(MACROS_EMITS, [getDefineMacrosStatement(node)]) }, onDefineOptionsExit(node) { - macrosNodes.set(MACROS_OPTIONS, getDefineMacrosStatement(node)) + macrosNodes.set(MACROS_OPTIONS, [getDefineMacrosStatement(node)]) }, onDefineSlotsExit(node) { - macrosNodes.set(MACROS_SLOTS, getDefineMacrosStatement(node)) + macrosNodes.set(MACROS_SLOTS, [getDefineMacrosStatement(node)]) }, onDefineModelExit(node) { - macrosNodes.set(MACROS_MODEL, getDefineMacrosStatement(node)) + const currentModelMacros = macrosNodes.get(MACROS_MODEL) ?? [] + currentModelMacros.push(getDefineMacrosStatement(node)) + macrosNodes.set(MACROS_MODEL, currentModelMacros) }, onDefineExposeExit(node) { defineExposeNode = getDefineMacrosStatement(node) @@ -142,7 +144,10 @@ function create(context) { program ) const orderedList = order - .map((name) => ({ name, node: macrosNodes.get(name) })) + .flatMap((name) => { + const nodes = macrosNodes.get(name) ?? [] + return nodes.map((node) => ({ name, node })) + }) .filter( /** @returns {data is OrderedData} */ (data) => utils.isDef(data.node) @@ -165,6 +170,7 @@ function create(context) { .map(({ node }) => node) const targetStatementIndex = moveTargetNodes.indexOf(targetStatement) + if (targetStatementIndex >= 0) { moveTargetNodes = moveTargetNodes.slice(0, targetStatementIndex) } diff --git a/tests/lib/rules/define-macros-order.js b/tests/lib/rules/define-macros-order.js index 09ac0208d..78c3637d2 100644 --- a/tests/lib/rules/define-macros-order.js +++ b/tests/lib/rules/define-macros-order.js @@ -238,6 +238,22 @@ tester.run('define-macros-order', rule, { parser: require.resolve('@typescript-eslint/parser') } } + }, + { + filename: 'test.vue', + code: ` + + `, + options: [ + { + order: ['defineModel', 'defineSlots'] + } + ] } ], invalid: [ @@ -818,6 +834,103 @@ tester.run('define-macros-order', rule, { line: 8 } ] + }, + { + filename: 'test.vue', + code: ` + + `, + output: ` + + `, + options: [ + { + order: ['defineModel', 'defineOptions'] + } + ], + errors: [ + { + message: message('defineModel'), + line: 6 + } + ] + }, + { + filename: 'test.vue', + code: ` + + `, + output: ` + + `, + options: [ + { + order: ['defineModel', 'defineOptions'] + } + ], + errors: [ + { + message: message('defineModel'), + line: 5 + } + ] + }, + { + filename: 'test.vue', + code: ` + + `, + output: ` + + `, + options: [ + { + order: ['defineModel', 'defineOptions'], + defineExposeLast: true + } + ], + errors: [ + { + message: message('defineModel'), + line: 5 + }, + { + message: defineExposeNotTheLast, + line: 6 + } + ] } ] })