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-cli): emit correct css string escape sequences #22776

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/compiler-cli/src/transformers/node_emitter.ts
Expand Up @@ -150,13 +150,28 @@ function firstAfter<T>(a: T[], predicate: (value: T) => boolean) {
// NodeEmitterVisitor
type RecordedNode<T extends ts.Node = ts.Node> = (T & { __recorded: any; }) | null;

function escapeLiteral(value: string): string {
return value.replace(/(\"|\\)/g, '\\$1').replace(/(\n)|(\r)/g, function(v, n, r) {
Copy link
Contributor

Choose a reason for hiding this comment

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

would .replace(\n).replace(\r) be more readable ?

Copy link
Contributor

Choose a reason for hiding this comment

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

or .replace(/(\n|\r)/) ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure but slower.

return n ? '\\n' : '\\r';
});
}

function createLiteral(value: any) {
if (value === null) {
return ts.createNull();
} else if (value === undefined) {
return ts.createIdentifier('undefined');
} else {
return ts.createLiteral(value);
const result = ts.createLiteral(value);
if (ts.isStringLiteral(result) && result.text.indexOf('\\') >= 0) {
// Hack to avoid problems cause indirectly by:
// https://github.com/Microsoft/TypeScript/issues/20192
// This avoids the string escaping normally performed for a string relying on that
// TypeScript just emits the text raw for a numeric literal.
(result as any).kind = ts.SyntaxKind.NumericLiteral;
result.text = `"${escapeLiteral(result.text)}"`;
}
return result;
}
}

Expand Down
8 changes: 8 additions & 0 deletions packages/compiler-cli/test/transformers/node_emitter_spec.ts
Expand Up @@ -176,6 +176,14 @@ describe('TypeScriptNodeEmitter', () => {
]).toStmt())
.replace(/\s+/gm, ''))
.toEqual(`({someKey:1,a:"a","b":"b","*":"star"});`);

// Regressions #22774
expect(emitStmt(o.literal('\\0025BC').toStmt())).toEqual('"\\\\0025BC";');
expect(emitStmt(o.literal('"some value"').toStmt())).toEqual('"\\"some value\\"";');
expect(emitStmt(o.literal('"some \\0025BC value"').toStmt()))
.toEqual('"\\"some \\\\0025BC value\\"";');
expect(emitStmt(o.literal('\n \\0025BC \n ').toStmt())).toEqual('"\\n \\\\0025BC \\n ";');
expect(emitStmt(o.literal('\r \\0025BC \r ').toStmt())).toEqual('"\\r \\\\0025BC \\r ";');
});

it('should support blank literals', () => {
Expand Down