Skip to content

Commit

Permalink
fix: add default initializer for unrequired parameter, close #115
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Jun 4, 2020
1 parent dfd03d7 commit 9050165
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,9 @@ export function emitFile(
else {
emitTypeAnnotation(node.type);
}
if (node.questionToken && !node.initializer) {
node.initializer = ts.createIdentifier('null');
}
// The comment position has to fallback to any present node within the parameterdeclaration because as it turns out, the parser can make parameter declarations with _just_ an initializer.
emitInitializer(node.initializer, node.type ? node.type.end : node.questionToken ? node.questionToken.end : node.name ? node.name.end : node.modifiers ? node.modifiers.end : node.decorators ? node.decorators.end : node.pos, node);
}
Expand Down
4 changes: 2 additions & 2 deletions test/features/FunctionDeclaration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ FunctionDeclaration
## FunctionDeclaration

```ts
function aaa(a: string, b: string, c: string = undefined) {
function aaa(a: string, b?: string, c: string = undefined) {
const d = 123;
const e = b + 123;
return `1${a}2${e}3${c}4${d}`;
Expand Down Expand Up @@ -42,7 +42,7 @@ if (b) {
```

```php
function aaa($a, $b, $c = null) {
function aaa($a, $b = null, $c = null) {
$d = 123;
$e = $b . 123;
return "1" . $a . "2" . $e . "3" . $c . "4" . $d;
Expand Down
2 changes: 1 addition & 1 deletion test/features/funcParamRefer.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $b = array(
"a" => 1
);
$c = "123";
function aaa($m, $n, $k, $d) {
function aaa($m, $n, $k, $d = null) {
array_push($m, 4);
$n["b"] = 2;
$k .= "4";
Expand Down

0 comments on commit 9050165

Please sign in to comment.