Skip to content

Instantly share code, notes, and snippets.

@ka2n
Created August 20, 2024 05:52
Show Gist options
  • Save ka2n/0f3abc07ee0890a12da541e88904b37f to your computer and use it in GitHub Desktop.
Save ka2n/0f3abc07ee0890a12da541e88904b37f to your computer and use it in GitHub Desktop.
remove pathpida 50%
import {pagesPath} from './lib/$path.ts'
type Rule = {
pattern: string
fix: string
id: string
}
const rulesForLevel = (paths: any, parent: string[] = []) => {
let rules: Rule[] = []
for (let k of Object.keys(paths)) {
if (k === '$url' && typeof paths[k] === 'function') {
// add rule
try {
const result = paths[k]()
rules.push(
{
id: parent.join('_') + '_' + k + '_pathname',
pattern: `${parent.join('.')}.${k}().pathname`,
fix: `"'${result.pathname}'"`
},
{
id: parent.join('.') + '.' + k,
pattern: `${parent.join('.')}.${k}()`,
fix: `"'${result.pathname}'"`
},
)
} catch (e) {
console.error(e)
}
continue
}
const path = paths[k]
if (typeof path === 'object') {
rules.push(...rulesForLevel(path, [...parent, k]))
}
}
return rules
}
const main = () => {
const rules = rulesForLevel(pagesPath, ['pagesPath'])
let yaml = ''
for (let rule of rules) {
yaml += "---\n"
yaml += `id: ${rule.id}\n`
yaml += `language: tsx\n`
yaml += `rule:\n `
yaml += `pattern: ${rule.pattern}\n`
yaml += `fix: ${rule.fix}\n`
}
console.log(yaml)
}
main()
#!/bin/sh
deno ./gen-ast-gep-manifest.ts > pattern.yaml
ast-grep scan -r ./pattern.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment