From 220fed5614dbb35c5ad06d816bd5b9defb4a02ae Mon Sep 17 00:00:00 2001 From: PeterAlfredLee Date: Fri, 2 Jul 2021 15:59:41 +0800 Subject: [PATCH] feat: refactor the ast parse --- src/ast-parse/astParse.ts | 50 ++++++++++++------- .../transformations/addJsxTransformation.ts | 5 +- src/ast-parse/transformations/index.ts | 15 +----- .../removeHtmlLangInTemplateTransformation.ts | 5 +- 4 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/ast-parse/astParse.ts b/src/ast-parse/astParse.ts index 41d9f2b..bcd672f 100644 --- a/src/ast-parse/astParse.ts +++ b/src/ast-parse/astParse.ts @@ -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) @@ -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) { @@ -29,7 +49,7 @@ export function astParseRoot (rootDir: string) { } // execute the transformation - tempTransformationResult = transformation.transformAST(context) + tempTransformationResult = transformation.transformAST(fileInfo) if (tempTransformationResult == null) { continue } @@ -37,7 +57,7 @@ export function astParseRoot (rootDir: string) { transformationResult = tempTransformationResult if (transformation.needReparse) { - context = parseVueSfc(filePath, transformationResult) + fileInfo.source = transformationResult } } if (fileChanged) { @@ -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, diff --git a/src/ast-parse/transformations/addJsxTransformation.ts b/src/ast-parse/transformations/addJsxTransformation.ts index 9304d99..0e5f8d8 100644 --- a/src/ast-parse/transformations/addJsxTransformation.ts +++ b/src/ast-parse/transformations/addJsxTransformation.ts @@ -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; } diff --git a/src/ast-parse/transformations/index.ts b/src/ast-parse/transformations/index.ts index c03fc5b..b1914f6 100644 --- a/src/ast-parse/transformations/index.ts +++ b/src/ast-parse/transformations/index.ts @@ -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 = { - (context: Context, params: Params): string | null + (fileInfo: FileInfo, params: Params): string | null } export const transformationMap: { diff --git a/src/ast-parse/transformations/removeHtmlLangInTemplateTransformation.ts b/src/ast-parse/transformations/removeHtmlLangInTemplateTransformation.ts index 8babfcd..f88dbb7 100644 --- a/src/ast-parse/transformations/removeHtmlLangInTemplateTransformation.ts +++ b/src/ast-parse/transformations/removeHtmlLangInTemplateTransformation.ts @@ -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; }