Skip to content

Commit

Permalink
feat: refactor the ast parse
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterAlfredLee committed Jul 2, 2021
1 parent 4887fa2 commit 220fed5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 34 deletions.
50 changes: 33 additions & 17 deletions src/ast-parse/astParse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
import { Context, transformationMap } from './transformations/index'
import { vueSfcAstParser } from '@originjs/vue-sfc-ast-parser'
import { transformationMap } from './transformations/index'
import { SFCDescriptor, vueSfcAstParser } from '@originjs/vue-sfc-ast-parser'
import * as globby from 'globby'
import fs from 'fs'
import { JSCodeshift } from 'jscodeshift/src/core';

export type FileInfo = {
path: string,
source: string
}

export type VueSFCContext = {
path: string
source: string
// templateAST: ESLintProgram,
templateAST: any,
scriptAST: any,
jscodeshiftParser: JSCodeshift,
descriptor: SFCDescriptor
}

export function astParseRoot (rootDir: string) {
const resolvedPaths : string[] = globby.sync(rootDir)
Expand All @@ -14,9 +30,13 @@ export function astParseRoot (rootDir: string) {
const extension = (/\.([^.]*)$/.exec(filePath) || [])[0]

let fileChanged: boolean = false
let context = parseVueSfc(filePath)
let transformationResult: string = context.source
let tempTransformationResult: string|null
const source: string = fs.readFileSync(filePath).toString().split('\r\n').join('\n')
const fileInfo: FileInfo = {
path: filePath,
source: source
}
let transformationResult: string = source
let tempTransformationResult: string | null

// iter all transformations
for (const key in transformationMap) {
Expand All @@ -29,15 +49,15 @@ export function astParseRoot (rootDir: string) {
}

// execute the transformation
tempTransformationResult = transformation.transformAST(context)
tempTransformationResult = transformation.transformAST(fileInfo)
if (tempTransformationResult == null) {
continue
}
fileChanged = true
transformationResult = tempTransformationResult

if (transformation.needReparse) {
context = parseVueSfc(filePath, transformationResult)
fileInfo.source = transformationResult
}
}
if (fileChanged) {
Expand All @@ -46,18 +66,14 @@ export function astParseRoot (rootDir: string) {
})
}

function parseVueSfc (filePath: string, source?: string) : Context {
if (!source || source.length === 0) {
source = fs.readFileSync(filePath).toString().split('\r\n').join('\n')
}
const fileInfo = {
path: filePath,
source: source
export function parseVueSfc (fileInfo: FileInfo) : VueSFCContext {
if (!fileInfo.source || fileInfo.source.length === 0) {
fileInfo.source = fs.readFileSync(fileInfo.path).toString().split('\r\n').join('\n')
}
const astParseResult = vueSfcAstParser(fileInfo)
const context : Context = {
path: filePath,
source: source,
const context : VueSFCContext = {
path: fileInfo.path,
source: fileInfo.source,
templateAST: astParseResult.templateAST,
scriptAST: astParseResult.scriptAST,
jscodeshiftParser: astParseResult.jscodeshiftParser,
Expand Down
5 changes: 3 additions & 2 deletions src/ast-parse/transformations/addJsxTransformation.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ASTTransformation } from './index'
import { Context } from './index'
import { stringifyDescriptor } from '@originjs/vue-sfc-ast-parser'
import { FileInfo, VueSFCContext, parseVueSfc } from '../astParse';

export const transformAST:ASTTransformation = (context: Context) => {
export const transformAST:ASTTransformation = (fileInfo: FileInfo) => {
const context: VueSFCContext = parseVueSfc(fileInfo)
if (!context.scriptAST || context.scriptAST.findJSXElements().length === 0) {
return null;
}
Expand Down
15 changes: 2 additions & 13 deletions src/ast-parse/transformations/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
// import { ESLintProgram } from 'vue-eslint-parser/ast/nodes';
import { JSCodeshift } from 'jscodeshift/src/core';
import { SFCDescriptor } from '@originjs/vue-sfc-ast-parser'

export type Context = {
path: string
source: string
// templateAST: ESLintProgram,
templateAST: any,
scriptAST: any,
jscodeshiftParser: JSCodeshift,
descriptor: SFCDescriptor
}
import { FileInfo } from '../astParse';

export type ASTTransformation<Params = void> = {
(context: Context, params: Params): string | null
(fileInfo: FileInfo, params: Params): string | null
}

export const transformationMap: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import type { ASTTransformation } from './index'
import { Context } from './index'
import { stringifyDescriptor } from '@originjs/vue-sfc-ast-parser'
import { FileInfo, parseVueSfc, VueSFCContext } from '../astParse';

export const transformAST:ASTTransformation = (context: Context) => {
export const transformAST:ASTTransformation = (fileInfo: FileInfo) => {
const context: VueSFCContext = parseVueSfc(fileInfo)
if (!context.descriptor.template || !context.descriptor.template.attrs!.lang) {
return null;
}
Expand Down

0 comments on commit 220fed5

Please sign in to comment.