Skip to content

Commit

Permalink
fix: #95
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Nov 28, 2019
1 parent d145546 commit 1d3e1e7
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 33 deletions.
38 changes: 5 additions & 33 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1149,37 +1149,7 @@ export function emitFile(

function emitPropertyAccessExpression(node: ts.PropertyAccessExpression) {

const symbol = typeChecker.getSymbolAtLocation(node);

let prefix = '["';
let suffix = '"]';

if (symbol) {
let expression = utilities.getRealExpression(node.expression);
if (
// $this->func();
expression.kind === ts.SyntaxKind.ThisKeyword
|| isClassInstance(expression, typeChecker)
|| expression.kind === ts.SyntaxKind.NewExpression
) {
prefix = '->';
suffix = '';
}
else if (isClassLike(node.expression, typeChecker)) {
switch (symbol.getFlags()) {
case ts.SymbolFlags.Method:
prefix = '::';
suffix = '';
break;
case ts.SymbolFlags.Property:
prefix = '::$';
suffix = '';
break;
default:
break;
}
}
}
const {prefix, suffix} = utilities.getAccessPrefixAndSuffix(node, typeChecker);

emitWithHint(ts.EmitHint.Expression, node.expression);
writePunctuation(prefix);
Expand Down Expand Up @@ -1208,10 +1178,12 @@ export function emitFile(
// }

function emitElementAccessExpression(node: ts.ElementAccessExpression) {
const {prefix, suffix} = utilities.getAccessPrefixAndSuffix(node, typeChecker);

emitExpression(node.expression);
emitTokenWithComment(SyntaxKind.OpenBracketToken, node.expression.end, writePunctuation, node);
writePunctuation(prefix);
emitExpression(node.argumentExpression);
emitTokenWithComment(SyntaxKind.CloseBracketToken, node.argumentExpression.end, writePunctuation, node);
writePunctuation(suffix);
}

function emitCallExpression(node: ts.CallExpression) {
Expand Down
44 changes: 44 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import * as ts from 'typescript';
import {Node} from 'typescript';
import {error} from './state';
import { isClassInstance, isClassLike } from './utilities/nodeTest';

const indentStrings: string[] = ["", " "];
export function getIndentString(level: number) {
Expand Down Expand Up @@ -463,3 +464,46 @@ export function getRealExpression(node: ts.Node) {
}
return node;
}

export function getAccessPrefixAndSuffix(node: ts.PropertyAccessExpression | ts.ElementAccessExpression, typeChecker: ts.TypeChecker) {
const symbol = typeChecker.getSymbolAtLocation(node);

let prefix = '[';
let suffix = ']';
if (ts.isPropertyAccessExpression(node)) {
prefix = '["';
suffix = '"]';
}

// if (symbol) {
let expression = getRealExpression(node.expression);
if (
// $this->func();
expression.kind === ts.SyntaxKind.ThisKeyword
|| isClassInstance(expression, typeChecker)
|| expression.kind === ts.SyntaxKind.NewExpression
) {
prefix = '->';
suffix = '';
}
else if (symbol && isClassLike(node.expression, typeChecker)) {
switch (symbol.getFlags()) {
case ts.SymbolFlags.Method:
prefix = '::';
suffix = '';
break;
case ts.SymbolFlags.Property:
prefix = '::$';
suffix = '';
break;
default:
break;
}
}
// }

return {
prefix,
suffix
};
}
9 changes: 9 additions & 0 deletions test/features/Class.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ private function publish($id) {
}
$a = new Article(array( "title" => "a" ));
$b = $a->base;
$name = "base";
$d = $a->$name;
$a->dispose();
echo $b;
class A {
Expand All @@ -41,3 +43,10 @@ function getName() {
$n = "cat";
$c = new Cat($n);
echo $c->getName();
abstract class XX {
}
function get($name) {
return array();
}
$val = get("aaaa");
echo $val->a;
11 changes: 11 additions & 0 deletions test/features/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ export class Article extends Base {

const a = new Article({title: 'a'});
const b = a.base;

const name = 'base';
const d = a[name];

a.dispose();
console.log(b);

Expand All @@ -52,3 +56,10 @@ const n = 'cat';

const c = new Cat(n);
console.log(c.getName());

abstract class XX { [key:string]: any }
function get(name: string): any {
return {};
}
const val: XX = get('aaaa');
console.log(val.a);

0 comments on commit 1d3e1e7

Please sign in to comment.