Skip to content

Commit

Permalink
feat: rest arguments support
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed May 7, 2019
1 parent 35f6a74 commit 7ff1b23
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 12 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,8 @@ $f = function () use(&$b) {
```typescript
function funcA(...args: string[]) {
}
function funcC(a: string, ...args: string[]) {
}
```

output
Expand All @@ -437,9 +439,12 @@ output
function funcA() {
$args = func_get_args();
}
function funcC() {
$a = func_get_arg(0); $args = array_slice(func_get_args(), 1);
}
```

> 注:箭头函数暂不支持,并且只能用于所有参数都用 `...` 来加载的情况
> 注:箭头函数暂不支持
### Core JavaScript API

Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ export function compile(filePath: string, options: Ts2phpOptions = {}) {
};
}


project.resolveSourceFileDependencies();
const program = project.getProgram().compilerObject;

Expand Down Expand Up @@ -145,6 +144,7 @@ export function compile(filePath: string, options: Ts2phpOptions = {}) {

return {
phpCode: code,
errors: state.errors
errors: state.errors,
sourceFile: state.sourceFile
};
}
61 changes: 53 additions & 8 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,26 +337,71 @@ export function transform(context: ts.TransformationContext) {

const parameterIndex = node.parameters.findIndex(node => !!node.dotDotDotToken);

if (parameterIndex > 0) {
if (parameterIndex < 0) {
return node.body;
}

const parameter = node.parameters[parameterIndex];
const declarationName = parameter.name.kind === ts.SyntaxKind.Identifier ? ts.getMutableClone(parameter.name) : ts.createTempVariable(/*recordTempVariable*/ undefined);
node.parameters = ts.createNodeArray(node.parameters.filter(node => !node.dotDotDotToken));
const statement = ts.createVariableStatement(
/*modifiers*/ undefined,
ts.createVariableDeclarationList([
const declarationName = parameter.name.kind === ts.SyntaxKind.Identifier
? ts.getMutableClone(parameter.name)
: ts.createTempVariable(/*recordTempVariable*/ undefined);

const parameters = [...node.parameters];
node.parameters = ts.createNodeArray([]);

let variableDeclarationList: ts.VariableDeclarationList;

if (parameterIndex === 0) {
variableDeclarationList = ts.createVariableDeclarationList([
ts.createVariableDeclaration(
declarationName,
/*type*/ undefined,
undefined,
ts.createCall(
ts.createIdentifier('func_get_args'),
[],
[]
)
)
])
]);
}
else {
let list = parameters
.slice(0, parameters.length - 1)
.map((parameter, index) =>
ts.createVariableDeclaration(
parameter.name,
undefined,
ts.createCall(
ts.createIdentifier('func_get_arg'),
[],
[ts.createNumericLiteral(`${index}`)]
)
)
);
list.push(
ts.createVariableDeclaration(
declarationName,
undefined,
ts.createCall(
ts.createIdentifier('array_slice'),
undefined,
[
ts.createCall(
ts.createIdentifier('func_get_args'),
undefined,
[]
),
ts.createNumericLiteral(`${parameterIndex}`)
]
)
)
);
variableDeclarationList = ts.createVariableDeclarationList(list);
}

const statement = ts.createVariableStatement(
undefined,
variableDeclarationList
);

return ts.createBlock(
Expand Down
5 changes: 4 additions & 1 deletion test/features/Spread.php → test/features/restArguments.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace test\case_Spread;
namespace test\case_restArguments;
function funcA() {
$args = func_get_args();
}
Expand All @@ -11,3 +11,6 @@ function funcB() {
$f = \Ts2Php_Helper::includes($d, "1");
call_user_func_array("funcA", array_merge(array("a"), $args, array($c)));
}
function funcC() {
$a = func_get_arg(0); $args = array_slice(func_get_args(), 1);
}
3 changes: 3 additions & 0 deletions test/features/Spread.ts → test/features/restArguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ function funcB(...args: string[]) {
let f = d.includes('1');
funcA('a', ...args, c);
}

function funcC(a: string, ...args: string[]) {
}

0 comments on commit 7ff1b23

Please sign in to comment.