refactor: move prop decorator into core, expose initial props to initialziers

This commit is contained in:
Evan You
2019-02-26 21:33:50 -05:00
parent 2f165c1e87
commit ff9cddd46f
12 changed files with 53 additions and 62 deletions

View File

@@ -1,3 +0,0 @@
__tests__/
__mocks__/
dist/packages

View File

@@ -1 +0,0 @@
# @vue/decorators

View File

@@ -1,59 +0,0 @@
import { prop } from '../src/index'
import { Component, createInstance } from '@vue/runtime-test'
test('without options', () => {
let capturedThisValue
let capturedPropsValue
class Foo extends Component<{ p: number }> {
@prop
p: number
created() {
capturedThisValue = this.p
capturedPropsValue = this.$props.p
}
}
createInstance(Foo, {
p: 1
})
expect(capturedThisValue).toBe(1)
expect(capturedPropsValue).toBe(1)
// explicit override
createInstance(Foo, {
p: 2
})
expect(capturedThisValue).toBe(2)
expect(capturedPropsValue).toBe(2)
})
test('with options', () => {
let capturedThisValue
let capturedPropsValue
class Foo extends Component<{ p: number }> {
@prop({
default: 1
})
p: number
created() {
capturedThisValue = this.p
capturedPropsValue = this.$props.p
}
}
// default value
createInstance(Foo)
expect(capturedThisValue).toBe(1)
expect(capturedPropsValue).toBe(1)
// explicit override
createInstance(Foo, {
p: 2
})
expect(capturedThisValue).toBe(2)
expect(capturedPropsValue).toBe(2)
})

View File

@@ -1,7 +0,0 @@
'use strict'
if (process.env.NODE_ENV === 'production') {
module.exports = require('./dist/decorators.cjs.prod.js')
} else {
module.exports = require('./dist/decorators.cjs.js')
}

View File

@@ -1,21 +0,0 @@
{
"name": "@vue/decorators",
"version": "3.0.0-alpha.1",
"description": "@vue/decorators",
"main": "index.js",
"module": "dist/decorators.esm-bundler.js",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/vuejs/vue.git"
},
"keywords": [
"vue"
],
"author": "Evan You",
"license": "MIT",
"bugs": {
"url": "https://github.com/vuejs/vue/issues"
},
"homepage": "https://github.com/vuejs/vue/tree/dev/packages/decorators#readme"
}

View File

@@ -1,22 +0,0 @@
import { PropValidator, Component } from '@vue/runtime-core'
export function prop(
target: Component | PropValidator<any>,
key?: string
): any {
if (key) {
applyProp(target, key)
} else {
const options = target as PropValidator<any>
return (target: any, key: string) => {
applyProp(target, key, options)
}
}
}
function applyProp(target: any, key: string, options: PropValidator<any> = {}) {
// here `target` is the prototype of the component class
Object.defineProperty(target, `__prop_${key}`, {
value: options
})
}