import * as t from '@babel/types' import parse from '../src' import { convertStyleUnit, IContext, parseContent, parseStyle, parseWXML } from '../src/wxml' import { generateMinimalEscapeCode, removeBackslashesSerializer } from './util' expect.addSnapshotSerializer(removeBackslashesSerializer) const logFileMap = new Map() jest.mock('fs', () => ({ ...jest.requireActual('fs'), // 保留原始的其他函数 appendFile: jest.fn((path, content): any => { logFileMap.set(path, content) }), })) const option: any = { framework: 'react', json: '{}', logFilePath: '', path: '', rootPath: '', script: '', scriptPath: '', wxml: '', isApp: false, } describe('wxml 语法', () => { test('当 wxml 是 undefined', () => { option.wxml = undefined // parseWXML 会先获取缓存,所有每个用例的 path 需要保持其唯一性 option.path = 'wxml_undefined' const { wxml, wxses, imports, refIds } = parseWXML(option.path, option.wxml) expect(wxml?.type).toBe('NullLiteral') expect(wxses).toEqual([]) expect(imports).toEqual([]) expect(Array.from(refIds)).toEqual(Array.from(new Set())) }) test('wxml 简单应用', () => { option.wxml = `Hello Word!` // parseWXML 会先获取缓存,所有每个用例的 path 需要保持其唯一性 option.path = 'wxml__' const { wxml } = parseWXML(option.path, option.wxml) // wxml 转为 ast expect(wxml).toBeTruthy() expect(wxml?.type).toBe('JSXElement') }) test('wxml 数据绑定', () => { option.wxml = `{{data}}` option.path = 'wxml_message' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe('{data}') }) test('组件属性值为字符串', () => { option.wxml = ` ` option.path = 'wxml_component_properties_string' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(``) }) test('组件属性值为变量', () => { option.wxml = ` ` option.path = 'wxml_component_properties_variable' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(``) }) test('属性值为关键字 (需要在双引号之内)', () => { // false 关键字为 boolean option.wxml = `` option.path = 'wxml_keywords' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe('') }) test('属性值为三元运算', () => { option.wxml = `` option.path = 'wxml_ternary_operations' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe('!(flag ? true : false) && Hidden') }) test('渲染数据为字符串和变量拼接', () => { option.wxml = `{{"hello" + name}}` option.path = 'wxml_string_operations' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(`{"hello" + name}`) }) test('列表渲染', () => { option.wxml = ` {{index}}: {{item.message}} ` option.path = 'wxml_wx_for' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) test('wx:key="index"会转换成 key="index"', () => { option.wxml = ` {{item.name}} ` option.path = 'wxml_key' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) test('条件渲染', () => { option.wxml = ` 1 2 3 ` option.path = 'wxml_if' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(`length > 5 ? 1 : length > 2 ? 2 : 3`) }) test('使用 wx:if 替换 wx:show 属性', () => { option.wxml = ` 测试使用 wx:if 替换 wx:show 属性 ` option.path = 'wxml_show' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) test('wxml 中 class 不支持数组写法', () => { option.wxml = `` option.path = 'wxml_unsupported_classArray' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) }) describe('slot 插槽', () => { test('匿名插槽', () => { /** * 插槽模板 */ option.wxml = ` 这里是组件的内部节点 ` option.path = `anonymity_slot` const { wxml: slotWxml }: any = parseWXML(option.path, option.wxml) const slotWxmlCode = generateMinimalEscapeCode(slotWxml) expect(slotWxmlCode).toBe(`这里是组件的内部节点{this.props.children}`) /** * wxml 页面内容 * 检测插槽组件是否引入需在 taro-cli-convertor 中 */ option.wxml = ` 插入 slot 的内容 ` option.path = 'anonymity_wxml_slot' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe('插入 slot 的内容') }) test('具名插槽', () => { /** * 插槽模板 */ option.wxml = ` 这里是组件的内部细节 ` option.path = 'named_slot' const { wxml: slotWxml }: any = parseWXML(option.path, option.wxml) const slotWxmlCode = generateMinimalEscapeCode(slotWxml) expect(slotWxmlCode).toBe( `{this.props.renderBefore}这里是组件的内部细节{this.props.renderAfter}` ) /** * wxml 页面内容 * 检测插槽组件是否引入需在 taro-cli-convertor 中 */ option.wxml = ` 这里是插入到组件 slot name="before"中的内容 这里是插入到组件 slot name="after"中的内容 ` option.path = 'named_wxml_slot' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe( `这里是插入到组件 slot name="before"中的内容} renderAfter={这里是插入到组件 slot name="after"中的内容}>` ) }) test('当元素设置 slot 属性且值为空串时,移除 slot 属性', () => { option.wxml = ` 这里是插入到组件 slot name="before"中的内容 这里是插入到组件 slot name="after"中的内容 ` option.path = 'component_slot_empty' const { wxml: slotWxml }: any = parseWXML(option.path, option.wxml) const slotWxmlCode = generateMinimalEscapeCode(slotWxml) expect(slotWxmlCode).toMatchSnapshot() }) test('slot 的值为非字符串', () => { option.wxml = ` 这里是组件的内部细节 ` option.path = 'slot_name_no_string' expect(() => parseWXML(option.path, option.wxml)).toThrowError() }) }) describe('wxs', () => { /** * 关于外部 wxs 文件的转换在 taro-cli-convertor 包中 */ test('在页面中引入外部 wxs,转换情况', () => { /** const wxs = ` var foo = "'hello world' from comm.wxs"; var bar = function(d) { return d; } module.exports = { foo: foo, bar: bar }; ` */ option.wxml = ` {{wxs_test.foo}} {{wxs_test.bar('nihao')}} ` option.path = 'wxs_src' option.script = 'Page({})' const { ast }: any = parse(option) const code = generateMinimalEscapeCode(ast) expect(code).toMatchSnapshot() }) test('页面中使用 wxs', () => { option.wxml = ` module.exports = { data: 'wxs demo' } Hello Word! {{wxs_demo.data}} ` // parseWXML 会先获取缓存,所有每个用例的 path 需要保持其唯一性 option.path = 'wxml_wxs' const { wxml, wxses, imports }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(wxmlCode).toBe('Hello Word!{wxs_demo.data}') expect(wxses[0]).toEqual({ module: 'wxs_demo', src: './wxs__wxs_demo' }) expect(importsCode).toMatchSnapshot() }) test('wxs 中 单行注释', () => { option.wxml = ` module.exports = { foo: "'hello world' from tools.wxs", // msg:'123' }; {{wxs_test.foo}} ` option.path = 'wxs_line_comments' const { imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(importsCode).toMatchSnapshot() }) test('wxs 多行注释', () => { option.wxml = ` module.exports = { foo: "'hello world' from tools.wxs", /* * msg:'123', * name:'xixi' **/ }; {{wxs_test.foo}} ` option.path = 'wxs_multiple_comments' const { imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(importsCode).toMatchSnapshot() }) test('wxs 结尾注释', () => { option.wxml = ` module.exports = { foo: "'hello world' from tools.wxs", }; /* var = msg:'123', var = name:'xixi' {{wxs_test.foo}} ` option.path = 'wxs_end_comments' expect(() => parseWXML(option.path, option.wxml)).toThrowError() }) test('wxs 标签的属性值为空', () => { option.wxml = `` option.path = 'wxs_empty' expect(() => parseWXML(option.path, option.wxml)).toThrowError('wxs 标签的属性值不得为空') }) test('wxs 没有 src 属性且内部无代码', () => { option.wxml = `` option.path = 'wxs_no_code' expect(() => parseWXML(option.path, option.wxml)).toThrowError('wxs 如果没有 src 属性,标签内部必须有 wxs 代码。') }) test('wxs 模块中的 var regexp = getRegExp() 转换为 var regexp = new RegExp()', () => { option.wxml = ` var regexp = getRegExp() ` option.path = 'wxml_wxs_regexp' const { wxses, imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(wxses[0]).toEqual({ module: 'wxs_regexp', src: './wxs__wxs_regexp' }) expect(importsCode).toBe('var regexp = new RegExp();') }) test('getRegExp() 只有一个参数', () => { // 参数为变量 option.wxml = ` module.exports = { regFun: function(date){ return getRegExp(date) } } {{wxs_getRegExp.regFun('x')}} ` option.path = 'wxs_getRegExp_argument_variable' expect(() => parseWXML(option.path, option.wxml)).toThrowError('getRegExp 函数暂不支持传入变量类型的参数') // 参数为非字符串 option.wxml = ` module.exports = { reg : getRegExp(123) } {{wxs_getRegExp.reg}} ` option.path = 'wxs_getRegExp_argument_no_string' expect(() => parseWXML(option.path, option.wxml)).toThrowError('getRegExp 函数暂不支持传入非字符串类型的参数') }) test('getRegExp() 有两个参数', () => { // 参数为变量 option.wxml = ` module.exports = { regFun: function(date,flag){ return getRegExp(date,flag) } } {{wxs_getRegExp.regFun('x','img')}} ` option.path = 'wxs_getRegExp_arguments_variable' expect(() => parseWXML(option.path, option.wxml)).toThrowError('getRegExp 函数暂不支持传入变量类型的参数') // 参数为非字符串 option.wxml = ` module.exports = { reg : getRegExp(123,'img') } {{wxs_getRegExp.reg}} ` option.path = 'wxs_getRegExp_arguments_no_string' expect(() => parseWXML(option.path, option.wxml)).toThrowError('getRegExp 函数暂不支持传入非字符串类型的参数') }) test('wxs 标签中的 getDate() 转换为 new Date()', () => { option.wxml = ` module.exports = { date1:getDate(), //参数为数字 date2:getDate(1500000000000), //参数为字符串 date3:getDate('2017-7-14') } ` option.path = 'wxml_wxs_getDate' const { wxses, imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(wxses[0]).toEqual({ module: 'wxs_getDate', src: './wxs__wxs_getDate' }) expect(importsCode).toMatchSnapshot() }) }) describe('解析 wxs 中创建正则表达式方法的转换', () => { test('定义了正则表达式的修饰符,则使用自定义修饰符', () => { option.wxml = ` var a = getRegExp('jzy123','img') module.exports = { reg: a } ` option.path = 'wxml_wxs_reg1' const { wxses, imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(wxses[0]).toEqual({ module: 'xxxfile', src: './wxs__xxxfile' }) expect(importsCode).toMatchSnapshot() }) test('没有定义了正则表达式的修饰符,就不添加修饰符使用默认', () => { option.wxml = ` var a = getRegExp('jzy123') module.exports = { reg: a } ` option.path = 'wxml_wxs_reg2' const { wxses, imports }: any = parseWXML(option.path, option.wxml) const importsCode = generateMinimalEscapeCode(imports[0].ast) expect(wxses[0]).toEqual({ module: 'xxxfile', src: './wxs__xxxfile' }) expect(importsCode).toMatchSnapshot() }) }) describe('组件', () => { test('wxml 中 image 的 mode=""会转换成 mode', () => { option.wxml = `` option.path = 'wxml_mode' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(``) }) test('组件属性 data-XXX,XXX 转为小写', () => { option.wxml = `组件属性 data 标识` option.path = 'data_id' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(`组件属性 data 标识`) }) }) describe('parseContent', () => { test('节点 key 的值解析', () => { const contentInput = '{{list}}' const { type, content } = parseContent(contentInput) expect(type).toBe('expression') expect(content).toBe(`(list)`) }) }) describe('style 属性的解析', () => { // 判断是否为 style 属性 test('判断是否为 style 属性', () => { option.wxml = '123' option.path = 'style_isorno' expect(() => parseWXML(option.path, option.wxml)).toThrow() expect(logFileMap).toMatchSnapshot() }) // style 属性的解析 test('style 属性值为变量', () => { option.wxml = '' option.path = 'style_is_experssion' const { wxml }:any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) // 第一种以 style="xxx:xxx"的用法 test('style = xxx:xxx', () => { let contentInput = 'color: red;background-color: aqua;' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(color: red;background-color: aqua;)') } }) test('style = xxx:{{ xxx }}', () => { let contentInput = 'color: red;background-color: aqua;font-size: {{ fontSize }};' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(color: red;background-color: aqua;font-size: {{ fontSize }};)') } }) test('style = xxx:{{ xxx }}px', () => { let contentInput = 'height: {{ height }}px;width: 100px;background-color: red;' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(height: {{ height }}px;width: 5rem;background-color: red;)') } }) test('style = xxx:{{ xxx }}, 含有三元运算符', () => { let contentInput = 'height: {{ list.length > 10 ? 200 : 100 }}px;width: 100px;background-color: green;' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(height: {{ list.length > 10 ? 200 : 100 }}px;width: 100px;background-color: green);') } }) test('style = xxx:{{ xxx + 10 }}px', () => { let contentInput = 'height: {{ height + 10 }}px;width: 100px;background-color: red;' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('({{ height + 10 }}px;width: 5rem;background-color: red;)') } }) test('style = xxx:{{ xxx + 10 }}rpx', () => { const contentInput = 'style 参数为变量' const contentOut = convertStyleUnit(contentInput) expect(contentOut).toBe(`style 参数为变量`) }) // style=xxx:xxx 情况中属性值中的 value 既有变量也有变量拼字符串 test('style = xxx:xxx 变量与变量拼接混用', () => { let contentInput = 'height: {{ height }}px;width: {{ width }}px;background-color: {{ color }};' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(height: {{ height }}px;width: {{ width }}px;background-color: {{ color }})') } }) // style=xxx:xxx 情况中属性值中的 value 既有变量拼接也有三元表达式 test('style = xxx:xxx 变量拼接与三元表达式混用', () => { let contentInput = 'height: {{ height }}px;width: {{ width }}px;background-color: {{ false ? color : "blue" }};' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult.type).toEqual('JSXAttribute') } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe( '(height: {{ height }}px;width: {{ width }}px;background-color: {{ false ? color : "blue" }};)' ) } }) // 第二种以双括号用法 test('style = {{ xxx }}变量形式', () => { let contentInput = '{{ myStyle }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(myStyle)') } }) test('style = {{ xxx }}字符串形式', () => { let contentInput = "{{ 'height: 100px;width: 100px;background-color: red;' }}" contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe("('height: 5rem;width: 5rem;background-color: red;')") } }) test('style = {{ xxx }}含三元运算符', () => { let contentInput = "{{ index > 1 ? 'height: 100px;width: 100px;background-color: green;' : '' }}" contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe("(index > 1 ? 'height: 5rem;width: 5rem;background-color: green;' : '')") } }) test('style = {{ xxx }}含三元运算符和变量', () => { let contentInput = '{{ index ? "background-color: " + backgroundColor + ";" : "" }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(index ? "background-color: " + backgroundColor + ";" : "")') } }) // 第三种 xxx:xxx 和{{ xxx }}混用情况 // style 属性值是 xxx:xxx 与{{ xxx }}混用 test('xxx:xxx 和{{ xxx }}混用情况 1', () => { let contentInput = 'background-color: aqua;{{ global_css }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('"background-color: aqua;"+(global_css)') } }) // style 属性值是 xxx:xxx 与{{ 三元表达式 }}混用 test('xxx:xxx 和{{ xxx }}混用情况 2', () => { let contentInput = 'background-color: aqua;{{ true ? global_css : "" }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('"background-color: aqua;"+(true ? global_css : "")') } }) // style 属性值是变量拼接字符串和{{ xxx }}混用 test('xxx:xxx 和{{ xxx }}混用情况 3', () => { let contentInput = 'background-color: {{ color }};{{ global_css }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('"background-color: "+(color)+";"+(global_css)') } }) // style 属性值是三元表达式拼接字符串和{{ xxx }}混用 test('xxx:xxx 和{{ xxx }}混用情况 4', () => { let contentInput = 'background-color: {{ true ? color : "" }};{{ global_css }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('"background-color: "+(true ? color : "")+";"+(global_css)') } }) // style 属性值是多个{{ xxx }} test('xxx:xxx 和{{ xxx }}混用情况 5', () => { let contentInput = '{{ global_css }}{{ myStyle }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(global_css)+(myStyle)') } }) // 第四种情况,虽然也是 xxx:xxx 形式但是 key 是一个变量 test('style = xxx:xxx, 但是 key 是变量', () => { let contentInput = '{{ styleName }}: red;height: 100px;width: 100px;' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') expect(content).toBe('(styleName)+": red;height: 5rem;width: 5rem;"') } }) // style 为空的特殊写法 // style = { xxx }微信不规范写法 test('style 为空的特殊写法 1', () => { let contentInput = '{styleX}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('raw') expect(content).toBe('{styleX}') } }) // style = "", 属性值为空 test('style 为空的特殊写法 2', () => { let contentInput = '""' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('raw') expect(content).toBe('""') } }) // style = {{ }}, 属性值为空 test('style 为空的特殊写法 3', () => { let contentInput = '{{}}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('raw') expect(content).toBe('{{}}') } }) // style = {{ '' }}, 属性值为空 test('style 为空的特殊写法 4', () => { let contentInput = '{{ "" }}' contentInput = convertStyleUnit(contentInput) const styleParseResult = parseStyle('style', contentInput) if (t.isJSXAttribute(styleParseResult as t.Node)) { expect(styleParseResult).toEqual(t.jSXAttribute) } else { const { type, content } = styleParseResult as IContext expect(type).toBe('expression') 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 = `` contentInput = convertStyleUnit(contentInput) expect(contentInput).toBe(``) }) test('绝对值小于 1 的 px/rpx 转换成 rem', () => { let contentInput = `` contentInput = convertStyleUnit(contentInput) expect(contentInput).toBe(``) }) test('style="height: calc(100vh - {{xxx}}rem)",内联样式使用 calc 计算,包含变量,变量前有空格,转换后空格保留', () => { let contentInput = `` contentInput = convertStyleUnit(contentInput) expect(contentInput).toBe('') }) test('当设置 style 属性但未赋值则删除该属性', () => { option.wxml = `style 为空` option.path = 'component_style_empty' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toBe(`style 为空`) }) }) describe('wx 作用域属性解析', () => { test('wx 不支持的属性 scope-data', () => { option.wxml = ` {{id}} ` option.path = 'wx_unknown' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toEqual('{id}') }) test('wx:if', () => { option.wxml = ` {{xxx}} ` option.path = 'wx_if' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toEqual(`xx && {xxx}`) }) test('wx:for', () => { option.wxml = ` {{item}} ` option.path = 'wx_for' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toEqual(`list.map((item, index) => {\n return {item};\n})`) }) test('wx:for & wx:for-xxx', () => { option.wxml = ` {{idx}}: {{itemName.message}} ` option.path = 'wx_for_xxx' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) test('wx:if & wx:for', () => { option.wxml = ` the value is : {{item.value}} ` option.path = 'wx_if_for' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) test('wx:for & wx:if', () => { option.wxml = ` the value is : {{item.value}} ` option.path = 'wx_for_if' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toMatchSnapshot() }) }) describe('hidden 属性解析', () => { test('一般组件的 hidden 属性解析', () => { option.wxml = `` option.path = 'hidden' const { wxml }: any = parseWXML(option.path, option.wxml) const wxmlCode = generateMinimalEscapeCode(wxml) expect(wxmlCode).toEqual(`!xx && {xxx}`) }) test('template 的 hidden 属性解析', () => { option.wxml = `