Skip to content

Commit

Permalink
change amp-bind verification to user warning and clarify message
Browse files Browse the repository at this point in the history
  • Loading branch information
William Chou committed Feb 5, 2018
1 parent 274b63c commit 456a90b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
15 changes: 8 additions & 7 deletions extensions/amp-bind/0.1/bind-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -962,11 +962,12 @@ export class Bind {
* @private
*/
verifyBinding_(boundProperty, element, expectedValue) {
const property = boundProperty.property;
const {property, expressionString} = boundProperty;
const tagName = element.tagName;

// Don't show a warning for bind-only attributes,
// like 'slide' on amp-carousel.
const bindOnlyAttrs = BIND_ONLY_ATTRIBUTES[element.tagName];
const bindOnlyAttrs = BIND_ONLY_ATTRIBUTES[tagName];
if (bindOnlyAttrs && bindOnlyAttrs.includes(property)) {
return;
}
Expand Down Expand Up @@ -1023,11 +1024,11 @@ export class Bind {
}

if (!match) {
const err = user().createError(`${TAG}: ` +
`Default value for [${property}] does not match first expression ` +
`result (${expectedValue}). This can result in unexpected behavior ` +
'after the next state change.');
reportError(err, element);
user().warn(`${TAG}: ` +
`Default value for <${tagName} [${property}]="${expressionString}"> ` +
`does not match first result (${expectedValue}). We recommend ` +
'writing expressions with matching default values, but this can be ' +
'safely ignored if intentional.');
}
}

Expand Down
14 changes: 7 additions & 7 deletions extensions/amp-bind/0.1/test/integration/test-bind-impl.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,20 +287,20 @@ describe.configure().ifNewChrome().run('Bind', function() {
createElement(env, container, '[class]="\'foo\'" class=" foo "');
createElement(env, container, '[class]="\'\'"');
createElement(env, container, '[class]="\'bar\'" class="qux"'); // Error
const errorSpy = env.sandbox.spy(user(), 'createError');
const warnSpy = env.sandbox.spy(user(), 'warn');
return onBindReady(env, bind).then(() => {
expect(errorSpy).to.be.calledOnce;
expect(errorSpy).calledWithMatch(/bar/);
expect(warnSpy).to.be.calledOnce;
expect(warnSpy).calledWithMatch(/bar/);
});
});

it('should verify string attribute bindings in dev mode', () => {
window.AMP_MODE = {development: true, test: true};
// Only the initial value for [a] binding does not match.
createElement(env, container, '[text]="\'a\'" [class]="\'b\'" class="b"');
const errorSpy = env.sandbox.spy(user(), 'createError');
const warnSpy = env.sandbox.spy(user(), 'warn');
return onBindReady(env, bind).then(() => {
expect(errorSpy).to.be.calledOnce;
expect(warnSpy).to.be.calledOnce;
});
});

Expand All @@ -309,9 +309,9 @@ describe.configure().ifNewChrome().run('Bind', function() {
createElement(env, container, '[disabled]="true" disabled', 'button');
createElement(env, container, '[disabled]="false"', 'button');
createElement(env, container, '[disabled]="true"', 'button'); // Mismatch.
const errorSpy = env.sandbox.spy(user(), 'createError');
const warnSpy = env.sandbox.spy(user(), 'warn');
return onBindReady(env, bind).then(() => {
expect(errorSpy).to.be.calledOnce;
expect(warnSpy).to.be.calledOnce;
});
});

Expand Down

0 comments on commit 456a90b

Please sign in to comment.