Skip to content

Commit dad15a1

Browse files
committed
fix: improve rewrite check
1 parent a32c24d commit dad15a1

File tree

2 files changed

+23
-37
lines changed

2 files changed

+23
-37
lines changed

rewrite/shared.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
import type { Group, Replacement } from './config'
1+
import type { Group } from './config'
22

33
export function groupMatches(content: string, group: Group) {
44
const markers = group.markers || []
55
return markers.every((marker) => content.includes(marker))
66
}
77

8-
export function replacementWouldChange(content: string, replacement: Replacement) {
9-
if (typeof replacement.pattern === 'string') {
10-
return content.includes(replacement.pattern)
11-
}
12-
const re = new RegExp(replacement.pattern.source, replacement.pattern.flags)
13-
return re.test(content)
14-
}
15-
168
export function applyGroups(content: string, groups: Group[]) {
179
let out = content
1810
for (const group of groups) {
@@ -31,16 +23,3 @@ export function applyGroups(content: string, groups: Group[]) {
3123
}
3224
return { content: out, changed: out !== content }
3325
}
34-
35-
export function analyze(content: string, groups: Group[]) {
36-
for (const group of groups) {
37-
if (!groupMatches(content, group)) {
38-
continue
39-
}
40-
if (group.replacements.some((r) => replacementWouldChange(content, r))) {
41-
return { matched: true, rewritable: true }
42-
}
43-
return { matched: true, rewritable: false }
44-
}
45-
return { matched: false, rewritable: false }
46-
}

scripts/check-rewrite.ts

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { GROUPS } from '@/rewrite/config'
2-
import { analyze } from '@/rewrite/shared'
2+
import { applyGroups, groupMatches } from '@/rewrite/shared'
33
import { chromium } from 'playwright-chromium'
44
import rules from '../public/rules/figma.json'
55

@@ -56,36 +56,43 @@ async function runCheck() {
5656
await page.goto(`https://www.figma.com/design/${process.env.FIGMA_FILE_KEY}`)
5757
console.log(`Page loaded at <${page.url()}>.`)
5858

59-
let matched: string | null = null
60-
let rewritable = false
59+
let matchedCount = 0
60+
let rewrittenCount = 0
61+
const notRewritten: string[] = []
6162

6263
for (const { url, content } of scripts) {
63-
const { matched: m, rewritable: r } = analyze(content, GROUPS)
64-
if (!m) {
64+
const matched = GROUPS.some((group) => groupMatches(content, group))
65+
if (!matched) {
6566
continue
6667
}
67-
matched = url
68+
69+
matchedCount++
6870
console.log(`Matched script: <${url}>.`)
69-
if (r) {
70-
rewritable = true
71-
console.log(`Rewritable script: <${url}>.`)
72-
break
71+
72+
const { changed } = applyGroups(content, GROUPS)
73+
if (changed) {
74+
rewrittenCount++
75+
console.log(`Rewritable (would change): <${url}>.`)
76+
} else {
77+
notRewritten.push(url)
78+
console.log(`Not rewritable (no change produced): <${url}>.`)
7379
}
7480
}
7581

76-
if (!matched) {
82+
if (matchedCount === 0) {
7783
console.log('❌ No matched script found.')
7884
return false
7985
}
8086

81-
console.log(`✅ Matched script: <${matched}>.`)
87+
console.log(`✅ Matched ${matchedCount} script(s).`)
8288

83-
if (!rewritable) {
84-
console.log('❌ Rewrite pattern not found.')
89+
if (rewrittenCount !== matchedCount) {
90+
console.log('❌ Some matched scripts would not be rewritten by rules:')
91+
notRewritten.forEach((url) => console.log(` - <${url}>`))
8592
return false
8693
}
8794

88-
console.log('✅ Rewrite pattern found.')
95+
console.log('✅ All matched scripts would be rewritten by rules.')
8996
return true
9097
} finally {
9198
await browser.close()

0 commit comments

Comments
 (0)