Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler): don't report parse error for interpolation inside string in property binding #40267

Closed

Conversation

@crisbeto
Copy link
Member

@crisbeto crisbeto commented Dec 26, 2020

Currently we check whether a property binding contains an interpolation using a regex so that we can throw an error. The problem is that the regex doesn't account for quotes which means that something like [prop]="'{{ foo }}'" will be considered an error, even though it's not actually an interpolation.

These changes build on top of the logic from #39826 to account for interpolation characters inside quotes.

Fixes #39601.

…ng in property binding

Currently we check whether a property binding contains an interpolation using a regex so
that we can throw an error. The problem is that the regex doesn't account for quotes
which means that something like `[prop]="'{{ foo }}'"` will be considered an error, even
though it's not actually an interpolation.

These changes build on top of the logic from angular#39826 to account for interpolation
characters inside quotes.

Fixes angular#39601.
@google-cla google-cla bot added the cla: yes label Dec 26, 2020
@crisbeto crisbeto marked this pull request as ready for review Dec 26, 2020
@pullapprove pullapprove bot requested a review from alxhub Dec 26, 2020
@ngbot ngbot bot added this to the Backlog milestone Dec 26, 2020
@ngbot ngbot bot added this to the Backlog milestone Dec 26, 2020
packages/compiler/test/expression_parser/parser_spec.ts Outdated Show resolved Hide resolved
this._forEachUnquotedChar(input, 0, charIndex => {
if (startIndex === -1) {
startIndex = input.indexOf(start, charIndex);
return false;
} else {
endIndex = this._getInterpolationEndIndex(input, end, charIndex);
return endIndex > -1;
}
});
Copy link
Member

@petebacondarwin petebacondarwin Dec 29, 2020

I think that this is a bit inefficient, since it will continue to run through each unquoted character even indexOf found the start.

Also I am concerned that the following string would find a false positive:

abc '{{' }} def

since the {{ is in a string, it should be ignored but would be picked up by the indexOf(), when the charIndex is 0.

Copy link
Member

@petebacondarwin petebacondarwin Dec 29, 2020

I think this would also be clearer if you created a new function _getInterpolationStartIndex(input, expressionStart, start):

  private _getInterpolationStartIndex(input: string, expressionStart: string, start: number): number {
    let result = -1;
    this._forEachUnquotedChar(input, start, charIndex => {
      if (input.startsWith(expressionStart, charIndex)) {
        result = charIndex;
        return true;
      } else {
        return false;
      }
    });
   return result;

Then you could do:

const startIndex = this._getInterpolationStartIndex(input, start, 0);
if (startIndex === -1) {
  return;
}
const endIndex = this._getInterpolationEndIndex(input, end, startIndex+start.length);
if (endIndex === -1) {
  return;
}

this._reportError(...);

Copy link
Member Author

@crisbeto crisbeto Dec 29, 2020

I didn't want to add a _getInterpolationStartIndex, because there's some other logic that looks for an interpolation start in splitInterpolation, but the requirements there are slightly different. As for the abc '{{' }} def case, I think I tried something similar and it was reported as a different (syntax?) error further down the compilation process.

packages/compiler/src/expression_parser/parser.ts Outdated Show resolved Hide resolved
@crisbeto
Copy link
Member Author

@crisbeto crisbeto commented Dec 29, 2020

I've added the extra test cases and addressed the feedback. @petebacondarwin

Copy link
Member

@petebacondarwin petebacondarwin left a comment

Thanks for the changes @crisbeto. One last thing.

packages/compiler/src/expression_parser/parser.ts Outdated Show resolved Hide resolved
Copy link
Member

@petebacondarwin petebacondarwin left a comment

Great! Thanks for the quick turnaround.

@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jan 5, 2021

@AndrewKushnir
Copy link
Contributor

@AndrewKushnir AndrewKushnir commented Jan 5, 2021

FYI, presubmit is successful for the changes in this PR.

@AndrewKushnir AndrewKushnir removed the request for review from alxhub Jan 5, 2021
josephperrott added a commit that referenced this issue Jan 5, 2021
…ng in property binding (#40267)

Currently we check whether a property binding contains an interpolation using a regex so
that we can throw an error. The problem is that the regex doesn't account for quotes
which means that something like `[prop]="'{{ foo }}'"` will be considered an error, even
though it's not actually an interpolation.

These changes build on top of the logic from #39826 to account for interpolation
characters inside quotes.

Fixes #39601.

PR Close #40267
@angular-automatic-lock-bot
Copy link

@angular-automatic-lock-bot angular-automatic-lock-bot bot commented Feb 5, 2021

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Feb 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

3 participants