-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 1 commit
4ecac1f
d55b301
11fe182
d28a27d
1fb9eea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import * as taroize from '@tarojs/taroize' | ||
import Convertor from '../src/index' | ||
import { copyFileToTaro, transRelToAbsPath, getMatchUnconvertDir } from '../src/util' | ||
|
||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
describe('parseAst', () => { | ||
// this.data.xx = XXX转为setData问题 | ||
test('this.data.xx = XXX转为setData问题', () => { | ||
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() | ||
}) | ||
|
||
// taroConvert工程对比Taro-cli里的模板工程,缺少文件问题 | ||
test('taroConvert工程对比Taro-cli里的模板工程,缺少文件问题', () => { | ||
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) | ||
} | ||
} | ||
}) | ||
|
||
// 适配convert.config.json,对符合json内路径的文件,在其被导入时不做转换 | ||
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/'] } | ||
let convertConfig = { ...convertJson } | ||
convertConfig.external = transRelToAbsPath(convertJsonPath, convertConfig.external) | ||
// 处理不转换的目录,可在convert.config.json中external字段配置 | ||
let 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) | ||
} | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,4 +82,18 @@ describe('parseScript', () => { | |
const ast = parseScript(option.script, option.scriptPath, wxml as t.Expression, option.wxses, option.refIds, true) | ||
expect(ast).toMatchSnapshot() | ||
}) | ||
}) | ||
|
||
|
||
describe('postcss-unit-transform', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个测试用例没有覆盖代码 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 已优化该用例,移入[packages/taro-cli-convertor/tests/script.test.ts]文件 |
||
// 个别base64图片显示不全问题 | ||
test('个别base64图片显示不全问题', () => { | ||
let value = 'TB0pX' | ||
value = value.replace(/\b-?(\d+(\.\d+)?)px\b/ig, function () { | ||
return Number(arguments[1]) === 0 ? '0px': parseInt(arguments[1], 10) !== 0? (parseInt(arguments[1], 10) * 2) + 'px': '1px' | ||
}).replace(/\b-?(\d+(\.\d+)?)rpx\b/ig, function () { | ||
return arguments[1] + 'px' | ||
}) | ||
expect(value).toBe('TB0pX') | ||
}) | ||
}) |
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 | ||
|
@@ -59,6 +59,24 @@ describe('wxml.ts测试', () => { | |
expect(wxses).toMatchSnapshot() | ||
expect(imports).toMatchSnapshot() | ||
}) | ||
|
||
// mode=""转换成mode | ||
test('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() | ||
}) | ||
|
||
// wx:key="index"转换成key="index" | ||
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', () => { | ||
|
@@ -359,4 +377,26 @@ describe('style属性的解析', () => { | |
expect(content).toBe('("")') | ||
} | ||
}) | ||
|
||
// px/rpx转换为rem | ||
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;') | ||
const styleParseReslut = parseStyle('style', contentInput) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 下面的解析的判断是不是可以优化 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 好的,已优化 |
||
if (t.isJSXAttribute(styleParseReslut)) { | ||
expect(styleParseReslut.type).toEqual('JSXAttribute') | ||
} | ||
}) | ||
|
||
// 0px/rpx转换为0rempx/rpx | ||
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>`) | ||
const styleParseReslut = parseStyle('style', contentInput) | ||
if (t.isJSXAttribute(styleParseReslut)) { | ||
expect(styleParseReslut.type).toEqual('JSXAttribute') | ||
} | ||
}) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
写问题场景,不要写问题
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已将描述修改为问题场景