Repository navigation
feat(define-macros-order): add allowReferencedAbove option - #3120
ValentinYoushkevich wants to merge 15 commits into
Conversation
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 detectedLatest commit: f55ee72 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
…s-order-allow-types # Conflicts: # lib/rules/define-macros-order.ts
…s-order-allow-types
allowTypesBetweenMacros optionallowReferencedAbove option
|
Reworked into |
|
|
||
| </eslint-code-block> | ||
|
|
||
| <eslint-code-block fix :rules="{'vue/define-macros-order': ['error', {order: ['defineModel', 'defineProps', 'defineEmits']}]}"> |
There was a problem hiding this comment.
The rule definition doesn't match the heading
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
But now the example doesn't show that variables are also allowed. It could look like only referenced types/interfaces are allowed.
There was a problem hiding this comment.
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.
…to their heading
…erencedAbove` examples again
…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.
…s-order-allow-types
Closes #2978, closes #2380, closes #2746.
As agreed in #2746, this adds a boolean
allowReferencedAboveoption (defaulttrue), replacing theallowTypesBetweenMacrosoption 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 Itemused byinterface PropsfordefineProps<Props>(). Any other statement between the macros is still reported, andfalsekeeps 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++betweenlet count = 0andconst api = { count }), withfalsetoo; 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.