Skip to content

Commit

Permalink
feat: support class extends and super keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Mar 13, 2019
1 parent d6f1321 commit e217375
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,9 @@ do {
#### `Class`/

```javascript
class Article {
import {Base} from '../some-utils';

class Article extends Base {

public title: string;
id: number;
Expand All @@ -164,32 +166,39 @@ class Article {
static published = [];

constructor(options: {title: string}) {
super(options);
this.title = options.title;
this.publish(1);
}

private publish(id) {
Article.published.push(id);
super.dispose();
}
}
```

output

```php
class Article {
require_once("../some-utils");
use \Base;
class Article extends Base {
public $title;
$id;
private $_x;
static $published = array();
constructor($options) {
__construct($options) {
parent::__construct($options);
$this->title = $options["title"];
$this->publish(1);
}
private publish($id) {
array_push(Article::$published, $id);
parent::dispose();
}
}

```

#### `typeof`
Expand Down
42 changes: 24 additions & 18 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
// return emitInferType(<InferTypeNode>node);
// case SyntaxKind.ParenthesizedType:
// return emitParenthesizedType(<ParenthesizedTypeNode>node);
// case SyntaxKind.ExpressionWithTypeArguments:
// return emitExpressionWithTypeArguments(<ExpressionWithTypeArguments>node);
case SyntaxKind.ExpressionWithTypeArguments:
return emitExpressionWithTypeArguments(<ts.ExpressionWithTypeArguments>node);
// case SyntaxKind.ThisType:
// return emitThisType();
// case SyntaxKind.TypeOperator:
Expand Down Expand Up @@ -338,6 +338,7 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
case SyntaxKind.StaticKeyword:
case SyntaxKind.PublicKeyword:
case SyntaxKind.PrivateKeyword:
case SyntaxKind.ProtectedKeyword:
writeTokenNode(node, writeKeyword);
return;

Expand Down Expand Up @@ -414,8 +415,8 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
return emitCaseClause(<ts.CaseClause>node);
case SyntaxKind.DefaultClause:
return emitDefaultClause(<ts.DefaultClause>node);
// case SyntaxKind.HeritageClause:
// return emitHeritageClause(<HeritageClause>node);
case SyntaxKind.HeritageClause:
return emitHeritageClause(<ts.HeritageClause>node);
// case SyntaxKind.CatchClause:
// return emitCatchClause(<CatchClause>node);

Expand Down Expand Up @@ -748,7 +749,6 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {

function emitPropertyDeclaration(node: ts.PropertyDeclaration) {
// emitDecorators(node, node.decorators);
// console.log('haha', node);
emitModifiers(node, node.modifiers);
emit(node.name);
// emit(node.questionToken);
Expand Down Expand Up @@ -794,7 +794,7 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {

function emitConstructor(node: ts.ConstructorDeclaration) {
// emitModifiers(node, node.modifiers);
writeKeyword("constructor");
writeKeyword("__construct");
emitSignatureAndBody(node, emitSignatureHead);
}

Expand Down Expand Up @@ -1105,7 +1105,6 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
const preferNewLine = node.multiLine ? ts.ListFormat.PreferNewLine : ts.ListFormat.None;
// const allowTrailingComma = currentSourceFile.languageVersion >= ScriptTarget.ES5 && !isJsonSourceFile(currentSourceFile) ? ListFormat.AllowTrailingComma : ListFormat.None;
const allowTrailingComma = ts.ListFormat.None;
// console.log(node.properties);
emitList(node, node.properties, ts.ListFormat.ObjectLiteralExpressionProperties | allowTrailingComma | preferNewLine);

// if (indentedFlag) {
Expand Down Expand Up @@ -1231,7 +1230,6 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
emitParametersForArrow(node, node.parameters);
// emitTypeAnnotation(node.type);
// writeSpace();
// console.log(node);
// emit(node.equalsGreaterThanToken);
}

Expand Down Expand Up @@ -1350,10 +1348,10 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
emitClassDeclarationOrExpression(node);
}

// function emitExpressionWithTypeArguments(node: ExpressionWithTypeArguments) {
// emitExpression(node.expression);
// emitTypeArguments(node, node.typeArguments);
// }
function emitExpressionWithTypeArguments(node: ts.ExpressionWithTypeArguments) {
emitExpression(node.expression);
// emitTypeArguments(node, node.typeArguments);
}

function emitAsExpression(node: ts.AsExpression) {
emitExpression(node.expression);
Expand Down Expand Up @@ -2233,12 +2231,12 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
emitList(parentNode, statements, format);
}

// function emitHeritageClause(node: HeritageClause) {
// writeSpace();
// writeTokenText(node.token, writeKeyword);
// writeSpace();
// emitList(node, node.types, ListFormat.HeritageClauseTypes);
// }
function emitHeritageClause(node: ts.HeritageClause) {
writeSpace();
writeTokenText(node.token, writeKeyword);
writeSpace();
emitList(node, node.types, ListFormat.HeritageClauseTypes);
}

// function emitCatchClause(node: CatchClause) {
// const openParenPos = emitTokenWithComment(SyntaxKind.CatchKeyword, node.pos, writeKeyword, node);
Expand Down Expand Up @@ -2980,6 +2978,14 @@ export function emitFile(sourceFile: SourceFile, state: CompilerState) {
if (node.kind === SyntaxKind.ThisKeyword) {
writer('$');
}
if (node.kind === SyntaxKind.SuperKeyword && ts.isCallExpression(node.parent)) {
writer('parent::__construct');
return;
}
if (node.kind === SyntaxKind.SuperKeyword && ts.isPropertyAccessExpression(node.parent)) {
writer('parent');
return;
}
writer(tokenToString(node.kind)!);
}

Expand Down
8 changes: 6 additions & 2 deletions test/features/Class.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
<?php
namespace test\Class;
class Article {
require_once("../some-utils");
use \Base;
class Article extends Base {
public $title;
$id;
private $_x;
static $published = array();
constructor($options) {
__construct($options) {
parent::__construct($options);
$this->title = $options["title"];
$this->publish(1);
}
private publish($id) {
array_push(Article::$published, $id);
parent::dispose();
}
}
5 changes: 4 additions & 1 deletion test/features/Class.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import {Base} from '../some-utils';

class Article {
class Article extends Base {

public title: string;
id: number;
Expand All @@ -10,11 +11,13 @@ class Article {
static published = [];

constructor(options: {title: string}) {
super(options);
this.title = options.title;
this.publish(1);
}

private publish(id) {
Article.published.push(id);
super.dispose();
}
}
1 change: 0 additions & 1 deletion test/features/import.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php
namespace test\import;
require_once("../some-utils");
use \Other_Utils as Util;
use \Some_Utils;
use \func;
Expand Down
7 changes: 6 additions & 1 deletion test/some-utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ export class Other_Utils {
hello(): string;
}

export function func(): string;
export function func(): string;

export class Base {
constructor(options: {title: string});
dispose(): void;
}

0 comments on commit e217375

Please sign in to comment.