Skip to content

Commit

Permalink
fix: imported function report unsupport
Browse files Browse the repository at this point in the history
  • Loading branch information
meixg committed Oct 15, 2020
1 parent 2e0842c commit 010ef00
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 23 deletions.
33 changes: 14 additions & 19 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ import {
isClassInstance,
isFunctionLike,
isVariable,
shouldUseReference
shouldUseReference,
isFromImport
} from './utilities/nodeTest';

import {CompilerState} from './types';
Expand Down Expand Up @@ -3059,25 +3060,19 @@ 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;
}
}

if (!(ts.isIdentifier(node) && isFromImport(node, typeChecker))) {
return false;
}

const symbol = typeChecker.getSymbolAtLocation(node);
const declarations = symbol.getDeclarations();
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() {
Expand Down
11 changes: 7 additions & 4 deletions src/features/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author cxtom(cxtom2008@gmail.com)
*/

import {
import ts, {
EmitHint,
isCallExpression,
isPropertyAccessExpression,
Expand All @@ -26,6 +26,8 @@ import {
createDiagnostic,
getUnSupportedMessage
} from '../utilities/error';
import { isFromImport } from '../utilities/nodeTest';
import { CompilerState } from '../types';

const map = {
parseInt: method('intval', {
Expand Down Expand Up @@ -75,8 +77,9 @@ const isDynamicImport = node => isCallExpression(node) && node.expression.kind =

export default {

emit(hint, node, state) {
emit(hint: ts.EmitHint, node: ts.Node, state: CompilerState) {

// @ts-ignore
const expNode = node.expression;
let func;

Expand All @@ -87,7 +90,7 @@ export default {
if (func) {
return func(node, helpers, {helperNamespace});
}
if (unSupportedGlobalMethods.has(expNode.escapedText)) {
if (!isFromImport(expNode, state.typeChecker) && unSupportedGlobalMethods.has(expNode.escapedText)) {
state.errors.push(createDiagnostic(
node, state.sourceFile,
getUnSupportedMessage(expNode.escapedText)
Expand Down Expand Up @@ -141,7 +144,7 @@ export default {
}

if (isDynamicImport(node)) {
const argu = node.arguments[0];
const argu = (node as ts.CallExpression).arguments[0];
if (isStringLiteral(argu)) {
const moduleName = argu.text;
const moduleIt = modules[moduleName];
Expand Down
10 changes: 10 additions & 0 deletions src/utilities/nodeTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,13 @@ export function isRegExp(node: ts.Node, typeChecker: ts.TypeChecker) {
const nodeType = typeChecker.getTypeAtLocation(node);
return nodeType.symbol?.getEscapedName() === 'RegExp';
}

export function isFromImport(node: ts.Node, typeChecker: ts.TypeChecker) {
const symbol = typeChecker.getSymbolAtLocation(node);
if (!symbol) {
return false;
}

const declarations = symbol.getDeclarations();
return declarations.length && ts.isImportSpecifier(declarations[0]);
}
13 changes: 13 additions & 0 deletions test/features/GlobalApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ const h = __filename;
```php
$g = dirname(__FILE__);
$h = __FILE__;
```

## escape

```ts
import {escape} from './helper/export';

escape();
```

```php
require_once(dirname(__FILE__) . '/' . "./helper/export.php");
\someModule\escape();
```
2 changes: 2 additions & 0 deletions test/features/helper/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export default function run(a: string) {
}

export {Some_Utils} from './some-utils';

export function escape() {}

0 comments on commit 010ef00

Please sign in to comment.