Skip to content

Commit 4596fcc

Browse files
committed
fix(typography): replace classes with breakpoints
1 parent 5d23bbf commit 4596fcc

4 files changed

Lines changed: 44 additions & 24 deletions

File tree

src/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export function findClassNodes (ast: AST.VDocumentFragment, utils: CodemodPlugin
228228
})
229229
}
230230

231-
return results
231+
return { results, matchingRegexp: new RegExp(matchingRegexp, 'g') }
232232
}
233233

234234
export function removeDotMember (ref: VueAST.ESLintIdentifier, name: string) {

src/plugins/v4/elevation.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ const elevationComponents = new Set([
5151
const mapping = [0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5]
5252

5353
const elevationMatch = String.raw`elevation-(\d{1,2})`
54-
const elevationRegexp = new RegExp(String.raw`(^|\s)${elevationMatch}(?=$|\s)`, 'g')
5554

5655
export const v4ElevationPlugin: CodemodPlugin = {
5756
type: 'codemod',
@@ -61,13 +60,13 @@ export const v4ElevationPlugin: CodemodPlugin = {
6160
let count = 0
6261

6362
// Match classes
64-
const found = findClassNodes(sfcAST, utils, [elevationMatch])
65-
for (const node of found) {
63+
const { results, matchingRegexp } = findClassNodes(sfcAST, utils, [elevationMatch])
64+
for (const node of results) {
6665
if (node.type === 'Identifier') {
67-
node.name = node.name.replaceAll(elevationRegexp, (_, s, n) => `${s}elevation-${mapping[Number(n)]}`)
66+
node.name = node.name.replaceAll(matchingRegexp, (_, s, n) => `${s}elevation-${mapping[Number(n)]}`)
6867
count++
6968
} else if (typeof node.value === 'string') {
70-
node.value = node.value.replaceAll(elevationRegexp, (_, s, n) => `${s}elevation-${mapping[Number(n)]}`)
69+
node.value = node.value.replaceAll(matchingRegexp, (_, s, n) => `${s}elevation-${mapping[Number(n)]}`)
7170
count++
7271
}
7372
}

src/plugins/v4/typography.spec.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,23 @@ it('replaces classes', () => {
2929
"
3030
`)
3131
})
32+
33+
it('replaces classes with breakpoints', () => {
34+
const input = `
35+
<template>
36+
<div class="text-h1" />
37+
<div class="text-md-h1" />
38+
<div :class="{ 'text-md-center': true }" />
39+
</template>
40+
`
41+
42+
expect(transform(input, 'file.vue', [v4TypographyPlugin]).code).toMatchInlineSnapshot(`
43+
"
44+
<template>
45+
<div class="text-display-large" />
46+
<div class="text-md-display-large" />
47+
<div :class="{ 'text-md-center': true }" />
48+
</template>
49+
"
50+
`)
51+
})

src/plugins/v4/typography.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
import type { CodemodPlugin } from 'vue-metamorph'
22
import { findClassNodes } from '../../helpers'
33

4+
const breakpoints = ['sm', 'md', 'lg', 'xl', 'xxl'] as const
45
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',
1819
}
1920

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('|')})`
2122

2223
export const v4TypographyPlugin: CodemodPlugin = {
2324
type: 'codemod',
2425
name: 'vuetify-4-typography',
2526
transform ({ sfcAST, utils }) {
2627
if (!sfcAST) return 0
2728
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) {
3031
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]}`)
3233
count++
3334
} 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]}`)
3536
count++
3637
}
3738
}

0 commit comments

Comments
 (0)