Skip to content

Commit

Permalink
feat: refactor vue sfc
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterAlfredLee committed Jul 10, 2021
1 parent c4b4058 commit a0e0a18
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
19 changes: 1 addition & 18 deletions src/ast-parse/astParse.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { transformationMap, TransformationType } from './transformations/index'
import { parsersMap, ParserType } from './parsers/index'
import { SFCDescriptor, vueSfcAstParser } from '@originjs/vue-sfc-ast-parser'
import { SFCDescriptor } from '@originjs/vue-sfc-ast-parser'
import * as globby from 'globby'
import fs from 'fs'
import { JSCodeshift } from 'jscodeshift/src/core';
Expand Down Expand Up @@ -122,20 +122,3 @@ export function astParseRoot (rootDir: string): AstParsingResult {
transformationResult: transformationResults
}
}

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 : VueSFCContext = {
path: fileInfo.path,
source: fileInfo.source,
templateAST: astParseResult.templateAST,
scriptAST: astParseResult.scriptAST,
jscodeshiftParser: astParseResult.jscodeshiftParser,
descriptor: astParseResult.descriptor
}

return context
}
3 changes: 2 additions & 1 deletion src/ast-parse/parsers/findJsxInScriptParser.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ASTParse, ParserType } from './index';
import { FileInfo, parseVueSfc, ParsingResultOccurrence, VueSFCContext } from '../astParse';
import { FileInfo, ParsingResultOccurrence, VueSFCContext } from '../astParse';
import { parseVueSfc } from '../../utils/astUtils'

export const astParse: ASTParse = (fileInfo: FileInfo) => {
const context: VueSFCContext = parseVueSfc(fileInfo)
Expand Down
3 changes: 2 additions & 1 deletion src/ast-parse/transformations/addJsxTransformation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ASTTransformation } from './index'
import { TransformationType } from './index'
import { stringifyDescriptor } from '@originjs/vue-sfc-ast-parser'
import { FileInfo, VueSFCContext, parseVueSfc, TransformationResult } from '../astParse';
import { FileInfo, VueSFCContext, TransformationResult } from '../astParse';
import { parseVueSfc } from '../../utils/astUtils'

export const astTransform:ASTTransformation = (fileInfo: FileInfo) => {
const context: VueSFCContext = parseVueSfc(fileInfo)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ASTTransformation } from './index'
import { TransformationType } from './index'
import { stringifyDescriptor } from '@originjs/vue-sfc-ast-parser'
import { FileInfo, parseVueSfc, TransformationResult, VueSFCContext } from '../astParse';
import { FileInfo, TransformationResult, VueSFCContext } from '../astParse';
import { parseVueSfc } from '../../utils/astUtils'

export const astTransform:ASTTransformation = (fileInfo: FileInfo) => {
const context: VueSFCContext = parseVueSfc(fileInfo)
Expand Down
20 changes: 20 additions & 0 deletions src/utils/astUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'fs';
import { vueSfcAstParser } from '@originjs/vue-sfc-ast-parser';
import { FileInfo, VueSFCContext } from '../ast-parse/astParse';

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 : VueSFCContext = {
path: fileInfo.path,
source: fileInfo.source,
templateAST: astParseResult.templateAST,
scriptAST: astParseResult.scriptAST,
jscodeshiftParser: astParseResult.jscodeshiftParser,
descriptor: astParseResult.descriptor
}

return context
}

0 comments on commit a0e0a18

Please sign in to comment.