Skip to content

feat(define-macros-order): add allowReferencedAbove option - #3120

Open
ValentinYoushkevich wants to merge 15 commits into
vuejs:masterfrom
ValentinYoushkevich:feat/define-macros-order-allow-types
Open

ValentinYoushkevich wants to merge 15 commits into
vuejs:masterfrom
ValentinYoushkevich:feat/define-macros-order-allow-types

Conversation

@ValentinYoushkevich

@ValentinYoushkevich ValentinYoushkevich commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Closes #2978, closes #2380, closes #2746.

As agreed in #2746, this adds a boolean allowReferencedAbove option (default true), replacing the allowTypesBetweenMacros option from this PR's first version. It lets declarations that a macro references (types, interfaces, enums, ambient declarations, variables, functions and classes) sit right above that macro, below the previous one, also when referenced indirectly, e.g. interface Item used by interface Props for defineProps<Props>(). Any other statement between the macros is still reported, and false keeps the current strict behavior. The autofix moves referenced declarations along with their macro. It now offers no fix when moving a macro would put it above a runtime declaration it references, or above a statement that uses the same variable at runtime (e.g. count++ between let count = 0 and const api = { count }), with false too; previously it always fixed, which could produce broken code or silently change behavior. It also keeps semicolon-less code valid when a moved or passed-over statement starts with (, [ or a template literal.

When enabled, TypeScript syntax that is fully erased at compile time
(type aliases, interfaces, `declare` statements, type-only imports and
exports, ambient function declarations) may sit between the compiler
macros without being reported. Anything that emits runtime code, such as
`enum`, `const enum` or a non-`declare`d `namespace`, is still reported.

Defaults to `false`, so the existing behaviour is unchanged.

Closes vuejs#2978
@changeset-bot

changeset-bot Bot commented Aug 12, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f55ee72

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
eslint-plugin-vue Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment thread docs/rules/define-macros-order.md Resolved
@ValentinYoushkevich ValentinYoushkevich changed the title feat(define-macros-order): add allowTypesBetweenMacros option feat(define-macros-order): add allowReferencedAbove option Nov 14, 2026
@ValentinYoushkevich

Copy link
Copy Markdown
Contributor Author

Reworked into allowReferencedAbove as agreed in #2746, so I think the do not merge label can go now.

Comment thread docs/rules/define-macros-order.md Outdated

</eslint-code-block>

<eslint-code-block fix :rules="{'vue/define-macros-order': ['error', {order: ['defineModel', 'defineProps', 'defineEmits']}]}">

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The rule definition doesn't match the heading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed, both examples now use { allowReferencedAbove: true }. I dropped the defineModel part from them, since with the default order the rule doesn't check where defineModel is, so that part only worked with a custom order.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But now the example doesn't show that variables are also allowed. It could look like only referenced types/interfaces are allowed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The section now uses exactly {allowReferencedAbove: true} from its heading, and the GOOD example shows a variable through a withDefaults default. The false section also got a GOOD example. While re-checking, I also fixed a false negative (an if or for with a var between macros was accepted) and stopped types from blocking the autofix; the description is updated accordingly.

…r message

With `allowReferencedAbove` (the default), a macro may also be placed below the declarations it references, so say so in the "not at the top" message. The previous message is kept for `allowReferencedAbove: false`.
…on't let types block the fix

- Only top-level declarations (variables, functions, classes, enums,
  namespaces and types, also when exported) are allowed above the macro
  that references them. A statement like `if (...) { var x }`, a `for`
  loop with a `var`, or `import x = ...` is reported again, as before
  `allowReferencedAbove` was added.
- Types can be used above their declaration, so a referenced type no
  longer prevents the autofix. When a later macro references the
  statement the macros are moved above, only the previous macros are
  moved.
- Find the top-level statement through `parent`, collect the referenced
  statements recursively, and check `isValueReference` without a cast.
…heir heading

Use exactly the heading's config in the `{ "allowReferencedAbove": true }` section and show a referenced variable through a `withDefaults` default, split the BAD example into one block per reported problem, add a GOOD example to the `false` section, and update the rule and option descriptions and the changeset.
…endent statements

Also keep semicolon-less code valid when a moved or passed-over statement starts with `(`, `[` or a template literal.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants