Skip to content

Commit

Permalink
[FEATURE beta] Assert expand properties are balanced and not nested
Browse files Browse the repository at this point in the history
In #13693, it is asked to error for an unsupported feature. Though we
don't say that this is not supported, it would be nice if we could let
the user know that what he is trying to do will not work instead of
silently fail.

Fixes #13693

(cherry picked from commit 6f2d615)
  • Loading branch information
Serabe authored and Robert Jackson committed Nov 7, 2016
1 parent b901dfe commit c50695e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/ember-metal/lib/expand_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,27 @@ export default function expandProperties(pattern, callback) {
'Brace expanded properties cannot contain spaces, e.g. "user.{firstName, lastName}" should be "user.{firstName,lastName}"',
pattern.indexOf(' ') === -1
);
assert(
`Brace expanded properties have to be balanced and cannot be nested, pattern: ${pattern}`,
((str) => {
let inBrace = 0;
let char;
for (let i = 0; i < str.length; i++) {
char = str.charAt(i);

if (char === '{') {
inBrace++;
} else if (char === '}') {
inBrace--;
}

if (inBrace > 1 || inBrace < 0) {
return false;
}
}

return true;
})(pattern));

let parts = pattern.split(SPLIT_REGEX);
let properties = [parts];
Expand Down
14 changes: 14 additions & 0 deletions packages/ember-metal/tests/expand_properties_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ QUnit.test('A property with only brace expansions expands correctly', function()
deepEqual(expected.sort(), foundProperties.sort());
});

QUnit.test('Nested brace expansions are not allowed', function() {
let nestedBraceProperties = [
'a.{b.{c,d}}',
'a.{{b}.c}',
'a.{b,c}.{d.{e,f}.g',
'a.{b.{c}',
'a.{b,c}}'
];

nestedBraceProperties.forEach((invalidProperties) => {
expectAssertion(() => expandProperties(invalidProperties, addProperty));
}, /Brace expanded properties have to be balanced and cannot be nested/);
});

QUnit.test('A pattern must be a string', function() {
expect(1);

Expand Down

0 comments on commit c50695e

Please sign in to comment.