Skip to content

Commit

Permalink
fix: #75
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Nov 6, 2019
1 parent 1559fb0 commit aa73942
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 29 deletions.
61 changes: 40 additions & 21 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1227,25 +1227,7 @@ export function emitFile(
writeBase("$");
}

let writeNamespace = false;
if (ts.isIdentifier(node.expression)) {
const symbol = typeChecker.getSymbolAtLocation(node.expression);

if (symbol) {
const declarations = symbol.getDeclarations();

if (declarations.length && ts.isImportSpecifier(declarations[0])) {
const specifier = declarations[0] as ts.ImportSpecifier;
const declaration = specifier.parent.parent.parent as ts.ImportDeclaration;
const moduleName = declaration.moduleSpecifier.getText().replace(/^['"]/, '').replace(/['"]$/, '');
const namespace = state.modules[moduleName] && state.modules[moduleName].namespace;
namespace && writeBase(namespace);
writeNamespace = true;
emitExpression(specifier.propertyName || specifier.name)
}
}

}
let writeNamespace = emitIdentifierFromImport(node.expression);
if (!writeNamespace) {
emitWithHint(ts.EmitHint.Expression, node.expression);
}
Expand Down Expand Up @@ -2378,7 +2360,17 @@ export function emitFile(
writeSpace();
writePunctuation("=>");
writeSpace();
emitExpression(node.initializer);
if (isFunctionLike(node.initializer, typeChecker) && !isVariable(node.initializer, typeChecker) && !ts.isFunctionLikeDeclaration(node.initializer)) {
writeBase('"');
let fromImport = emitIdentifierFromImport(node.initializer);
if (!fromImport) {
emit(node.initializer);
}
writeBase('"');
}
else {
emitExpression(node.initializer);
}
}

function emitShorthandPropertyAssignment(node: ts.ShorthandPropertyAssignment) {
Expand All @@ -2398,7 +2390,8 @@ export function emitFile(

if (isFunctionLike(node, typeChecker) && !isVariable(node, typeChecker)) {
writeBase('"');
emit(node.name);
let fromImport = emitIdentifierFromImport(node.name);
!fromImport && emit(node.name);
writeBase('"');
}
else {
Expand Down Expand Up @@ -3019,6 +3012,32 @@ export function emitFile(
}
}


/**
* identifier from import may need add namespace
*/
function emitIdentifierFromImport(node: ts.Node): boolean {
if (ts.isIdentifier(node)) {
const type = typeChecker.getTypeAtLocation(node);
const symbol = typeChecker.getSymbolAtLocation(node);

if (symbol) {
const declarations = symbol.getDeclarations();

if (declarations.length && ts.isImportSpecifier(declarations[0])) {
const specifier = declarations[0] as ts.ImportSpecifier;
const declaration = specifier.parent.parent.parent as ts.ImportDeclaration;
const moduleName = declaration.moduleSpecifier.getText().replace(/^['"]/, '').replace(/['"]$/, '');
const namespace = state.modules[moduleName] && state.modules[moduleName].namespace;
namespace && writeBase(namespace);
emitExpression(specifier.propertyName || specifier.name);
return true;
}
}

}
}

// function commitPendingSemicolonInternal() {
// if (pendingSemicolon) {
// writeSemicolonInternal();
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/nodeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function shouldAddDollar(node: Node, state: CompilerState): boolean {
return false;
}

if (isFunctionLike(node, state.typeChecker) && node.parent && ts.isImportSpecifier(node.parent)) {
if (isFunctionLike(node, state.typeChecker) && !isVariable(node, state.typeChecker)) {
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion test/features/Class.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace test\case_Class;
require_once(dirname(__FILE__) . '/' . "../some-utils.php");
use \Base;
use \someModule\Base;
class Article extends Base {
public $title;
public $id;
Expand Down
8 changes: 7 additions & 1 deletion test/features/ObjectLiteralExpression.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
namespace test\case_ObjectLiteralExpression;
require_once(dirname(__FILE__) . '/' . "../some-utils.php");
$b = array(
"a" => 123,
"b" => "456"
Expand Down Expand Up @@ -37,7 +38,12 @@ function aaa() {
echo "ccc";
},
"ddd" => $ddd,
"eee" => $eee
"eee" => $eee,
"fff" => "aaa",
"ggg" => $bbb,
"hhh" => $ddd,
"iii" => $eee,
"jjj" => "\someModule\func"
);
$mmm["aaa"]();
$mmm["bbb"]();
Expand Down
9 changes: 8 additions & 1 deletion test/features/ObjectLiteralExpression.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {func as func1} from '../some-utils';

const b = {
a: 123,
b: '456'
Expand Down Expand Up @@ -44,7 +46,12 @@ let mmm = {
console.log('ccc');
},
ddd,
eee
eee,
fff: aaa,
ggg: bbb,
hhh: ddd,
iii: eee,
jjj: func1
};

mmm.aaa();
Expand Down
6 changes: 3 additions & 3 deletions test/features/import.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
namespace test\case_import;
require_once(dirname(__FILE__) . '/' . "../some-utils.php");
use \Other_Utils as Util;
use \Some_Utils;
use \someModule\Other_Utils as Util;
use \someModule\Some_Utils;
$tplData = array();
$tplData["src"] = Some_Utils::makeTcLink("url");
$tplData["title"] = Some_Utils::highlight("title");
$tplData["title"] = Util::$sample;
$tplData["title"] = \func() . "aa";
$tplData["title"] = \someModule\func() . "aa";
$a = array(
"test" => "hello"
);
Expand Down
2 changes: 1 addition & 1 deletion test/features/inheritedVariables.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
namespace test\case_inheritedVariables;
require_once(dirname(__FILE__) . '/' . "./Class.php");
use \Article as Art;
use \someModule\Article as Art;
$a = array(
"b" => "123456"
);
Expand Down
3 changes: 3 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ describe('features', () => {
'vue': {
required: true
}
},
getModuleNamespace(name) {
return '\\someModule\\';
}
});
assert.equal(res.phpCode, phpContent);
Expand Down
1 change: 1 addition & 0 deletions typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4040,6 +4040,7 @@ declare namespace ts {
function isPropertyName(node: Node): node is PropertyName;
function isBindingName(node: Node): node is BindingName;
function isFunctionLike(node: Node): node is SignatureDeclaration;
function isFunctionLikeDeclaration(node: Node): node is FunctionLikeDeclaration;
function isClassElement(node: Node): node is ClassElement;
function isClassLike(node: Node): node is ClassLikeDeclaration;
function isAccessor(node: Node): node is AccessorDeclaration;
Expand Down

0 comments on commit aa73942

Please sign in to comment.