Skip to content
Permalink
Browse files

fix(file): Prevent emit error when try to read file

  • Loading branch information...
iamthes committed Dec 13, 2017
1 parent ef41c93 commit 08c1390884bf7e6ec48b9949662144c6b7d55d8f
Showing with 30 additions and 8 deletions.
  1. +2 −0 fixtures/lead-to-unknown.ts
  2. +1 −1 package.json
  3. +5 −0 src/file.spec.ts
  4. +10 −4 src/file.ts
  5. +9 −0 src/module.spec.ts
  6. +2 −2 src/module.ts
  7. +1 −1 src/parse.ts
@@ -0,0 +1,2 @@
export * from 'fs';
export * from './foo';
@@ -5,7 +5,7 @@
"main": "lib",
"typings": "src/index.ts",
"scripts": {
"fixtures": "npm i --no-save angular2-calendar rxjs gulp-tslint @angular/core @types/express",
"fixtures": "npm i --no-save angular2-calendar rxjs gulp-tslint @angular/core @types/express @types/fs-extra",
"pretest": "npm run fixtures",
"test": "npm run eslint && npm run tscheck && npm run t",
"t": "npm run mocha -- src/*.spec.ts",
@@ -14,3 +14,8 @@ it('import all should contain name', async () => {
const [x2] = result.filter(m => m.name === 'x2');
assert.equal(x2.filepath, normalize(`${rootPath}/fixtures/component/index.ts`));
});

it('try to parse unexisting file', async () => {
const result = await file(`${rootPath}/fixtures/lead-to-unknown.ts`);
assert(result.length === 0);
});
@@ -34,18 +34,24 @@ export function file(path: string, options: FileOptions = {}): Promise<Entry[]>
}
const basedir = dirname(filepath);
const promises = unnamed.map(m => {
return new Promise<Entry[]>((done, reject) => {
return new Promise<Entry[]>((done) => {
if (!m.specifier) {
return done([]);
done([]);
return;
}
resolve(m.specifier, { ...resolveOptions, basedir }, (err, resolved) => {
// Supress error, file do not exists or not readable
// E.g. export * from "fs"
if (err) {
return reject(err);
done([]);
return;
}
if (resolved) {
return file(resolved, options).then(items => {
done(items);
}, reject);
}, () => {
done([]);
});
}
done([]);
});
@@ -104,3 +104,12 @@ it('types express', async () => {
assert(request);
assert(request.module === 'express');
});

it('types fs-extra', async () => {
const result = await parse('@types/fs-extra', { basedir: rootPath });
assert(result.length > 0);
const [copy] = result.filter(m => m.name === 'copy');
assert(copy);
const [copyOptions] = result.filter(m => m.name === 'CopyOptions');
assert(copyOptions);
});
@@ -116,9 +116,9 @@ export function module(id: string, options: ModuleOptions = {}): Promise<Entry[]
return entries;
});
}).catch(err => {
if (err && err.code === 'MODULE_NOT_FOUND') {
if (err && (err.code === 'MODULE_NOT_FOUND' || err.code === 'ENOENT')) {
return Promise.resolve([]);
}
return err;
return Promise.reject(err);
});
}
@@ -59,7 +59,7 @@ export function parse(sourceText: string, options: ParseOptions = {}): Entry[] {
walk(sourceFile);
function walk(statement: ts.Node) {
const node = statement;
if (node.pos >= moduleEnd) {
if (node.pos >= moduleEnd!) {
module = options.module;
moduleName = undefined;
moduleEnd = undefined;

0 comments on commit 08c1390

Please sign in to comment.
You can’t perform that action at this time.