Skip to content
This repository has been archived by the owner on Jun 22, 2020. It is now read-only.

Commit

Permalink
fix(*): walk ES AST to map all requires
Browse files Browse the repository at this point in the history
  • Loading branch information
mattyclarkson committed Mar 18, 2018
1 parent aaa0c53 commit 95979c7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
27 changes: 15 additions & 12 deletions lib/es/File.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Program } from 'estree';
import { Node, Program } from 'estree';
import { PathLike, readFile as readFileSync, writeFile as writeFileSync } from 'fs';
import { ModuleKind } from 'typescript';
import { promisify } from 'util';
Expand All @@ -9,8 +9,7 @@ import Import from '@es/Import';
import isExportNamedDeclaration from '@es/isExportNamedDeclaration';
import isImportDeclaration from '@es/isImportDeclaration';
import isRequireCallExpression from '@es/isRequireCallExpression';
import isVariableDeclaration from '@es/isVariableDeclaration';
import { attachComments, Comment, generate, parse, plugins, Token } from '@es/parser';
import { attachComments, Comment, generate, parse, plugins, Token, traverse } from '@es/parser';
import Require from '@es/Require';
import Base, { IDerivedOptions as IBaseOptions } from '@lib/File';

Expand Down Expand Up @@ -59,17 +58,21 @@ export default class File extends Base<Import | Require, Export> {
}

async *imports(): AsyncIterableIterator<Import | Require> {
const { body } = await this.ast;
yield* body
const ast = await this.ast;

yield* ast.body
.filter(isImportDeclaration)
.map(declaration => new Import({file: this, declaration}));
yield* body
.filter(isVariableDeclaration)
.map(({declarations}) => declarations)
.reduce((a, n) => a.concat(n), [])
.map(({init}) => init)
.filter(isRequireCallExpression)
.map(declaration => new Require({file: this, declaration}));

const requires: Array<Require> = [];
traverse(ast, {
enter: (declaration: Node) => {
if (isRequireCallExpression(declaration)) {
requires.push(new Require({file: this, declaration}));
}
}
});
yield* requires;
}

async *exports(): AsyncIterableIterator<Export> {
Expand Down
1 change: 1 addition & 0 deletions lib/es/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ export type Token = acorn.Token;
export const parse = acorn.parse;
export const generate = escodegen.generate;
export const attachComments = estraverse.attachComments;
export const traverse = estraverse.traverse;
export { plugins };

0 comments on commit 95979c7

Please sign in to comment.