Skip to content

Commit

Permalink
Merge pull request #66 from bywhitebird/65-use-babelparser-instead-of…
Browse files Browse the repository at this point in the history
…-typescript-eslintparser-in-kaz-ast

refactor(KazAst): use `@babel/parser` instead of `@typescript-eslint/parser`
  • Loading branch information
arthur-fontaine authored Nov 3, 2023
2 parents 5b14ea6 + a43ade4 commit 6033be2
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 19 deletions.
2 changes: 1 addition & 1 deletion packages/kaz-ast/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@typescript-eslint/parser": "^5.50.0",
"@babel/parser": "^7.22.5",
"eslint": "8.46.0",
"typescript": "^5.0.0",
"zod": "^3.21.4"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { parse } from '@typescript-eslint/parser'
import { parse } from '@babel/parser'

import { ConditionContext } from '..'
import { Token } from '../../../lib/voltair'

const getExpression = (rawValue: string) => {
const parsed = parse(`if (${rawValue}) {}`, { warnOnUnsupportedTypeScriptVersion: false })
const ifExpression = parsed.body[0]
const parsed = parse(`if (${rawValue}) {}`, {
plugins: ['typescript'],
})
const ifExpression = parsed.program.body[0]

if (
ifExpression?.type !== 'IfStatement'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { parse } from '@typescript-eslint/parser'
import { parse } from '@babel/parser'

import { ForParametersContext } from '..'
import { Token } from '../../../lib/voltair'

const getExpression = (rawValue: string) => {
const parsed = parse(`for (${rawValue}) {}`, { warnOnUnsupportedTypeScriptVersion: false })
const forExpression = parsed.body[0]
const parsed = parse(`for (${rawValue}) {}`, {
plugins: ['typescript'],
})
const forExpression = parsed.program.body[0]

if (
forExpression?.type !== 'ForStatement'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { parse } from '@typescript-eslint/parser'
import { parse } from '@babel/parser'

import { ExpressionToken } from '..'

const getExpression = (rawValue: string) => {
const parsed = parse(`() => ${rawValue}`, { warnOnUnsupportedTypeScriptVersion: false })
const arrowFunctionExpression = parsed.body[0]
const parsed = parse(`() => ${rawValue}`, {
plugins: ['typescript'],
})
const arrowFunctionExpression = parsed.program.body[0]

if (
arrowFunctionExpression?.type !== 'ExpressionStatement'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { parse } from '@typescript-eslint/parser'
import { parse } from '@babel/parser'

import { ExpressionContext } from '..'
import { TagAttributeValueExpressionContext } from '../../../features/tag'
import { TemplateExpressionContext } from '../../../features/template'
import { Token } from '../../../lib/voltair'

const getExpression = (rawValue: string) => {
const parsed = parse(`const _ = ${rawValue}`, { warnOnUnsupportedTypeScriptVersion: false, range: true })
const variable = parsed.body[0]
const parsed = parse(`const _ = ${rawValue}`, {
plugins: ['typescript'],
})
const variable = parsed.program.body[0]

if (variable?.type !== 'VariableDeclaration' || variable.declarations[0] === undefined)
throw new Error(`Invalid expression: ${rawValue}`)
Expand Down
8 changes: 5 additions & 3 deletions packages/kaz-ast/src/shared/type/tokens/TypeToken.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { parse } from '@typescript-eslint/parser'
import { parse } from '@babel/parser'

import { TypeContext } from '..'
import { Token } from '../../../lib/voltair'

const getTypeAnnotation = (rawValue: string) => {
const parsed = parse(`type T = ${rawValue}`, { warnOnUnsupportedTypeScriptVersion: false })
const type = parsed.body[0]
const parsed = parse(`type T = ${rawValue}`, {
plugins: ['typescript'],
})
const type = parsed.program.body[0]

if (type?.type !== 'TSTypeAliasDeclaration')
throw new Error(`Invalid type: ${rawValue}`)
Expand Down
8 changes: 5 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6033be2

Please sign in to comment.