|
| 1 | +<!-- Remove Badge if it does not have fix--> |
| 2 | +## Upgrade Ant Design Vue <Badge type="tip" text="Has Fix" /> |
| 3 | + |
| 4 | +* [Playground Link](/playground.html#eyJtb2RlIjoiQ29uZmlnIiwibGFuZyI6Imh0bWwiLCJxdWVyeSI6IiIsInJld3JpdGUiOiIiLCJzdHJpY3RuZXNzIjoicmVsYXhlZCIsInNlbGVjdG9yIjoiIiwiY29uZmlnIjoidXRpbHM6XG4gIGluc2lkZS10YWc6XG4gICAgaW5zaWRlOlxuICAgICAga2luZDogZWxlbWVudCBcbiAgICAgIHN0b3BCeTogeyBraW5kOiBlbGVtZW50IH1cbiAgICAgIGhhczpcbiAgICAgICAgc3RvcEJ5OiB7IGtpbmQ6IHRhZ19uYW1lIH1cbiAgICAgICAga2luZDogdGFnX25hbWVcbiAgICAgICAgcGF0dGVybjogJFRBR19OQU1FXG5ydWxlOlxuICBraW5kOiBhdHRyaWJ1dGVfbmFtZVxuICByZWdleDogOnZpc2libGVcbiAgbWF0Y2hlczogaW5zaWRlLXRhZyAgXG5maXg6IDpvcGVuXG5jb25zdHJhaW50czpcbiAgVEFHX05BTUU6XG4gICAgcmVnZXg6IGEtbW9kYWx8YS10b29sdGlwIiwic291cmNlIjoiPHRlbXBsYXRlPlxuICA8YS1tb2RhbCA6dmlzaWJsZT1cInZpc2libGVcIj5jb250ZW50PC9hLW1vZGFsPlxuICA8YS10b29sdGlwIDp2aXNpYmxlPVwidmlzaWJsZVwiIC8+XG4gIDxhLXRhZyA6dmlzaWJsZT1cInZpc2libGVcIj50YWc8L2EtdGFnPlxuPC90ZW1wbGF0ZT4ifQ==) |
| 5 | + |
| 6 | +### Description |
| 7 | + |
| 8 | + |
| 9 | +ast-grep can be used to upgrade Vue template using the HTML parser. |
| 10 | + |
| 11 | +This rule is an example to upgrade [one breaking change](https://next.antdv.com/docs/vue/migration-v4#component-api-adjustment) in [Ant Design Vue](https://next.antdv.com/components/overview) from v3 to v4, unifying the controlled visible API of the component popup. |
| 12 | + |
| 13 | +It is designed to identify and replace the `visible` attribute with the `open` attribute for specific components like `a-modal` and `a-tooltip`. Note the rule should not replace other visible attributes that are not related to the component popup like `a-tag`. |
| 14 | + |
| 15 | +The rule can be broken down into the following steps: |
| 16 | +1. Find the target attribute name by `kind` and `regex` |
| 17 | +2. Find the attribute's enclosing element using `inside`, and get its tag name |
| 18 | +3. Ensure the tag name is related to popup components, using constraints |
| 19 | + |
| 20 | +<!-- Use YAML in the example. Delete this section if use pattern. --> |
| 21 | +### YAML |
| 22 | +```yaml |
| 23 | +utils: |
| 24 | + inside-tag: |
| 25 | + # find the enclosing element of the attribute |
| 26 | + inside: |
| 27 | + kind: element |
| 28 | + stopBy: { kind: element } # only the closest element |
| 29 | + # find the tag name and store it in metavar |
| 30 | + has: |
| 31 | + stopBy: { kind: tag_name } |
| 32 | + kind: tag_name |
| 33 | + pattern: $TAG_NAME |
| 34 | +rule: |
| 35 | + # find the target attribute_name |
| 36 | + kind: attribute_name |
| 37 | + regex: :visible |
| 38 | + # find the element |
| 39 | + matches: inside-tag |
| 40 | +# ensure it only matches modal/tooltip but not tag |
| 41 | +constraints: |
| 42 | + TAG_NAME: |
| 43 | + regex: a-modal|a-tooltip |
| 44 | +fix: :open |
| 45 | +``` |
| 46 | +
|
| 47 | +### Example |
| 48 | +
|
| 49 | +<!-- highlight matched code in curly-brace {lineNum} --> |
| 50 | +```html {2,3} |
| 51 | +<template> |
| 52 | + <a-modal :visible="visible">content</a-modal> |
| 53 | + <a-tooltip :visible="visible" /> |
| 54 | + <a-tag :visible="visible">tag</a-tag> |
| 55 | +</template> |
| 56 | +``` |
| 57 | + |
| 58 | +### Diff |
| 59 | +<!-- use // [!code --] and // [!code ++] to annotate diff --> |
| 60 | +```js |
| 61 | +<template> |
| 62 | + <a-modal :visible="visible">content</a-modal> // [!code --] |
| 63 | + <a-modal :open="visible">content</a-modal> // [!code ++] |
| 64 | + <a-tooltip :visible="visible" /> // [!code --] |
| 65 | + <a-tooltip :open="visible" /> // [!code ++] |
| 66 | + <a-tag :visible="visible">tag</a-tag> |
| 67 | +</template> |
| 68 | +``` |
| 69 | + |
| 70 | +### Contributed by |
| 71 | +Inspired by [Vue.js RFC](https://github.com/vuejs/rfcs/discussions/705#discussion-7255672) |
0 commit comments