|
1 | 1 | import type { CodemodPlugin } from 'vue-metamorph' |
2 | 2 | import { findClassNodes } from '../../helpers' |
3 | 3 |
|
| 4 | +const breakpoints = ['sm', 'md', 'lg', 'xl', 'xxl'] as const |
4 | 5 | const replacements: Record<string, string> = { |
5 | | - 'text-h1': 'text-display-large', |
6 | | - 'text-h2': 'text-display-medium', |
7 | | - 'text-h3': 'text-display-small', |
8 | | - 'text-h4': 'text-headline-large', |
9 | | - 'text-h5': 'text-headline-medium', |
10 | | - 'text-h6': 'text-headline-small', |
11 | | - 'text-subtitle-1': 'text-body-large', |
12 | | - 'text-subtitle-2': 'text-label-large', |
13 | | - 'text-body-1': 'text-body-large', |
14 | | - 'text-body-2': 'text-body-medium', |
15 | | - 'text-caption': 'text-body-small', |
16 | | - 'text-button': 'text-label-large', |
17 | | - 'text-overline': 'text-label-small', |
| 6 | + 'h1': '-display-large', |
| 7 | + 'h2': '-display-medium', |
| 8 | + 'h3': '-display-small', |
| 9 | + 'h4': '-headline-large', |
| 10 | + 'h5': '-headline-medium', |
| 11 | + 'h6': '-headline-small', |
| 12 | + 'subtitle-1': '-body-large', |
| 13 | + 'subtitle-2': '-label-large', |
| 14 | + 'body-1': '-body-large', |
| 15 | + 'body-2': '-body-medium', |
| 16 | + 'caption': '-body-small', |
| 17 | + 'button': '-label-large', |
| 18 | + 'overline': '-label-small', |
18 | 19 | } |
19 | 20 |
|
20 | | -const matchingRegexp = new RegExp(String.raw`(^|\s)(${Object.keys(replacements).join('|')})(?=$|\s)`, 'g') |
| 21 | +const matcher = String.raw`text(-(?:${breakpoints.join('|')}))?-(${Object.keys(replacements).join('|')})` |
21 | 22 |
|
22 | 23 | export const v4TypographyPlugin: CodemodPlugin = { |
23 | 24 | type: 'codemod', |
24 | 25 | name: 'vuetify-4-typography', |
25 | 26 | transform ({ sfcAST, utils }) { |
26 | 27 | if (!sfcAST) return 0 |
27 | 28 | let count = 0 |
28 | | - const found = findClassNodes(sfcAST, utils, Object.keys(replacements)) |
29 | | - for (const node of found) { |
| 29 | + const { results, matchingRegexp } = findClassNodes(sfcAST, utils, [matcher]) |
| 30 | + for (const node of results) { |
30 | 31 | if (node.type === 'Identifier') { |
31 | | - node.name = node.name.replaceAll(matchingRegexp, (_, s, m) => `${s}${replacements[m]}`) |
| 32 | + node.name = node.name.replaceAll(matchingRegexp, (_, s, b, m) => `${s}text${b || ''}${replacements[m]}`) |
32 | 33 | count++ |
33 | 34 | } else if (typeof node.value === 'string') { |
34 | | - node.value = node.value.replaceAll(matchingRegexp, (_, s, m) => `${s}${replacements[m]}`) |
| 35 | + node.value = node.value.replaceAll(matchingRegexp, (_, s, b, m) => `${s}text${b || ''}${replacements[m]}`) |
35 | 36 | count++ |
36 | 37 | } |
37 | 38 | } |
|
0 commit comments