Skip to content

Commit

Permalink
Merge pull request #68 from bywhitebird/67-bad-expression-transformat…
Browse files Browse the repository at this point in the history
…ion-in-vue

67 bad expression transformation in vue
  • Loading branch information
arthur-fontaine authored Nov 10, 2023
2 parents 6033be2 + 9ea891b commit 772dfd7
Show file tree
Hide file tree
Showing 44 changed files with 740 additions and 382 deletions.
2 changes: 2 additions & 0 deletions packages/kaz-ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const parse = (tokens: Token[]) => {
export const semanticTokensLegend = getSemanticTokensLegend(config.tokens)

export * from './types/KazAst'

export { traverse } from './lib/traverse'
33 changes: 33 additions & 0 deletions packages/kaz-ast/src/lib/traverse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { z } from 'zod'

import type * as schemas from '../types/KazAst'

type AllSchemas = typeof schemas[keyof typeof schemas]
type AllInferedSchemas = Extract<z.infer<AllSchemas>, { $type: string }>
type Visitor = {
[T in AllInferedSchemas['$type']]?: (node: Extract<AllInferedSchemas, { $type: T }>) => void
} & {
enter?: (node: AllInferedSchemas) => void
}

export const traverse = (ast: AllInferedSchemas, visitor: Visitor) => {
const traverse = (node: AllInferedSchemas) => {
(visitor[node.$type] ?? visitor.enter)?.(node as never)

for (const key in node) {
const value = node[key as keyof typeof node] as unknown

if (Array.isArray(value)) {
for (const item of value) {
if (typeof item === 'object' && item !== null)
traverse(item)
}
}
else if (typeof value === 'object' && value !== null && '$type' in value) {
traverse(value as AllInferedSchemas)
}
}
}

traverse(ast)
}
9 changes: 8 additions & 1 deletion packages/parser-kaz/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,18 @@
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@babel/parser": "^7.22.5",
"@babel/traverse": "^7.22.5",
"@whitebird/kaz-ast": "workspace:*",
"@whitebird/kazam-parser-base": "workspace:*",
"glob": "10.2.1"
"@whitebird/kazam-transform-utils": "workspace:*",
"@whitebird/kazam-transformer-typescript": "workspace:*",
"glob": "10.2.1",
"magic-string": "^0.30.5"
},
"devDependencies": {
"@babel/types": "^7.22.5",
"@types/babel__traverse": "^7.20.1",
"@types/node": "^18.11.9",
"@vitest/coverage-v8": "^0.34.1",
"@whitebird/kazam-transformer-base": "workspace:*",
Expand Down
4 changes: 4 additions & 0 deletions packages/parser-kaz/src/parser-kaz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { parse, tokenize } from '@whitebird/kaz-ast'
import { ParserBase } from '@whitebird/kazam-parser-base'
import { glob } from 'glob'

import { transformAst } from './transform-ast'

export class ParserKaz extends ParserBase<{
pathRelativeToInputPath: string
inputPath: string
Expand Down Expand Up @@ -58,6 +60,8 @@ export class ParserKaz extends ParserBase<{
if (ast === undefined)
throw new Error(`Could not parse file ${filePath}`)

transformAst(ast, { fileName: filePath })

kazAsts[pathRelativeToInputPath] = {
ast,
sourceAbsoluteFilePath: filePath,
Expand Down
Loading

0 comments on commit 772dfd7

Please sign in to comment.