Skip to content
Permalink
Browse files

refactor: Renamed parse function to main

BREAKING CHANGE: Renamed parse function to main
  • Loading branch information...
roman.vasilev
roman.vasilev committed Feb 14, 2019
1 parent 47815d2 commit c495275d8aa5c65bcfc4ff9fd5862c231586a9af
Showing with 70 additions and 987 deletions.
  1. +0 −56 src/commonjs.spec.ts
  2. +0 −46 src/directory.spec.ts
  3. +0 −78 src/directory.ts
  4. +0 −13 src/entry.spec.ts
  5. +1 −1 src/entry.ts
  6. +0 −1 src/file-extensions.ts
  7. +0 −21 src/file.spec.ts
  8. +0 −66 src/file.ts
  9. +0 −13 src/fixtures.spec.ts
  10. +0 −1 src/get.ts
  11. +22 −0 src/index.spec.ts
  12. +47 −4 src/index.ts
  13. +0 −141 src/module.spec.ts
  14. +0 −151 src/module.ts
  15. +0 −230 src/parse.spec.ts
  16. +0 −165 src/parse.ts

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -50,6 +50,6 @@ export class Entry {
}

id() {
return `${this.name || this.counter}${this.isDefault ? '*' : '' }/${this.module ? this.module : this.filepath}`;
return `${this.name || this.counter}${this.isDefault ? '*' : ''}/${this.module ? this.module : this.filepath}`;
}
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,22 @@
import * as assert from 'assert';
import { parse } from '.';

it('smoke test', () => {
assert(parse);
});

it.only('var export', () => {
const [result] = parse(`export var test = 1`);
assert(result.name === 'test');
});

it('declare module', () => {
const result = parse(`
declare module "fs" {
namespace access {
foo();
}
function readFile();
}
`);
});
@@ -1,4 +1,47 @@
export { directory } from './directory';
export { module } from './module';
export { file } from './file';
export { parse } from './parse';
import * as ts from 'typescript';
import { Entry } from './entry';

type WalkNodeOptions = {
module?: string;
declaredModule?: string;
};

function walkNode(node: ts.Node, options: WalkNodeOptions = {}): any {
debugger;
// console.log("node.kind", node.kind);
if (node.kind === ts.SyntaxKind.Identifier) {
const name = (node as ts.Identifier).escapedText;
} else if (node.kind === ts.SyntaxKind.ModuleDeclaration) {
const children = node.getChildren();
const moduleBlock = children.find(c => c.kind === ts.SyntaxKind.ModuleBlock) as ts.Block | undefined;
if (!moduleBlock) {
return;
}
let declaredModule = (node as any).name.text;
return moduleBlock
.statements.filter((st: any) => st.name && ts.isIdentifier(st.name))
.map((node: any) => node.name.text)
.map(name => new Entry({ ...options, name, module: declaredModule }));
}
ts.forEachChild(node, walkNode);
}

export function main(sourceText: string, options: WalkNodeOptions = {}) {
const sourceFile = ts.createSourceFile('dummy.ts', sourceText, ts.ScriptTarget.ESNext, true);
let result = [];
ts.forEachChild<ts.Node>(sourceFile, (node: any) => {
result = walkNode(node, options);
return undefined;
});
return result;
}

function hasParentOf(node: ts.Node, syntaxKinds: ts.SyntaxKind[]) {
while (true) {
if (node && node.parent && syntaxKinds.includes(node.parent.kind)) {
return true;
}
node = node.parent;
}
return false;
}
Oops, something went wrong.

0 comments on commit c495275

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