Skip to content

Commit

Permalink
Merge pull request #34 from buehler/develop
Browse files Browse the repository at this point in the history
Release 2017-10-26
  • Loading branch information
buehler authored Oct 26, 2017
2 parents 0a74747 + 43ecda4 commit 2aaeb73
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 29 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
This package is a TypeScript and ECMAScript parser. It uses the underlying typescript parser to generate
a more or less human readable AST out of .js or .ts files.

[![Build Status](https://travis-ci.org/TypeScript-Heroes/node-typescript-parser.svg)](https://travis-ci.org/TypeScript-Heroes/node-typescript-parser)
[![Build Status](https://travis-ci.org/buehler/node-typescript-parser.svg)](https://travis-ci.org/buehler/node-typescript-parser)
[![npm](https://img.shields.io/npm/v/typescript-parser.svg?maxAge=3600)](https://www.npmjs.com/package/typescript-parser)
[![Coverage status](https://img.shields.io/coveralls/TypeScript-Heroes/node-typescript-parser.svg?maxAge=3600)](https://coveralls.io/github/TypeScript-Heroes/node-typescript-parser)
[![Coverage status](https://img.shields.io/coveralls/buehler/node-typescript-parser.svg?maxAge=3600)](https://coveralls.io/github/buehler/node-typescript-parser)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![Greenkeeper badge](https://badges.greenkeeper.io/TypeScript-Heroes/node-typescript-parser.svg)](https://greenkeeper.io/)
[![Greenkeeper badge](https://badges.greenkeeper.io/buehler/node-typescript-parser.svg)](https://greenkeeper.io/)

## How to use

Expand Down Expand Up @@ -40,7 +40,7 @@ Keep in mind, that the index'll only contain exported declarations.
## Changelog

The changelog is generated by [semantic release](https://github.com/semantic-release/semantic-release) and is located under the
[release section](https://github.com/TypeScript-Heroes/node-typescript-parser/releases).
[release section](https://github.com/buehler/node-typescript-parser/releases).

## Licence

Expand Down
64 changes: 49 additions & 15 deletions src/TypescriptParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ import {
InterfaceDeclaration,
ModuleDeclaration,
Node,
ScriptKind,
ScriptTarget,
SourceFile,
SyntaxKind,
TypeAliasDeclaration,
VariableStatement,
} from 'typescript';

import { parse } from 'path';
import { parseClass } from './node-parser/class-parser';
import { parseEnum } from './node-parser/enum-parser';
import { parseExport } from './node-parser/export-parser';
Expand All @@ -37,31 +39,39 @@ import { Resource } from './resources/Resource';
* This class is the parser of the whole extension. It uses the typescript compiler to parse a file or given
* source code into the token stream and therefore into the AST of the source. Afterwards an array of
* resources is generated and returned.
*
*
* @export
* @class TypescriptParser
*/
export class TypescriptParser {
/**
* Parses the given source into an anonymous File resource.
* Mainly used to parse source code of a document.
*
*
* @param {string} source
* @param {ScriptKind} [scriptKind=ScriptKind.TS]
* @returns {Promise<File>}
*
*
* @memberof TsResourceParser
*/
public async parseSource(source: string): Promise<File> {
return await this.parseTypescript(createSourceFile('inline.tsx', source, ScriptTarget.ES2015, true), '/');
public async parseSource(source: string, scriptKind: ScriptKind = ScriptKind.TS): Promise<File> {
return await this.parseTypescript(
createSourceFile(
'inline.tsx',
source,
ScriptTarget.ES2015,
true,
scriptKind),
'/');
}

/**
* Parses a single file into a parsed file.
*
*
* @param {string} filePath
* @param {string} rootPath
* @returns {Promise<File>}
*
*
* @memberof TsResourceParser
*/
public async parseFile(filePath: string, rootPath: string): Promise<File> {
Expand All @@ -71,28 +81,52 @@ export class TypescriptParser {

/**
* Parses multiple files into parsed files.
*
*
* @param {string[]} filePathes
* @param {string} rootPath
* @returns {Promise<File[]>}
*
*
* @memberof TsResourceParser
*/
public async parseFiles(filePathes: string[], rootPath: string): Promise<File[]> {
public async parseFiles(
filePathes: string[],
rootPath: string): Promise<File[]> {
return filePathes
.map(o => createSourceFile(o, readFileSync(o).toString(), ScriptTarget.ES2015, true))
.map((o) => {
let scriptKind: ScriptKind = ScriptKind.Unknown;
const parsed = parse(o);
switch (parsed.ext.toLowerCase()) {
case 'js':
scriptKind = ScriptKind.JS;
break;
case 'jsx':
scriptKind = ScriptKind.JSX;
break;
case 'ts':
scriptKind = ScriptKind.TS;
break;
case 'tsx':
scriptKind = ScriptKind.TSX;
break;
}
return createSourceFile(o,
readFileSync(o).toString(),
ScriptTarget.ES2015,
true,
scriptKind);
})
.map(o => this.parseTypescript(o, rootPath));
}

/**
* Parses the typescript source into the file instance. Calls .parse afterwards to
* get the declarations and other information about the source.
*
*
* @private
* @param {SourceFile} source
* @param {string} rootPath
* @returns {TsFile}
*
*
* @memberof TsResourceParser
*/
private parseTypescript(source: SourceFile, rootPath: string): File {
Expand All @@ -108,11 +142,11 @@ export class TypescriptParser {
* Recursive function that runs through the AST of a source and parses the nodes.
* Creates the class / function / etc declarations and instanciates a new module / namespace
* resource if needed.
*
*
* @private
* @param {Resource} resource
* @param {Node} node
*
*
* @memberof TsResourceParser
*/
private parse(resource: Resource, node: Node): void {
Expand Down
11 changes: 6 additions & 5 deletions test/TypescriptParser.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { readFileSync } from 'fs';
import { ScriptKind } from 'typescript';

import { ClassDeclaration } from '../src/declarations/ClassDeclaration';
import { DeclarationVisibility } from '../src/declarations/DeclarationVisibility';
Expand Down Expand Up @@ -617,7 +618,7 @@ describe('TypescriptParser', () => {
});

it('should parseSource correctly', async () => {
const parsedSource = await parser.parseSource(readFileSync(file).toString());
const parsedSource = await parser.parseSource(readFileSync(file).toString(), ScriptKind.TSX);

expect(parsedSource.usages).toMatchSnapshot();
});
Expand Down Expand Up @@ -679,7 +680,7 @@ describe('TypescriptParser', () => {
it('should parse the correct usages with "parseSource"', async () => {
const file = getWorkspaceFile(`typescript-parser/specific-cases/${testFile.filename}`);
const fileSource = readFileSync(file).toString();
const parsed = await parser.parseSource(fileSource);
const parsed = await parser.parseSource(fileSource, ScriptKind.TSX);

for (const usage of testFile.requiredUsages) {
expect(parsed.usages).toContain(usage);
Expand Down Expand Up @@ -714,7 +715,7 @@ describe('TypescriptParser', () => {

it('should parse a simple javascript file correctly with "parseSource"', async () => {
const content = readFileSync(file).toString();
const parsed = await parser.parseSource(content);
const parsed = await parser.parseSource(content, ScriptKind.JS);

expect(parsed).toMatchSnapshot();
});
Expand Down Expand Up @@ -743,7 +744,7 @@ describe('TypescriptParser', () => {

it('should parse a simple javascript react file correctly with "parseSource"', async () => {
const content = readFileSync(file).toString();
const parsed = await parser.parseSource(content);
const parsed = await parser.parseSource(content, ScriptKind.JSX);

expect(parsed).toMatchSnapshot();
});
Expand All @@ -757,7 +758,7 @@ describe('TypescriptParser', () => {
public test() {
let a = <T>() => { let b = null; };
}
}`);
}`, ScriptKind.TS);
expect(parsed).toMatchSnapshot();
});

Expand Down
16 changes: 11 additions & 5 deletions test/__snapshots__/TypescriptParser.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1492,21 +1492,29 @@ File {
"isExported": true,
"methods": Array [
MethodDeclaration {
"end": 144,
"end": 130,
"isAbstract": false,
"name": "test",
"parameters": Array [],
"start": 41,
"type": undefined,
"variables": Array [
VariableDeclaration {
"end": 144,
"end": 112,
"isConst": false,
"isExported": false,
"name": "a",
"start": 77,
"type": undefined,
},
VariableDeclaration {
"end": 109,
"isConst": false,
"isExported": false,
"name": "b",
"start": 96,
"type": undefined,
},
],
"visibility": 2,
},
Expand All @@ -1526,9 +1534,7 @@ File {
"usages": Array [
"a",
"T",
"let",
undefined,
"",
"b",
],
}
`;
Expand Down

0 comments on commit 2aaeb73

Please sign in to comment.