Skip to content

Commit

Permalink
feat: add extensions filter for ast transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterAlfredLee authored and ygj6 committed Jul 2, 2021
1 parent 83d9494 commit a8a7380
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/ast-parse/astParse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { vueSfcAstParser } from '@originjs/vue-sfc-ast-parser'
import * as globby from 'globby'
import fs from 'fs'

const extensions = ['.vue']

export function astParseRoot (rootDir: string) {
const resolvedPaths : string[] = globby.sync(rootDir)
resolvedPaths.forEach(filePath => {
Expand All @@ -14,9 +12,6 @@ export function astParseRoot (rootDir: string) {
}

const extension = (/\.([^.]*)$/.exec(filePath) || [])[0]
if (!extensions.includes(extension)) {
return
}

let fileChanged: boolean = false
let context = parseVueSfc(filePath)
Expand All @@ -26,6 +21,14 @@ export function astParseRoot (rootDir: string) {
// iter all transformations
for (const key in transformationMap) {
const transformation = transformationMap[key]

// filter by file extension
const extensions: string[] = transformation.extensions
if (!extensions.includes(extension)) {
continue
}

// execute the transformation
tempTransformationResult = transformation.transformAST(context)
if (tempTransformationResult == null) {
continue
Expand Down
4 changes: 3 additions & 1 deletion src/ast-parse/transformations/addJsxTransformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ export const transformAST:ASTTransformation = (context: Context) => {
return stringifyDescriptor(descriptor);
}

export const needReparse : boolean = false
export const needReparse: boolean = false

export const extensions: string[] = ['vue']
3 changes: 2 additions & 1 deletion src/ast-parse/transformations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export type ASTTransformation<Params = void> = {
export const transformationMap: {
[name: string]: {
transformAST: ASTTransformation,
needReparse: boolean
needReparse: boolean,
extensions: string[]
}
} = {
addJsxTransformation: require('./addJsxTransformation'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ export const transformAST:ASTTransformation = (context: Context) => {
}

export const needReparse : boolean = false

export const extensions: string[] = ['vue']

0 comments on commit a8a7380

Please sign in to comment.