Skip to content

Commit

Permalink
feat: str.replace start
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Dec 13, 2018
1 parent 06dce2e commit 0cf5cb2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ import {
shouldUseArray,
shouldAddDoubleQuote,
isBlock,
isStringLike
isStringLike,
isCallExpression,
isPropertyAccessExpression
} from './utilities/nodeTest';
import {
forEach,
Expand Down Expand Up @@ -70,7 +72,7 @@ export function emitFile(sourceFile: SourceFile, typeChecker: ts.TypeChecker) {
reset();
const writer = createTextWriter("\n");
writer.writeLine();


// 变量与 module 的映射,标记某个变量是从哪个 module 中引入的
// 调用函数的时候,需要转换成类方法
Expand All @@ -83,7 +85,7 @@ export function emitFile(sourceFile: SourceFile, typeChecker: ts.TypeChecker) {
// console.log(writer.getText());
});
return writer.getText();


// let commitPendingSemicolon: typeof commitPendingSemicolonInternal = noop;

Expand Down Expand Up @@ -1082,6 +1084,21 @@ export function emitFile(sourceFile: SourceFile, typeChecker: ts.TypeChecker) {
}

function emitCallExpression(node: ts.CallExpression) {

const expNode = node.expression;

// string.prototype.replace
if (
isPropertyAccessExpression(expNode)
&& isStringLike(expNode.expression, typeChecker)
&& expNode.name.escapedText === 'replace'
) {
writePunctuation('str_replace');
node.arguments = ts.createNodeArray([...node.arguments, expNode.expression]);
emitExpressionList(node, node.arguments, ts.ListFormat.CallExpressionArguments);
return;
}

emitWithHint(ts.EmitHint.Expression, node.expression);
// emitTypeArguments(node, node.typeArguments);
emitExpressionList(node, node.arguments, ts.ListFormat.CallExpressionArguments);
Expand Down Expand Up @@ -2956,7 +2973,7 @@ export function emitFile(sourceFile: SourceFile, typeChecker: ts.TypeChecker) {
head = '"' + head;
tail += '"';
}

// 是从模块中引入的
if (varModuleMap[name]) {
const moduleName = varModuleMap[name];
Expand Down
3 changes: 3 additions & 0 deletions test/features/stringProto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
$a = "wwa";
$b = str_replace("a", "b", $a);
3 changes: 3 additions & 0 deletions test/features/stringProto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

let a = 'wwa';
let b = a.replace('a', 'b');

0 comments on commit 0cf5cb2

Please sign in to comment.