Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 新增白名单文件夹、配置文件平移,组件的动态名称、wx:key、base64、px/rpx、mode、this.data.xxx转… #345

Merged
Merged
129 changes: 129 additions & 0 deletions packages/taro-cli-convertor/__tests__/script.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import * as taroize from '@tarojs/taroize'

import Convertor from '../src/index'
import { copyFileToTaro, getMatchUnconvertDir, transRelToAbsPath } from '../src/util'

const fs = require('fs')
const path = require('path')

describe('语法转换', () => {
test('使用新建的setData替换组件中this.data.xx,实现this.data.xx的转换', () => {
const entryJSON = { 'pages': ['pages/index/index'] }
// json:index.json的内容 path:index的根目录(文件路径) rootPath:小程序的根目录(文件路径)
// script:index.js的内容 scriptPath:index.js的绝对路径 wxml:index.html的内容
const param = {
json:'{}',
path:'',
rootPath:'',
script:'this.data.intData = 1024',
scriptPath:'',
wxml:''
}
// new Convertot后会直接执行 init(),为确保 init() 在测试中通过采用 spyOn 去模拟
jest.spyOn(Convertor.prototype, 'getApp').mockImplementation(() => {
Convertor.prototype.entryJSON = entryJSON
})
jest.spyOn(Convertor.prototype, 'getPages').mockImplementation(() => {
Convertor.prototype.pages = new Set(entryJSON.pages)
})
const convert = new Convertor('', false)
convert.pages = Convertor.prototype.pages
const taroizeResult = taroize({
...param,
framework:'react'
})
// sourceFilePath:需要转换的文件路径 outputFilePath:转换输出路径 importStylePath:style的文件路径
const { ast } = convert.parseAst({
ast:taroizeResult.ast,
sourceFilePath:'',
outputFilePath:'',
importStylePath:'',
depComponents: new Set(),
imports:[]
})
expect(ast).toMatchSnapshot()
})

test('组件的动态名称转换', () => {
const entryJSON = { 'pages': ['pages/index/index'] }
// json:index.json的内容 path:index的根目录(文件路径) rootPath:小程序的根目录(文件路径)
// script:index.js的内容 scriptPath:index.js的绝对路径 wxml:index.html的内容
const param = {
json:'{}',
path:'',
rootPath:'',
script:'',
scriptPath:'',
wxml:`<view wx:for="{{infoList}}" wx:key="infoId">
<template is="info-{{item.tempName}}" data="{{item}}"></template>
</view>`
}
// new Convertot后会直接执行 init(),为确保 init() 在测试中通过采用 spyOn 去模拟
jest.spyOn(Convertor.prototype, 'getApp').mockImplementation(() => {
Convertor.prototype.entryJSON = entryJSON
})
jest.spyOn(Convertor.prototype, 'getPages').mockImplementation(() => {
Convertor.prototype.pages = new Set(entryJSON.pages)
})
const convert = new Convertor('', false)
convert.pages = Convertor.prototype.pages
const taroizeResult = taroize({
...param,
framework:'react'
})
// sourceFilePath:需要转换的文件路径 outputFilePath:转换输出路径 importStylePath:style的文件路径
const { ast } = convert.parseAst({
ast:taroizeResult.ast,
sourceFilePath:'',
outputFilePath:'',
importStylePath:'',
depComponents: new Set(),
imports:[]
})
expect(ast).toMatchSnapshot()
})

test('css中字母+数字+pX会转换成px', async () => {
const convert = new Convertor('', false)
const { css } = await convert.styleUnitTransform('', 'background-image: url("data:image/png;base64,TB0pX/TB0PX/TB0rpX/TB0RPX");')
expect(css).toBe('background-image: url("data:image/png;base64,TB0pX/TB0PX/TB0rpX/TB0RPX");')
})
})

describe('文件转换', () => {
test('拷贝tsconfig.json文件到转换后的工程', () => {
const convert = new Convertor('', false)
const selfDefinedConfig: any = []
// 目前只有tsconfig.json,还有的话继续加到array里
selfDefinedConfig[0] = `tsconfig${convert.fileTypes.CONFIG}`
for (const tempConfig of selfDefinedConfig) {
const tempConfigPath = path.join(convert.root, tempConfig)
if (fs.existsSync(tempConfig)) {
const outputFilePath = path.join(convert.convertRoot, tempConfig)
copyFileToTaro(tempConfigPath, outputFilePath)
expect(fs.existsSync(outputFilePath)).toBe(true)
}
}
})

test('适配convert.config.json,对符合json内路径的文件,在其被导入时不做转换', () => {
const convert = new Convertor('', false)
const root = transRelToAbsPath('', ['./taro-cli-convertor'])[0].replace(/\\/g, '/')
// 处理convert.config.json,并存储到convertConfig中
const convertJsonPath: string = path.join(root, `convert.config${convert.fileTypes.CONFIG}`)
const convertJson = { 'external': ['./taroConvert/'] }
const convertConfig = { ...convertJson }
convertConfig.external = transRelToAbsPath(convertJsonPath, convertConfig.external)
// 处理不转换的目录,可在convert.config.json中external字段配置
const filePath = transRelToAbsPath(convertJsonPath, ['./taroConvert/tsconfig.json'])
const matchUnconvertDir: string | null = getMatchUnconvertDir(filePath[0], convertConfig?.external)
if (matchUnconvertDir !== null) {
// 支持用户在convert.config.json中配置不转换的目录
const outputFilePath = 'src/taroConvert'
if (!fs.existsSync(outputFilePath)) {
copyFileToTaro(matchUnconvertDir, outputFilePath)
}
expect(fs.existsSync(outputFilePath)).toBe(true)
}
})
})
2 changes: 1 addition & 1 deletion packages/taroize/__tests__/script.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,4 @@ describe('parseScript', () => {
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true)
expect(ast).toMatchSnapshot()
})
})
})
30 changes: 29 additions & 1 deletion packages/taroize/__tests__/wxml.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as t from '@babel/types'

import { convertStyleUnit,parseContent, parseStyle, parseWXML } from '../src/wxml'
import { convertStyleUnit, parseContent, parseStyle, parseWXML } from '../src/wxml'

interface Option {
path: string
Expand Down Expand Up @@ -59,6 +59,22 @@ describe('wxml.ts测试', () => {
expect(wxses).toMatchSnapshot()
expect(imports).toMatchSnapshot()
})

test('wxml中image的mode=""会转换成mode', () => {
option.wxml = `<image class="img" src="{{imgSrc}}" mode=""></image>`
option.path = 'wxml_mode'
const { wxml }: any = parseWXML(option.path, option.wxml)
expect(wxml).toMatchSnapshot()
})

test('wx:key="index"会转换成key="index"', () => {
option.wxml = `<view wx:key="index" wx:for="{{data}}">
<text>{{item.name}}</text>
</view>`
option.path = 'wxml_key'
const { wxml }: any = parseWXML(option.path, option.wxml)
expect(wxml).toMatchSnapshot()
})
})

describe('parseContent', () => {
Expand Down Expand Up @@ -359,4 +375,16 @@ describe('style属性的解析', () => {
expect(content).toBe('("")')
}
})

test('px/rpx单位转换为rem', () => {
let contentInput = 'width: 100px;height: 200rpx;padding: {{padCount}}px;margin: {{marCount}}rpx;'
contentInput = convertStyleUnit(contentInput)
expect(contentInput).toBe('width: 5rem;height: 5rem;padding: {{padCount/20}}rem;margin: {{marCount/40}}rem;')
})

test('0px/rpx转换为0rempx/rpx', () => {
let contentInput = `<swiper-item style="transform: translate(0%, 0px) translateZ(0rpx);"></swiper-item>`
contentInput = convertStyleUnit(contentInput)
expect(contentInput).toBe(`<swiper-item style="transform: translate(0%, 0rem) translateZ(0rem);"></swiper-item>`)
})
})