Skip to content

Commit

Permalink
Merge pull request #465 from fahasikei/release-3.6.8-app
Browse files Browse the repository at this point in the history
feat: 新增export from语法支持及测试用例
  • Loading branch information
Vector-Hope authored Dec 5, 2023
2 parents e9cf89b + 1134163 commit 8f6de88
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 2 deletions.
21 changes: 19 additions & 2 deletions packages/taro-cli-convertor/__tests__/__mocks__/fs-extra.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function isDir (path) {
}

/**
* 获取状态
* 获取文件或目录状态,在处理路径为符号链接时返回链接指向的文件或目录的状态
*/
function statSyncMock (path) {
if (typeof path !== 'string' || path === '') {
Expand All @@ -243,14 +243,30 @@ function statSyncMock (path) {
}
}

/**
* 获取文件或目录状态,在处理路径为符号链接时返回链接自身的文件或目录的状态
*/
function lstatSyncMock (path) {
if (typeof path !== 'string' || path === '') {
return
}

path = normalizePath(path)
// 返回包含状态信息的对象
return {
isFile: () => customIsFile(path),
isDirectory: () => customIsDirectory(path)
}
}

// 自定义的 isFile 函数
function customIsFile (path) {
return oriFileMap.get(path)
}

// 自定义的 isDirectory 函数
function customIsDirectory (path) {
if (typeof path !== 'string' || path === '') {
if (typeof path === 'string' && path.includes('.') && path.indexOf('.') !== 0) {
return false
}
return true
Expand All @@ -277,6 +293,7 @@ module.exports = {
writeFileSync: jest.fn((path, data) => writeFileSyncMock(path, data)),
copySync: jest.fn((from, to) => copySyncMock(from, to)),
statSync: jest.fn((path) => statSyncMock(path)),
lstatSync: jest.fn((path) => lstatSyncMock(path)),
}

module.exports.setMockFiles = setMockFiles
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class _C extends React.Component {
export default _C;
`;

exports[`语法转换 支持export from语法,情况一:部分导入 1`] = `export { var1, func1 } from "./tools1.js";`;

exports[`语法转换 支持export from语法,情况二:全部导入 1`] = `export * from "./tools1.js";`;

exports[`语法转换 模版的动态名称转换 1`] = `
import withWeapp, { cacheOptions } from "@tarojs/with-weapp";
import { Block, View } from "@tarojs/components";
Expand Down
98 changes: 98 additions & 0 deletions packages/taro-cli-convertor/__tests__/script.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { fs } from '@tarojs/helper'
import * as taroize from '@tarojs/taroize'
import wxTransformer from '@tarojs/transformer-wx'

import Convertor from '../src/index'
import { copyFileToTaro } from '../src/util'
Expand Down Expand Up @@ -48,6 +49,11 @@ describe('语法转换', () => {
convert = new Convertor('', false)
})

// 还原模拟函数
afterEach(() => {
jest.restoreAllMocks()
})

test('使用新建的setData替换组件中this.data.xx,实现this.data.xx的转换', () => {
param.script = 'this.data.intData = 1024'
const taroizeResult = taroize({
Expand Down Expand Up @@ -102,6 +108,88 @@ describe('语法转换', () => {
)
expect(css).toBe('background-image: url("data:image/png;base64,TB0pX/TB0PX/TB0rpX/TB0RPX");')
})

test('支持export from语法,情况一:部分导入', () => {
const wxScriptFile = {
'tools1.js': `
{
export const var1 = 'Hello';
export function func1() {
return 'This is function 1';
}
}`,
'tools2.js': `
{
export { var1, func1 } from './tools1'
}`,
}
updateMockFiles(root, wxScriptFile)

const code = `
export { var1, func1 } from './tools1'
`
const file = path.join(root, 'tools2.js')
const outputFilePath = ''

const transformResult = wxTransformer({
code,
sourcePath: file,
isNormal: true,
isTyped: true,
logFilePath: '',
})

const { ast, scriptFiles } = convert.parseAst({
ast: transformResult.ast,
outputFilePath,
sourceFilePath: file,
})

const jsCode = generateMinimalEscapeCode(ast)
expect(jsCode).toMatchSnapshot()
expect(scriptFiles.size).toBe(1)
})

test('支持export from语法,情况二:全部导入', () => {
const wxScriptFile = {
'tools1.js': `
{
export const var1 = 'Hello';
export function func1() {
return 'This is function 1';
}
}`,
'tools2.js': `
{
export * from './tools1'
}`,
}
updateMockFiles(root, wxScriptFile)

const code = `
export * from './tools1'
`
const file = path.join(root, 'tools2.js')
const outputFilePath = ''

const transformResult = wxTransformer({
code,
sourcePath: file,
isNormal: true,
isTyped: true,
logFilePath: '',
})

const { ast, scriptFiles } = convert.parseAst({
ast: transformResult.ast,
outputFilePath,
sourceFilePath: file,
})

const jsCode = generateMinimalEscapeCode(ast)
expect(jsCode).toMatchSnapshot()
expect(scriptFiles.size).toBe(1)
})
})

describe('文件转换', () => {
Expand All @@ -116,6 +204,11 @@ describe('文件转换', () => {
clearMockFiles()
})

// 还原模拟函数
afterEach(() => {
jest.restoreAllMocks()
})

test('拷贝tsconfig.json文件到转换后的工程', () => {
const tsConfigFile = {
'tsconfig.json': `
Expand Down Expand Up @@ -183,6 +276,11 @@ describe('page页面转换', () => {
convert.pages = Convertor.prototype.pages
})

// 还原模拟函数
afterEach(() => {
jest.restoreAllMocks()
})

test('template组件名转换', () => {
param.wxml = `
<!-- template的name值为全小写 -->
Expand Down
32 changes: 32 additions & 0 deletions packages/taro-cli-convertor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,38 @@ export default class Convertor {
scriptImports.push(importName)
})
},
// export全部导入写法
ExportAllDeclaration (astPath) {
const node = astPath.node
const source = node.source
const value = source.value
analyzeImportUrl(
self.root,
sourceFilePath,
scriptFiles,
source,
value,
self.isTsProject,
self.pluginInfo.pluginName
)
},
// export部分导入写法
ExportNamedDeclaration (astPath) {
const node = astPath.node
const source = node.source || ''
if (source) {
const value = source.value
analyzeImportUrl(
self.root,
sourceFilePath,
scriptFiles,
source,
value,
self.isTsProject,
self.pluginInfo.pluginName
)
}
},
CallExpression (astPath) {
printToLogFile(`package: taro-cli-convertor, 解析CallExpression: ${astPath} ${getLineBreak()}`)
const node = astPath.node
Expand Down

0 comments on commit 8f6de88

Please sign in to comment.