Skip to content

Commit 9b76588

Browse files
committed
[BUGFIX LTS] Warn about invalid properties
There was a bug in the brace expansion implementation before 2.13 that admitted expressions like `a.{b,c` and `a.b,c` in property expansion without failing. On February 2017, emberjs#13231 was merged improving the implementation of `expandProperties` and fixing the previous issue. Sadly, people upgrading from 2.12 are having problems with this. We have two options here: - Backporting emberjs#13231 fixes it and can be done, as of today, without any problem just by cherry-picking its two commits. Sadly, this would break people apps in a patch version, and this bug does not seem like a critical bugfix. - Adding a warning if a user is using invalid properties. I went with this option since it looks like the less intrusive one while letting the users know that their apps will break in the next update.
1 parent 527b317 commit 9b76588

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

packages/ember-metal/lib/expand_properties.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert } from './debug';
1+
import { assert, warn } from './debug';
22

33
/**
44
@module ember
@@ -63,6 +63,30 @@ export default function expandProperties(pattern, callback) {
6363
return true;
6464
})(pattern));
6565

66+
warn(
67+
`You used the key "${pattern}" which is invalid and is not working starting on Ember 2.13.`,
68+
((str) => {
69+
let inBrace = 0;
70+
let char;
71+
for (let i = 0; i < str.length; i++) {
72+
char = str.charAt(i);
73+
74+
if (char === '{') {
75+
inBrace++;
76+
} else if (char === '}') {
77+
inBrace--;
78+
} else if (char === ',' && inBrace === 0) {
79+
return false;
80+
}
81+
82+
if (inBrace > 1 || inBrace < 0) {
83+
return false;
84+
}
85+
}
86+
87+
return inBrace === 0;
88+
})(pattern), { id: 'ember-metal.invalid-expand-properties' });
89+
6690
let parts = pattern.split(SPLIT_REGEX);
6791
let properties = [parts];
6892

packages/ember-metal/tests/expand_properties_test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,19 @@ QUnit.test('A pattern must be a string', function() {
9797
}, /A computed property key must be a string/);
9898
});
9999

100+
QUnit.test('unbalanced braces are warned', function() {
101+
let invalidBraceProperties = [
102+
'model.{baz,bar',
103+
'model.bar,bar'
104+
];
105+
106+
invalidBraceProperties.forEach((invalidProperties) =>
107+
expectWarning(() => {
108+
expandProperties('model.{baz,bar', addProperty)
109+
}, / which is invalid and is not working starting on Ember 2\.13\./)
110+
);
111+
});
112+
100113
QUnit.test('A pattern must not contain a space', function() {
101114
expect(1);
102115

0 commit comments

Comments
 (0)