refactor_options-API-composition-API-correspondence.md 4.7 KB
Newer Older
DCloud-WZF's avatar
DCloud-WZF 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# hello uvue 选项式 API 与 组合式 API 对应重构

## 文件目录逻辑

按 API 类型划分文件夹,每个文件夹下有多个示例或 API\
每个示例或 API 有一个文件夹,文件夹名为示例或 API 名称\
每个文件夹下有 xxx-options.uvue 和 xxx-composition.uvue,分别对应选项式 API 示例和组合式 API 示例
<!-- template 和 style 通过 src 引入,两种 API 示例共用 -->

## 代码规范

不要有空的style, script\
script 中不要有空的 data, onLoad, methods\
DCloud-WZF's avatar
DCloud-WZF 已提交
14 15
缩进使用两个空格\
公共 css class 维护在 styles/common.css 中
DCloud-WZF's avatar
DCloud-WZF 已提交
16

DCloud-WZF's avatar
DCloud-WZF 已提交
17 18 19 20 21 22 23
## GoGoCode 

一个用于进行代码转换的 vscode 插件,在本项目中,可用于将 options API 写法转为 composition setup API 写法

### transform function

目前仅处理了 script 节点
DCloud-WZF's avatar
DCloud-WZF 已提交
24 25 26 27 28
已知问题:
- script 节点无法增加 setup
- 无法处理函数返回值类型
- 无法处理函数换行

DCloud-WZF's avatar
DCloud-WZF 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
```js
function transform(fileInfo, api) {
  const $ = api.gogocode
  const source = fileInfo.source
  const ast = $(source, { parseOptions: { language: 'vue' } })
  const script = ast.find('<script></script>')

  script.replace('components:{},', '').replace('mixins:[]', '')

  script
    .find('data(){return {}},')
    .replace('$_$:$_$', 'const $_$ = ref($_$)')
    .replace('data(){return {$$$}}', '$$$')

  script
    .find('computed:{}')
    .replace('$_$(){$$$}', 'const $_$ = computed(() => {$$$})')

  script
    .find('watch:{}')
DCloud-WZF's avatar
DCloud-WZF 已提交
49 50 51 52
    .replace(
      '$_$:{handler($_$){$$$}}',
      'watch(() => $_$,($_$) => {$$$})'
    )
DCloud-WZF's avatar
DCloud-WZF 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66
    .replace('$_$:{handler(){$$$}}', 'watch(() => $_$,() => {$$$})')
    .replace(
      "'$_$':{handler($_$){$$$},deep: true}",
      'watch(() => $_$,($_$) => {$$$},{deep: true})'
    )
    .replace(
      "'$_$':{handler($_$){$$$}}",
      'watch(() => $_$,($_$) => {$$$})'
    )
    .replace('$_$($_$){$$$}', 'watch(() => $_$,($_$) => {$$$})')
    .replace('$_$(){$$$}', 'watch(() => $_$,() => {$$$})')
    .replace('watch:{$$$}', '$$$')

  script
DCloud-WZF's avatar
DCloud-WZF 已提交
67 68 69 70 71 72
    .replace('onLoad(){$$$}', 'onLoad(() => {$$$})')
    .replace('onShow(){$$$}', 'onShow(() => {$$$})')
    .replace('onReady(){$$$}', 'onReady(() => {$$$})')
    .replace('onHide(){$$$}', 'onHide(() => {$$$})')
    .replace('onUnload(){$$$}', 'onUnload(() => {$$$})')
    .replace('onBackPress(){$$$}', 'onBackPress(() => {$$$})')
DCloud-WZF's avatar
DCloud-WZF 已提交
73 74 75 76 77 78 79 80 81 82
    .replace('created(){$$$}', 'onBeforeMount(() => {$$$})')
    .replace('mounted(){$$$}', 'onMounted(() => {$$$})')
    .replace('beforeUnmount(){$$$}', 'onBeforeUnmount(() => {$$$})')
    .replace('unmounted(){$$$}', 'onUnmounted(() => {$$$})')
    .replace('beforeDestroy(){$$$}', 'onBeforeUnmount(() => {$$$})')
    .replace('destoryed(){$$$}', 'onUnmounted(() => {$$$})')

  script
    .find('methods:{}')
    .replace(
DCloud-WZF's avatar
DCloud-WZF 已提交
83 84
      'async $_$($$$0){$$$1}',
      'const $_$ = async ($$$0) => {$$$1}'
DCloud-WZF's avatar
DCloud-WZF 已提交
85
    )
DCloud-WZF's avatar
DCloud-WZF 已提交
86
    .replace('$_$($$$0){$$$1}', 'const $_$ = ($$$0) => {$$$1}')
DCloud-WZF's avatar
DCloud-WZF 已提交
87 88 89 90 91 92 93 94 95 96
    .replace('async $_$(){$$$}', 'const $_$ = async () => {$$$}')
    .replace('$_$(){$$$}', 'const $_$ = () => {$$$}')
    .replace('methods:{$$$}', '$$$')

  script.replace('export default {$$$}', '$$$')

  return ast.generate()
}
```

DCloud-WZF's avatar
DCloud-WZF 已提交
97 98 99 100
## app instance

- [x] app.component
- [x] app.globalProperties  app.globalProperties
DCloud-WZF's avatar
DCloud-WZF 已提交
101
- [x] app.use               app.use
DCloud-WZF's avatar
DCloud-WZF 已提交
102 103

## component instance
DCloud-WZF's avatar
DCloud-WZF 已提交
104 105
- [x] attrs useAttrs
- [x] data
DCloud-WZF's avatar
DCloud-WZF 已提交
106
- [x] props defineProps
DCloud-WZF's avatar
DCloud-WZF 已提交
107
- [x] el
108
- [x] options defineOptions
109
- [x] parent
DCloud-WZF's avatar
DCloud-WZF 已提交
110 111 112 113 114 115
- [ ] root  root
- [ ] slots defineSlots useSlots
- [ ] refs  ref
- [ ] emit defineEmits
- [ ] forceUpdate
- [ ] methods
DCloud-WZF's avatar
DCloud-WZF 已提交
116
- [ ] provide  provide
DCloud-WZF's avatar
DCloud-WZF 已提交
117 118
- [ ] inject   inject
- [ ] mixins   mixins
DCloud-WZF's avatar
DCloud-WZF 已提交
119
- [ ] nextTick
DCloud-WZF's avatar
DCloud-WZF 已提交
120 121
- [ ] options API setup
- [ ] defineExpose
DCloud-WZF's avatar
DCloud-WZF 已提交
122
- [ ] circular reference
DCloud-WZF's avatar
DCloud-WZF 已提交
123 124

## reactivity
DCloud-WZF's avatar
DCloud-WZF 已提交
125 126 127 128 129 130 131 132
- [ ] ref
- [ ] reactive
- [ ] computed computed
- [ ] watch    watch
- [ ] watchEffect
- [ ] watchPostEffect
- [ ] watchSyncEffect
- [ ] readonly
DCloud-WZF's avatar
DCloud-WZF 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
- [ ] customRef
- [ ] effectScope
- [ ] getCurrentScope
- [ ] isProxy
- [ ] isReadonly
- [ ] isRef
- [ ] markRaw
- [ ] onScopeDispose
- [ ] shallowReactive
- [ ] shallowReadonly
- [ ] shallowRef
- [ ] toRaw
- [ ] toRef
- [ ] toRefs
- [ ] toValue
- [ ] triggerRef
- [ ] unRef

## directives

- [ ] v-bind      v-bind
- [ ] v-for       v-for
- [ ] v-if        v-if
- [ ] v-model     defineModel
- [ ] v-on        v-on
- [ ] v-show      v-show
- [ ] v-slot      v-slot
DCloud-WZF's avatar
DCloud-WZF 已提交
160 161 162 163
- [ ] v-once      v-once
- [ ] v-memo      v-memo
- [ ] v-pre       v-pre
- [ ] v-html      v-html
DCloud-WZF's avatar
DCloud-WZF 已提交
164 165 166 167 168 169 170 171

## lifecycle

- [ ] component
- [ ] page

## built-in components

DCloud-WZF's avatar
DCloud-WZF 已提交
172 173
- [ ] keepAlive keepAlive
- [ ] teleport  teleport 
DCloud-WZF's avatar
DCloud-WZF 已提交
174 175 176 177 178 179 180 181 182 183 184 185 186 187

## template

- [ ] nested-component-communication
- [ ] set-custom-child-component-root-node-class

## rendering

- [ ] component is
- [ ] render function
- [ ] slots
- [ ] template  节点中 style class v-bind map 数据
- [ ] template 标签测试
- [ ] unrecognized-component