Skip to content

Commit

Permalink
fix: remove useless require #8
Browse files Browse the repository at this point in the history
  • Loading branch information
cxtom committed Apr 18, 2019
1 parent 024fa28 commit 2d73026
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 54 deletions.
79 changes: 26 additions & 53 deletions src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1930,7 +1930,32 @@ export function emitFile(
const moduleName = getImportModuleName(node);
const moduleIt = state.modules[moduleName];

if (moduleIt && !moduleIt.required) {
let validImportMember = false;

const isType = node => {
const {symbol} = typeChecker.getDeclaredTypeOfSymbol(node.symbol);
if (!symbol) {
validImportMember = true;
}
const symbolFlags = symbol.getFlags();
if (
symbolFlags !== ts.SymbolFlags.Interface
&& symbolFlags !== ts.SymbolFlags.TypeAlias
) {
validImportMember = true;
}
}

if (node.importClause) {
if (node.importClause.name) {
isType(node.importClause.name);
}
if (node.importClause.namedBindings) {
node.importClause.namedBindings.forEachChild(isType);
}
}

if (moduleIt && !moduleIt.required && validImportMember) {
writeBase(`require_once("${moduleIt.path ? moduleIt.path : state.getModulePath(moduleName)}")`);
writeSemicolon();
writeLine();
Expand All @@ -1943,58 +1968,6 @@ export function emitFile(

// emitExpression(node.moduleSpecifier);
// writeSemicolon();

// // 暂时先不支持模块化,只允许用户引入指定库,方便在开发时有代码补全提示
// if (!node.moduleSpecifier || !node.moduleSpecifier.getText) {
// state.errors.push({
// code: 100,
// msg: '模块引入出错'
// });
// return;
// }

// const allowedModules = state.modules;
// const importModuleName = getImportModuleName(node);
// if (!allowedModules[importModuleName]) {
// state.errors.push({
// code: 100,
// msg: `模块${importModuleName}未找到`
// });
// return;
// }

// const moduleIt = allowedModules[importModuleName];

// // 需要将引入的变量跟 module 对应起来
// if (node.importClause && node.importClause.namedBindings) {
// node.importClause.namedBindings.forEachChild(child => {
// const name = getTextOfNode(child);
// state.moduleNamedImports[name] = {
// className: moduleIt.className,
// moduleName: importModuleName
// };
// });
// }

// if (moduleIt.required) {
// return;
// }

// if (moduleIt.path) {
// writeBase(`require_once("${moduleIt.path}");`);
// writeLine();
// }

// if (node.importClause && node.importClause.name) {
// let text = getTextOfNode(node.importClause.name);
// state.moduleDefaultImports[text] = {
// className: moduleIt.className,
// moduleName: importModuleName
// };
// }

// moduleIt.required = true;

}

function emitImportClause(node: ts.ImportClause) {
Expand Down
6 changes: 6 additions & 0 deletions test/features/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import {Other_Utils as Util} from '../some-utils';
import {Some_Utils, func} from '../some-utils';

import {SomeType, SomeAlias} from '../some-types';

type TplData = {
src?: string,
title?: string
Expand All @@ -14,3 +16,7 @@ tplData.title = Some_Utils.highlight('title');
tplData.title = Util.sample;

tplData.title = func() + 'aa';

const a = {
test: 'hello'
} as SomeType;
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('features', () => {

for (let i = 0; i < featureNames.length; i++) {
const featureName = featureNames[i];
// if (featureName !== 'vue') {
// if (featureName !== 'import') {
// continue;
// }
it(featureName, async function () {
Expand Down
6 changes: 6 additions & 0 deletions test/some-types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

export interface SomeType {
test: string;
}

export type SomeAlias = 1 | 2;
1 change: 1 addition & 0 deletions typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2710,6 +2710,7 @@ declare namespace ts {
interface Type {
flags: TypeFlags;
symbol: Symbol;
objectFlags: ObjectFlags;
pattern?: DestructuringPattern;
aliasSymbol?: Symbol;
aliasTypeArguments?: ReadonlyArray<Type>;
Expand Down

0 comments on commit 2d73026

Please sign in to comment.