Skip to content

Commit 26c9255

Browse files
committed
test for setting dynamic non-existent path
1 parent 34da031 commit 26c9255

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/api/data.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ exports.$set = function (exp, val) {
4444
*/
4545

4646
exports.$add = function (key, val) {
47-
_.warn(
48-
'You are dynamically adding a property "' + key + '" to ' +
49-
'a vm instance. Consider pre-initializing the property ' +
50-
'with the "data" option for more reliable reactivity ' +
51-
'and better performance.'
52-
)
5347
this._data.$add(key, val)
5448
}
5549

src/parsers/path.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ function parsePath (path) {
200200
action()
201201

202202
if (mode === 'afterPath') {
203+
keys.raw = path
203204
return keys
204205
}
205206
}
@@ -298,14 +299,25 @@ exports.set = function (obj, path, val) {
298299
if (!_.isObject(obj)) {
299300
obj = {}
300301
last.$add(key, obj)
302+
warnNonExistent(path)
301303
}
302304
} else {
303305
if (key in obj) {
304306
obj[key] = val
305307
} else {
306308
obj.$add(key, val)
309+
warnNonExistent(path)
307310
}
308311
}
309312
}
310313
return true
314+
}
315+
316+
function warnNonExistent (path) {
317+
_.warn(
318+
'You are setting a non-existent path "' + path.raw + '" ' +
319+
'on a vm instance. Consider pre-initializing the property ' +
320+
'with the "data" option for more reliable reactivity ' +
321+
'and better performance.'
322+
)
311323
}

test/unit/specs/parsers/path_spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ describe('Path Parser', function () {
4444
assertPath('foo["baz"]', ['foo', 'baz'])
4545
assertPath('foo["b\\"az"]', ['foo', 'b"az'])
4646
assertPath("foo['b\\'az']", ['foo', "b'az"])
47+
assertPath('a[b][c]', ['a', '*b', '*c'])
4748
})
4849

4950
it('handle invalid paths', function () {
@@ -64,6 +65,7 @@ describe('Path Parser', function () {
6465
assertInvalidPath(' 42 ')
6566
assertInvalidPath('foo["bar]')
6667
assertInvalidPath("foo['bar]")
68+
assertInvalidPath('foo[bar + boo]')
6769
})
6870

6971
it('caching', function () {
@@ -104,6 +106,16 @@ describe('Path Parser', function () {
104106
expect(target.a.b.c).toBe(123)
105107
})
106108

109+
it('set dynamic non-existent', function () {
110+
var target = {
111+
key: 'what',
112+
obj: {}
113+
}
114+
var res = Path.set(target, 'obj[key]', 123)
115+
expect(res).toBe(true)
116+
expect(target.obj.what).toBe(123)
117+
})
118+
107119
it('set on prototype chain', function () {
108120
var parent = { a: {} }
109121
var target = Object.create(parent)

0 commit comments

Comments
 (0)