Skip to content

Commit 7ad6a84

Browse files
authored
Merge pull request #42 from ktsn/warn-class-property
Print warning if class property is used without inheriting Vue class
2 parents 7f91b8d + 1911fdf commit 7ad6a84

File tree

6 files changed

+68
-7
lines changed

6 files changed

+68
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Note:
2121
5. For all other options, pass them to the decorator function.
2222

2323
``` js
24+
import Vue from 'vue'
2425
import Component from 'vue-class-component'
2526

2627
@Component({
@@ -38,7 +39,7 @@ import Component from 'vue-class-component'
3839
</div>
3940
`
4041
})
41-
class App {
42+
class App extends Vue {
4243
// initial data
4344
msg = 123
4445

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"babel-plugin-transform-decorators-legacy": "^1.3.4",
4242
"babel-preset-es2015": "^6.18.0",
4343
"chai": "^3.5.0",
44+
"chai-spies": "^0.7.1",
4445
"mocha": "^3.1.2",
4546
"node-libs-browser": "^1.0.0",
4647
"rimraf": "^2.5.4",

src/data.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Vue from 'vue'
22
import { VueClass } from './declarations'
3-
import { noop } from './util'
3+
import { noop, warn } from './util'
44

55
export function collectDataFromConstructor (vm: Vue, Component: VueClass) {
66
// override _init to prevent to init as Vue instance
@@ -25,5 +25,14 @@ export function collectDataFromConstructor (vm: Vue, Component: VueClass) {
2525
}
2626
})
2727

28+
if (process.env.NODE_ENV !== 'production') {
29+
if (!(Component.prototype instanceof Vue) && Object.keys(plainData).length > 0) {
30+
warn(
31+
'Component class must inherit Vue or its descendant class ' +
32+
'when class property is used.'
33+
)
34+
}
35+
}
36+
2837
return plainData
29-
}
38+
}

src/globals.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* global type declarations in this project
3+
* should not expose to userland
4+
*/
5+
6+
declare const process: {
7+
env: {
8+
NODE_ENV: string
9+
}
10+
}

src/util.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,9 @@ export function createDecorator (
1919
$decoratorQueue.push(options => factory(options, key, index))
2020
}
2121
}
22+
23+
export function warn (message: string): void {
24+
if (typeof console !== 'undefined') {
25+
console.warn('[vue-class-component] ' + message)
26+
}
27+
}

test/test-babel.js

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import Component, { createDecorator } from '../lib/index'
2-
import { expect } from 'chai'
2+
import chai, { expect } from 'chai'
3+
import spies from 'chai-spies'
34
import Vue from 'vue'
45

6+
chai.use(spies)
7+
58
describe('vue-class-component with Babel', () => {
69
it('should be instantiated without any errors', () => {
710
@Component
@@ -10,12 +13,21 @@ describe('vue-class-component with Babel', () => {
1013
})
1114

1215
it('should collect class properties as data', () => {
13-
@Component
14-
class MyComp {
16+
@Component({
17+
props: ['propValue']
18+
})
19+
class MyComp extends Vue {
1520
foo = 'hello'
21+
bar = 1 + this.propValue
1622
}
17-
const c = new MyComp()
23+
const c = new MyComp({
24+
propsData: {
25+
propValue: 1
26+
}
27+
})
1828
expect(c.foo).to.equal('hello')
29+
expect(c.propValue).to.equal(1)
30+
expect(c.bar).to.equal(2)
1931
})
2032

2133
it('should not collect uninitialized class properties', () => {
@@ -35,4 +47,26 @@ describe('vue-class-component with Babel', () => {
3547
expect('foo' in c.$data).to.be.false
3648
expect('bar' in c.$data).to.be.false
3749
})
50+
51+
it('warn if class property is used without inheriting Vue class', () => {
52+
const spy = chai.spy.on(console, 'warn')
53+
54+
@Component({
55+
foo: Number
56+
})
57+
class MyComp {
58+
bar = this.foo + 2
59+
}
60+
const c = new MyComp({
61+
propsData: {
62+
foo: 1
63+
}
64+
})
65+
66+
const message = '[vue-class-component] ' +
67+
'Component class must inherit Vue or its descendant class ' +
68+
'when class property is used.'
69+
70+
expect(spy).to.have.been.called.with(message)
71+
})
3872
})

0 commit comments

Comments
 (0)