Skip to content

Commit

Permalink
refactor(transformer): 快应用自定义组件 props 声明改成下划线风格
Browse files Browse the repository at this point in the history
  • Loading branch information
yuche committed Jul 2, 2019
1 parent fa7c227 commit 3e4fc53
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions packages/taro-transformer-wx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from './constant'
import { Adapters, setAdapter, Adapter } from './adapter'
import { Options, setTransformOptions, buildBabelTransformOptions } from './options'
import { get as safeGet, cloneDeep } from 'lodash'
import { get as safeGet, cloneDeep, snakeCase } from 'lodash'
import { isTestEnv } from './env'

const template = require('babel-template')
Expand Down Expand Up @@ -615,10 +615,10 @@ export default function transform (options: Options): TransformResult {
for (const prop of props) {
if (t.isObjectProperty(prop)) {
if (t.isStringLiteral(prop.key) && /[A-Z]/.test(prop.key.value)) {
prop.key = t.stringLiteral(prop.key.value.toLowerCase())
prop.key = t.stringLiteral(snakeCase(prop.key.value))
}
if (t.isIdentifier(prop.key) && /[A-Z]/.test(prop.key.name)) {
prop.key = t.identifier(prop.key.name.toLowerCase())
prop.key = t.identifier(snakeCase(prop.key.name))
}
}
}
Expand All @@ -634,10 +634,10 @@ export default function transform (options: Options): TransformResult {
for (const prop of props) {
if (t.isObjectProperty(prop)) {
if (t.isStringLiteral(prop.key) && /[A-Z]/.test(prop.key.value)) {
prop.key = t.stringLiteral(prop.key.value.toLowerCase())
prop.key = t.stringLiteral(snakeCase(prop.key.value))
}
if (t.isIdentifier(prop.key) && /[A-Z]/.test(prop.key.name)) {
prop.key = t.identifier(prop.key.name.toLowerCase())
prop.key = t.identifier(snakeCase(prop.key.name))
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/taro-transformer-wx/src/jsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { createHTMLElement } from './create-html-element'
import { codeFrameError, decodeUnicode } from './utils'
import { Adapter, Adapters, isNewPropsSystem } from './adapter'
import { Status } from './functional'
import { snakeCase } from 'lodash'

export function isStartWithWX (str: string) {
return str[0] === 'w' && str[1] === 'x'
Expand Down Expand Up @@ -214,7 +215,7 @@ export function parseJSXElement (element: t.JSXElement, isFirstEmit = false): st
}
}
if (Adapters.quickapp === Adapter.type && !DEFAULT_Component_SET_COPY.has(componentName) && typeof name === 'string' && !/(^on[A-Z_])|(^catch[A-Z_])/.test(name)) {
name = name.toLowerCase()
name = snakeCase(name)
}
let value: string | boolean = true
let attrValue = attr.value
Expand Down
4 changes: 2 additions & 2 deletions packages/taro-transformer-wx/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
createRandomLetters,
isDerivedFromProps
} from './utils'
import { difference, get as safeGet, cloneDeep, uniq } from 'lodash'
import { difference, get as safeGet, cloneDeep, uniq, snakeCase } from 'lodash'
import {
setJSXAttr,
buildBlockElement,
Expand Down Expand Up @@ -358,7 +358,7 @@ export class RenderParser {
}
const properties: t.ObjectProperty[] = []
this.componentProperies.forEach((propName) => {
const p = Adapters.quickapp === Adapters.quickapp && this.upperCaseComponentProps.has(propName) ? propName.toLowerCase() : propName
const p = Adapters.quickapp === Adapters.quickapp && this.upperCaseComponentProps.has(propName) ? snakeCase(propName): propName
properties.push(
t.objectProperty(t.stringLiteral(p), t.objectExpression([
t.objectProperty(t.stringLiteral('type'), t.nullLiteral()),
Expand Down

0 comments on commit 3e4fc53

Please sign in to comment.