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(ivy): i18n - ensure that colons in i18n metadata are not rendered #33820

Closed

Conversation

petebacondarwin
Copy link
Member

The : char is used as a metadata marker in $localize messages.
If this char appears in the metadata it must be escaped, as \:.
Previously, although the : char was being escaped, the TS AST
being generated was not correct and so it was being output double
escaped, which meant that it appeared in the rendered message.

As of TS 3.6.2 the "raw" string can be specified when creating tagged
template AST nodes, so it is possible to correct this.

@petebacondarwin petebacondarwin added type: bug/fix action: review The PR is still awaiting reviews from at least one requested reviewer area: i18n target: patch This PR is targeted for the next patch release hotlist: release-blocker labels Nov 14, 2019
@petebacondarwin petebacondarwin requested review from a team as code owners November 14, 2019 11:40
@ngbot ngbot bot modified the milestone: needsTriage Nov 14, 2019
Copy link
Contributor

@ocombe ocombe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, except for debug-test.sh and yarn.lock that shouldn't be part of the PR! :)

@petebacondarwin
Copy link
Member Author

Thanks @ocombe - I removed the yarn.lock but I moved the debug-test.sh to its own commit since it is an important fix and might as well go in with this PR.

Copy link
Contributor

@AndrewKushnir AndrewKushnir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@AndrewKushnir AndrewKushnir added action: presubmit The PR is in need of a google3 presubmit and removed action: review The PR is still awaiting reviews from at least one requested reviewer labels Nov 14, 2019
Copy link
Member

@josephperrott josephperrott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@petebacondarwin petebacondarwin added the action: merge The PR is ready for merge by the caretaker label Nov 14, 2019
@AndrewKushnir
Copy link
Contributor

New set of g3 presubmits:

@AndrewKushnir
Copy link
Contributor

@petebacondarwin g3 presubmit indicated the following problem:

Error: Debug Failure. False expression: Expected argument 'text' to be the normalized (i.e. 'cooked') version of argument 'rawText'.
    at createTemplateLiteralLikeNode (javascript/node_modules/typescript/stable/lib/typescript.js:64087:22)
    at Object.createNoSubstitutionTemplateLiteral (javascript/node_modules/typescript/stable/lib/typescript.js:64111:20)
    at visitLocalizedString (packages/compiler-cli/src/ngtsc/translator/src/translator.ts:552:19)
...

Even though TS version requirement is satisfied. I sent you a message in Slack with additional info.

@petebacondarwin
Copy link
Member Author

From looking at the assertion - TS is re-parsing the raw text as though it was in the original source.

For example if we are creating a simple no-substitution template literal with the text abc they put this back through their normal tokenizer (scanner) wrapped in backticks abc and check that they then get the original abc out.

It is likely that the failing messages contain unescaped backticks or ${ sequences.

For example

ab`c

In this case they would put it through the scanner as

`ab`c`

which would obviously return only ab as the cooked string.

I have added a commit that escapes these sequences.

@AndrewKushnir
Copy link
Contributor

Thanks for the fix @petebacondarwin.

I've restarted g3 presubmits with the most up-to-date version of this PR:

@IgorMinar
Copy link
Contributor

Still fails with "Debug Failure. False expression: Expected argument 'text' to be the normalized (i.e. 'cooked') version of argument 'rawText'."

@AndrewKushnir
Copy link
Contributor

@petebacondarwin I performed additional investigation and here is the pattern that is causing failures in g3:

<div i18n="Some text \' [BACKUP_MESSAGE_ID: xxx]">Test</div>

@petebacondarwin
Copy link
Member Author

Thanks @AndrewKushnir - is the \' supposed to be rendered as a ' or as \' in the browser?
I can "fix" this by double escaping any \s that are found in the description, but I am wondering what the actual intent is.

@petebacondarwin
Copy link
Member Author

I have amended the last commit to also escape \ in the "raw" form.
@AndrewKushnir - please can you try the presubmit one more time?

The `:` char is used as a metadata marker in `$localize` messages.
If this char appears in the metadata it must be escaped, as `\:`.
Previously, although the `:` char was being escaped, the TS AST
being generated was not correct and so it was being output double
escaped, which meant that it appeared in the rendered message.

As of TS 3.6.2 the "raw" string can be specified when creating tagged
template AST nodes, so it is possible to correct this.
Since i18n messages are mapped to `$localize` tagged template strings,
the "raw" version must be properly escaped. Otherwise TS will throw an
error such as:

```
Error: Debug Failure. False expression: Expected argument 'text' to be the normalized (i.e. 'cooked') version of argument 'rawText'.
```

This commit ensures that we properly escape these raw strings before creating
TS AST nodes from them.
@AndrewKushnir
Copy link
Contributor

Thanks for the fix @petebacondarwin. I've started new presubmits:

@AndrewKushnir AndrewKushnir removed action: presubmit The PR is in need of a google3 presubmit state: blocked labels Nov 18, 2019
@AndrewKushnir
Copy link
Contributor

Hi @petebacondarwin, VE and Ivy presubmits look good with the most recent changes, so I've set the g3 status to "green" and removed the "blocked" label. Thanks for the fixes 👍

@petebacondarwin petebacondarwin added action: merge The PR is ready for merge by the caretaker and removed action: merge The PR is ready for merge by the caretaker labels Nov 18, 2019
const STRING = /'(\\'|[^'])*'|"(\\"|[^"])*"/;
const BACKTICK_STRING = /\\`(([\s\S]*?)(\$\{[^}]*?\})?)*?\\`/;
const BACKTICK_STRING = /\\`(([\s\S]*?)(\$\{[^}]*?\})?)*?[^\\]\\`/;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OOC, what does the [^\\] do?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:-)

This is so that we can pick up escaped single quotes (i.e. \') that exist outside of single quoted strings.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. I guess you don't need the capturing groups (because they will be messed up anyway 😁), right?

Nit: BTW, \$\{[^}]*?\} doesn't need to be non-greedy: \$\{[^}]*\}

alxhub pushed a commit that referenced this pull request Nov 19, 2019
alxhub pushed a commit that referenced this pull request Nov 19, 2019
…#33820)

The `:` char is used as a metadata marker in `$localize` messages.
If this char appears in the metadata it must be escaped, as `\:`.
Previously, although the `:` char was being escaped, the TS AST
being generated was not correct and so it was being output double
escaped, which meant that it appeared in the rendered message.

As of TS 3.6.2 the "raw" string can be specified when creating tagged
template AST nodes, so it is possible to correct this.

PR Close #33820
alxhub pushed a commit that referenced this pull request Nov 19, 2019
…#33820)

Since i18n messages are mapped to `$localize` tagged template strings,
the "raw" version must be properly escaped. Otherwise TS will throw an
error such as:

```
Error: Debug Failure. False expression: Expected argument 'text' to be the normalized (i.e. 'cooked') version of argument 'rawText'.
```

This commit ensures that we properly escape these raw strings before creating
TS AST nodes from them.

PR Close #33820
@alxhub alxhub closed this in 74e6d37 Nov 19, 2019
alxhub pushed a commit that referenced this pull request Nov 19, 2019
…#33820)

The `:` char is used as a metadata marker in `$localize` messages.
If this char appears in the metadata it must be escaped, as `\:`.
Previously, although the `:` char was being escaped, the TS AST
being generated was not correct and so it was being output double
escaped, which meant that it appeared in the rendered message.

As of TS 3.6.2 the "raw" string can be specified when creating tagged
template AST nodes, so it is possible to correct this.

PR Close #33820
alxhub pushed a commit that referenced this pull request Nov 19, 2019
…#33820)

Since i18n messages are mapped to `$localize` tagged template strings,
the "raw" version must be properly escaped. Otherwise TS will throw an
error such as:

```
Error: Debug Failure. False expression: Expected argument 'text' to be the normalized (i.e. 'cooked') version of argument 'rawText'.
```

This commit ensures that we properly escape these raw strings before creating
TS AST nodes from them.

PR Close #33820
@petebacondarwin petebacondarwin deleted the i18n-escape-meta-blocks branch November 19, 2019 10:35
@angular-automatic-lock-bot
Copy link

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 Dec 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker area: i18n cla: yes target: patch This PR is targeted for the next patch release type: bug/fix
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants