Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for flow type imports #21

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/findExports.ts
Original file line number Diff line number Diff line change
@@ -239,6 +239,7 @@ function findNamedExports(
let defaultName = null;
nodes.forEach((node) => {
result.push(...findESNamedExports(node));
result.push(...findNamedTypeExports(node).map(name => `type ${name}`));
const named = findCommonJSExports(node, {
definedNames,
absolutePathToFile,
@@ -332,6 +333,13 @@ function findRootNodes(ast) {
return realRootNodes;
}

function findNamedTypeExports(node) {
if (node.type !== 'ExportNamedDeclaration' || !node.declaration || node.declaration.type !== 'TypeAlias') {
return [];
}
return [node.declaration.id.name];
}

export default function findExports(data, absolutePathToFile) {
if (/\.json$/.test(absolutePathToFile)) {
return {
12 changes: 9 additions & 3 deletions src/importCompletion.ts
Original file line number Diff line number Diff line change
@@ -34,16 +34,22 @@ export class ImportCompletion implements vscode.CompletionItemProvider {
const handlers = [];
items.forEach(item => {
handlers.push({
label: item.importObj.module.name,
label: item.importObj.module.name.replace('type ', ''),
kind: vscode.CompletionItemKind.Reference,
detail: item.label,
documentation: '',
command: autofix ? { title: 'Autocomplete', command: 'extension.fixImport', arguments: [item.importObj, item.doc, item.range] } : null
command: autofix
? {
title: 'Autocomplete',
command: 'extension.fixImport',
arguments: [item.importObj, item.doc, item.range]
}
: null
});
});
return resolve(handlers);
}
return resolve([]);
})
});
}
}
14 changes: 9 additions & 5 deletions src/interpreter.ts
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@ export default class Interpreter {
exports.default\\s*=\\s*(\\w+).default
|module.exports\\s*=\\s*(\\w+)
|exports\\[[\\'\\"]default[\\'\\"]\\]\\s*=\\s*(\\w+)
|export\\s+(default\\s+){0,1}(?:const|let|var|interface|enum|async\\s+function|function|function\\*|class|abstract\\sclass)\\s+([\\w]+)
|export\\s+(default\\s+){0,1}(?:const|let|var|interface|type|enum|async\\s+function|function|function\\*|class|abstract\\sclass)\\s+([\\w]+)
|exports\\.([\\w]+)\\s*=
|exports\\[\\"([\\w]+)\\"\\]\\s*=
|Object.defineProperty\\(\\s*exports\\s*,\\s*[\\'|\\"]([\\w]+)[\\'|\\"]
@@ -72,14 +72,18 @@ export default class Interpreter {
if (res[4] != null) {
resultList.push({
default: true,
name: res[5],
})
name: res[0] && res[0].startsWith('export default type') ? `type ${res[5]}` : res[5]
});
continue;
}
for (i = 5; i <= 8; i+=1) {
for (i = 5; i <= 8; i += 1) {
if (res[i] != null) {
if (!this.isUnwantedName(res[i]) && !nameList.includes(res[i])) {
nameList.push(res[i]);
if (res[0] && res[0].startsWith('export type')) {
nameList.push(`type ${res[i]}`);
} else {
nameList.push(res[i]);
}
}
break;
}
11 changes: 7 additions & 4 deletions src/rootScanner.ts
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ export interface ImportObj {
* should add member to import statement like import 'file.less'
*/
isNotMember?: boolean;
type?: boolean;
};
isNodeModule: boolean;
}
@@ -224,11 +225,13 @@ export default class RootScanner {
}

private cacheModulesFromMain(moduleName, modulePath, packageJson) {
if (!packageJson.hasOwnProperty('main'))
return;
let mainFilePath = path.join(modulePath, packageJson.main);
let mainFilePath = path.join(modulePath, 'src/index.js');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there maybe no src/index.js in node_modules in most of case, src directory will ignore in npmignore .

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is actually a common pattern with flow types to include the src directory when publishing to npm. This is how flow will resolve types in node_modules.

if (!fs.existsSync(mainFilePath)) {
mainFilePath += '.js';
if (!packageJson.hasOwnProperty('main')) return;
mainFilePath = path.join(modulePath, packageJson.main);
if (!fs.existsSync(mainFilePath)) {
mainFilePath += '.js';
}
}
if (fs.existsSync(mainFilePath)) {
fs.readFile(mainFilePath, 'utf-8', (err, data) => {
1 change: 0 additions & 1 deletion test/importFixer.test.ts
Original file line number Diff line number Diff line change
@@ -92,7 +92,6 @@ suite("extractImportFromRoot", () => {
isPlainFile: true
}
});
console.log(obj)
assert.equal('./index.less', importFixer.extractImportFromRoot(obj, path.join(vscode.workspace.rootPath, 'src/app.js')))
});