Skip to content

Commit

Permalink
fix: $ in string
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jun 21, 2019
1 parent a199acb commit b82a011
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"fs-extra": "^7.0.1",
"lodash": "^4.17.11",
"ts-morph": "^2.3.0",
"typescript": "3.4.5",
"typescript": "~3.4.5",
"yargs": "^13.2.4"
},
"nyc": {
Expand Down
9 changes: 6 additions & 3 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ export function emitFile(
// SyntaxKind.TemplateMiddle
// SyntaxKind.TemplateTail
function emitLiteral(node: ts.LiteralLikeNode) {
const text = getLiteralTextOfNode(node, true);
let text = getLiteralTextOfNode(node, true);
text = text.replace(/(\\)?\$/g, '\\$');
// Quick info expects all literals to be called with writeStringLiteral, as there's no specific type for numberLiterals
writeStringLiteral(text);
}
Expand Down Expand Up @@ -2338,6 +2339,9 @@ export function emitFile(
if (ts.isLiteralExpression(node.name)) {
emitLiteral(node.name);
}
else if (ts.isIdentifier(node.name)) {
emit(node.name);
}
else {
emit(node.name);
}
Expand Down Expand Up @@ -3244,7 +3248,6 @@ export function emitFile(
// return idText(node);
// }
if (isIdentifier(node)) {
const name = (<ts.Identifier>node).escapedText as string;
let head = '';
let tail = '';

Expand All @@ -3265,7 +3268,7 @@ export function emitFile(
// head = className + '::' + head;
// }

return head + idText(<ts.Identifier>node) + tail;
return head + idText(<ts.Identifier>node).replace(/(\\)?\$/g, '\\$') + tail;
}

if (isImportSpecifier(node)) {
Expand Down
4 changes: 4 additions & 0 deletions test/features/ObjectLiteralExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@
"a-b" => $b
);
$c = array_key_exists("b", $a);
$d = array(
"\$a" => "a",
"b" => "\$b"
);
5 changes: 5 additions & 0 deletions test/features/ObjectLiteralExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ const a = {
};

const c = 'b' in a;

const d = {
$a: "a",
b: "$b"
};

0 comments on commit b82a011

Please sign in to comment.