Skip to content

Commit dffd4b0

Browse files
committed
deprecating @DaTa: fix failing unit tests
1 parent 1d2c533 commit dffd4b0

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

test/browser/specs/watch.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ describe('watch decorator', () => {
2929
data2: number = 1;
3030

3131
@Data()
32-
info: string;
32+
info: string = undefined;
3333

3434
changeData1() {
3535
this.data1 = 'Hola!';
@@ -78,7 +78,7 @@ describe('watch decorator', () => {
7878
this.title = title;
7979
}
8080

81-
title: string
81+
title: string = undefined
8282
}
8383

8484

@@ -97,7 +97,7 @@ describe('watch decorator', () => {
9797
data: Foo[] = [ new Foo('Hello!') ];
9898

9999
@Data()
100-
info: string;
100+
info: string = undefined;
101101

102102
changeData1() {
103103
this.data[0].title = 'Hi!';

test/common/specs/data.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('vue-class-component data test', () => {
1212
class App {
1313

1414
@Data()
15-
msg: string
15+
msg: string = undefined
1616
}
1717

1818
var app = new App();

test/common/specs/inheritance.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ describe('inheritance', () => {
3131
class App extends Base {
3232

3333
@Data()
34-
msg: string
34+
msg: string = undefined
3535
}
3636

3737
var app = new App();
@@ -49,7 +49,7 @@ describe('inheritance', () => {
4949
class App extends AbstractClass {
5050

5151
@Data()
52-
msg: string
52+
msg: string = undefined
5353
}
5454

5555
var app = new App();
@@ -65,7 +65,7 @@ describe('inheritance', () => {
6565
class App extends AbstractComponent {
6666

6767
@Data()
68-
msg: string
68+
msg: string = undefined
6969
}
7070

7171
var app = new App();

test/es6/test.build.js

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,7 @@
277277
}
278278

279279
function Data() {
280-
return function (target, key) {
281-
if (target['data'] && target['data'] instanceof Function) {
282-
throw "vue-typed error: [" + target.constructor.name + "]: You can't use @data attribute while you have already data() function in your class.";
283-
}
284-
var id = '$_vt_data';
285-
if (!target[id]) {
286-
target[id] = {};
287-
}
288-
if (!target[id][key]) {
289-
target[id][key] = key;
290-
}
291-
};
280+
return function (target, key) {};
292281
}
293282

294283
function Getter(getter) {
@@ -305,8 +294,9 @@
305294
};
306295
}
307296

297+
var vueInternalPropNames = Object.getOwnPropertyNames(new Vue());
298+
var vueInternalHooks = ['activate', 'init', 'ready', 'beforeCompile', 'compiled', 'attached', 'detached', 'created', 'beforeDestroy', 'destroyed', 'props', 'watch', 'data', 'beforeCreate', 'beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'render', 'el', 'vuex'];
308299
function Component(options) {
309-
var internalHooks = ['activate', 'init', 'ready', 'beforeCompile', 'compiled', 'attached', 'detached', 'created', 'beforeDestroy', 'destroyed', 'props', 'watch', 'data', 'beforeCreate', 'beforeMount', 'mounted', 'beforeUpdate', 'updated', 'activated', 'deactivated', 'render', 'el', 'vuex'];
310300
var factory = function factory(Component, options) {
311301
if (!options) {
312302
options = {};
@@ -328,27 +318,15 @@
328318
});
329319
}
330320
}
331-
var dataAttrs = proto['$_vt_data'];
332-
delete proto['$_vt_data'];
333-
if (dataAttrs) {
334-
var data_keys = Object.getOwnPropertyNames(dataAttrs);
335-
options['data'] = function () {
336-
var data_obj = {};
337-
for (var i = 0; i < data_keys.length; i++) {
338-
var prop = data_keys[i];
339-
data_obj[prop] = constructor[prop];
340-
}
341-
return data_obj;
342-
};
343-
}
344321
var propAttrs = proto['$_vt_props'];
322+
var propNames = undefined;
345323
delete proto['$_vt_props'];
346324
if (propAttrs) {
347325
var key = 'props';
348326
var props = {};
349-
var prop_keys = Object.getOwnPropertyNames(propAttrs);
350-
for (var i = 0; i < prop_keys.length; i++) {
351-
var prop = prop_keys[i];
327+
propNames = Object.getOwnPropertyNames(propAttrs);
328+
for (var i = 0; i < propNames.length; i++) {
329+
var prop = propNames[i];
352330
var propVal = undefined;
353331
var descriptor = Object.getOwnPropertyDescriptor(propAttrs, prop);
354332
var constructorDefault = constructor[prop];
@@ -371,7 +349,7 @@
371349
if (key === 'constructor') {
372350
return;
373351
}
374-
if (internalHooks.indexOf(key) > -1) {
352+
if (vueInternalHooks.indexOf(key) > -1) {
375353
options[key] = proto[key];
376354
return;
377355
}
@@ -386,6 +364,34 @@
386364
};
387365
}
388366
});
367+
var dataNames = [];
368+
var restrictedNames = vueInternalPropNames;
369+
if (propNames) restrictedNames = restrictedNames.concat(propNames);
370+
if (vueKeys && vueKeys.length) restrictedNames = restrictedNames.concat(vueKeys);
371+
Object.getOwnPropertyNames(constructor).forEach(function (key) {
372+
if (restrictedNames.indexOf(key) === -1) {
373+
dataNames.push(key);
374+
}
375+
});
376+
if (dataNames.length > 0) {
377+
var parentData = undefined;
378+
var parentDataType = _typeof(options['data']);
379+
if (parentDataType === 'function') {
380+
parentData = options['data']();
381+
} else if (parentDataType === 'object') {
382+
parentData = options['data'];
383+
}
384+
options['data'] = function () {
385+
var data_obj = parentData || {};
386+
dataNames.forEach(function (prop) {
387+
var descriptor = Object.getOwnPropertyDescriptor(constructor, prop);
388+
if (!descriptor.get && !descriptor.set && typeof descriptor.value !== 'function') {
389+
data_obj[prop] = constructor[prop];
390+
}
391+
});
392+
return data_obj;
393+
};
394+
}
389395
var superProto = Object.getPrototypeOf(proto);
390396
var Super = superProto instanceof Vue ? superProto.constructor : Vue;
391397
return Super['extend'](options);

0 commit comments

Comments
 (0)