Skip to content

Commit

Permalink
feat: add Math
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Dec 14, 2018
1 parent a06373b commit a2b7951
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 9 deletions.
32 changes: 24 additions & 8 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
isStringLike,
isPropertyAccessExpression,
isRegularExpressionLiteral,
isLiteralTypeNode,
} from './utilities/nodeTest';
import {
forEach,
Expand All @@ -49,7 +50,8 @@ import {Ts2phpOptions, ErrorInfo} from './types';
import {options as globalOptions, errors} from './globals';
import {getNodeId} from './checker';

import * as StringProtoHelpers from './features/string';
import * as StringProtoHelper from './features/string';
import * as MathHelper from './features/math';

let currentSourceFile: SourceFile;

Expand Down Expand Up @@ -1095,20 +1097,34 @@ export function emitFile(sourceFile: SourceFile, typeChecker: ts.TypeChecker) {
function emitCallExpression(node: ts.CallExpression) {

const expNode = node.expression;
const helpers = {
getLiteralTextOfNode,
emitExpressionList,
writePunctuation,
typeChecker
};

// string.prototype.replace
if (
isPropertyAccessExpression(expNode)
&& isStringLike(expNode.expression, typeChecker)
) {
const stringHelper = StringProtoHelpers[getTextOfNode(expNode.name)];
const stringHelper = StringProtoHelper[getTextOfNode(expNode.name)];
if (stringHelper) {
stringHelper(node, {
getLiteralTextOfNode,
emitExpressionList,
writePunctuation,
typeChecker
});
stringHelper(node, helpers);
}
return;
}

// Math.xx
if (
isPropertyAccessExpression(expNode)
&& isIdentifier(expNode.expression)
&& expNode.expression.escapedText === 'Math'
) {
const stringHelper = MathHelper[getTextOfNode(expNode.name)];
if (stringHelper) {
stringHelper(node, helpers);
}
return;
}
Expand Down
57 changes: 57 additions & 0 deletions src/features/math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @file Math 方法
* @author cxtom(cxtom2008@gmail.com)
*/

import {
CallExpression,
ListFormat
} from 'typescript';

function method(node: CallExpression, {emitExpressionList, writePunctuation}, method) {
writePunctuation(method);
emitExpressionList(node, node.arguments, ListFormat.CallExpressionArguments);
}

const map = {
abs: 'abs',
acos: 'acos',
acosh: 'acosh',
asin: 'asin',
asinh: 'asinh',
atan: 'atan',
atanh: 'atanh',
atan2: 'atan2',
cbrt: 'cbrt',
ceil: 'ceil',
clz32: 'clz32',
cos: 'cos',
cosh: 'cosh',
exp: 'exp',
expm1: 'expm1',
floor: 'floor',
// fround: 'fround',
hypot: 'hypot',
// imul: 'imul',
log: 'log',
log1p: 'log1p',
log10: 'log10',
// log2: 'log2',
max: 'max',
min: 'min',
pow: 'pow',
random: 'rand',
round: 'round',
// sign: 'sign',
sin: 'sin',
sinh: 'sinh',
sqrt: 'sqrt',
tan: 'tan',
tanh: 'tanh',
};

for (let key in map) {
if (map.hasOwnProperty(key)) {
exports[key] = (...args) => method(args[0], args[1], key);
}
}
1 change: 0 additions & 1 deletion src/features/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export function split(node: CallExpression, {emitExpressionList, writePunctuatio
emitExpressionList(node, node.arguments, ListFormat.CallExpressionArguments);
}


const map = {
trim: 'trim',
trimRight: 'rtrim',
Expand Down
2 changes: 2 additions & 0 deletions test/features/Math.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?php
$a = max(1, 2);
1 change: 1 addition & 0 deletions test/features/Math.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const a = Math.max(1, 2);

0 comments on commit a2b7951

Please sign in to comment.