Skip to content
Permalink
Browse files

feat(core): Can detect export default identifier

  • Loading branch information...
iamthes committed Feb 22, 2018
2 parents 3caff57 + 571e5de commit 6c537a1317899561092505a481e09f8d9d5df92d
Showing with 36 additions and 5 deletions.
  1. +2 −1 package.json
  2. +5 −0 src/module.spec.ts
  3. +22 −0 src/parse.spec.ts
  4. +7 −4 src/parse.ts
@@ -5,14 +5,15 @@
"main": "lib",
"typings": "src/index.ts",
"scripts": {
"fixtures": "npm i --no-save angular2-calendar rxjs gulp-tslint @angular/core @types/express @types/fs-extra",
"fixtures": "npm i --no-save angular2-calendar rxjs gulp-tslint @angular/core @types/express @types/fs-extra preact",
"pretest": "npm run fixtures",
"test": "npm run eslint && npm run tscheck && npm run t",
"t": "npm run mocha -- src/*.spec.ts",
"test:r": "npm run mocha -- src/*.spec.ts",
"mocha": "ts-node -F node_modules/mocha/bin/_mocha",
"test:w": "npm run mocha -- --watch-extensions ts --watch src/**/*.spec.ts",
"test:d": "node --inspect-brk node_modules/ts-node/dist/_bin.js -F node_modules/mocha/bin/_mocha --no-timeouts src/**/*.spec.ts",
"test:dd": "node --inspect node_modules/ts-node/dist/_bin.js -F node_modules/mocha/bin/_mocha --watch --no-timeouts src/**/*.spec.ts",
"tscheck": "echo tscheck... && tsc --noEmit",
"tscheck:w": "npm run tscheck -- --watch",
"tsclint": "tsc --noEmit --pretty false --strictNullChecks --forceConsistentCasingInFileNames --noImplicitReturns --noImplicitThis --noUnusedLocals --noUnusedParameters",
@@ -113,3 +113,8 @@ it('types fs-extra', async () => {
const [copyOptions] = result.filter(m => m.name === 'CopyOptions');
assert(copyOptions);
});

it('preact', async () => {
const result = await parse('preact', { basedir: rootPath });
assert(result.length > 0);
});
@@ -77,6 +77,13 @@ it('export default', () => {
assert.equal(entry.isDefault, true);
});

it('export default var', () => {
const code = `export default component`;
const [entry] = parse(code);
assert.equal(entry.name, 'component');
assert.equal(entry.isDefault, true);
});

it('empty source', () => {
const code = ``;
const result = parse(code);
@@ -162,3 +169,18 @@ export = e;
assert.equal(result[0].name, 'Request');
assert.equal(result[0].cjs, true);
});

it('preact declaration', () => {
const source = `
declare namespace preact {
function rerender();
type AnyComponent = {};
}
declare module "preact" {
export = preact;
}
`;
const result = parse(source);
assert(result[0].name === 'rerender');
assert(result[1].name === 'AnyComponent');
});
@@ -74,7 +74,7 @@ export function parse(sourceText: string, options: ParseOptions = {}): Entry[] {
if (moduleName) {
if (resolve.isCore(moduleName)) {
module = moduleName;
} else {
} else if (!Array.isArray(moduleBlockDeclarations[moduleName])) {
moduleBlockDeclarations[moduleName] = [];
}
}
@@ -116,16 +116,19 @@ export function parse(sourceText: string, options: ParseOptions = {}): Entry[] {
entries.forEach(entry => entrySet.push(entry));
} break;
case ts.SyntaxKind.ExportAssignment: {
const expr = (node as any).expression && (node as any).expression.text;
const declarations = expr && moduleBlockDeclarations && moduleBlockDeclarations[expr];
const expression = (node as any).expression;
const text = expression && expression.text;
const declarations = text && moduleBlockDeclarations && moduleBlockDeclarations[text];
if (Array.isArray(declarations)) {
declarations.forEach(entry => {
entry.cjs = true;
entry.ts = true;
entrySet.push(entry);
});
} else {
} else if (module) {
entrySet.result.push(new Entry({ module, cjs: true, ts: true }));
} else if (expression && expression.kind === ts.SyntaxKind.Identifier && text) {
entrySet.result.push(new Entry({ name: text, module, filepath, isDefault: true }));
}
} break;
}

0 comments on commit 6c537a1

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