Skip to content

Commit bc18352

Browse files
authored
fix(exports): Add exported members to usage (#52)
When elements are directly re-exported, they should appear in the usages. Relates to buehler/typescript-hero#380.
1 parent c0ee825 commit bc18352

File tree

16 files changed

+162
-90
lines changed

16 files changed

+162
-90
lines changed

.appveyor.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@ version: "{build} - {branch}"
22
skip_tags: true
33
skip_branch_with_pr: true
44

5+
matrix:
6+
fast_finish: true
7+
58
environment:
69
matrix:
10+
- nodejs_version: "9"
711
- nodejs_version: "8"
8-
- nodejs_version: "7"
9-
- nodejs_version: "6"
1012

1113
install:
1214
- ps: Install-Product node $env:nodejs_version
1315
- npm install
1416

1517
test_script:
1618
- npm test
19+
- npm install -g codecov
20+
- codecov
1721

1822
build: off

.travis.yml

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,38 @@
1-
sudo: false
21
language: node_js
32

4-
cache:
5-
directories:
6-
- node_modules
3+
stages:
4+
- name: test
5+
if: tag IS blank
6+
- name: deploy
7+
if: branch = master AND type != pull_request
8+
9+
matrix:
10+
fast_finish: true
711

812
notifications:
913
email: false
1014

11-
node_js:
12-
- '8'
13-
- '7'
14-
- '6'
15-
16-
before_script:
17-
- npm prune
18-
19-
after_success:
20-
- npm install coveralls@^2.11.9 && cat ./coverage/lcov.info | coveralls
21-
22-
after_script:
23-
- npm install
24-
- npm run build
25-
- npm run semantic-release
26-
27-
branches:
28-
except:
29-
- /^v\d+\.\d+\.\d+$/
15+
jobs:
16+
include:
17+
- stage: test
18+
node_js: '9'
19+
after_success:
20+
- npm i -g codecov
21+
- codecov
22+
- stage: test
23+
node_js: '8'
24+
after_success:
25+
- npm i -g codecov
26+
- codecov
27+
- stage: deploy
28+
node_js: '9'
29+
script: npm run typedoc
30+
deploy:
31+
provider: pages
32+
skip_cleanup: true
33+
github_token: $GH_TOKEN
34+
local_dir: ./docs
35+
- stage: deploy
36+
node_js: '9'
37+
before_script: npm run build
38+
script: npm run semantic-release

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ a more or less human readable AST out of .js or .ts files.
66
[![Build Status](https://travis-ci.org/buehler/node-typescript-parser.svg)](https://travis-ci.org/buehler/node-typescript-parser)
77
[![Build Status Windows](https://ci.appveyor.com/api/projects/status/j06bqjc4tkdt7sej?svg=true)](https://ci.appveyor.com/project/buehler/node-typescript-parser)
88
[![npm](https://img.shields.io/npm/v/typescript-parser.svg?maxAge=3600)](https://www.npmjs.com/package/typescript-parser)
9-
[![Coverage status](https://img.shields.io/coveralls/buehler/node-typescript-parser.svg?maxAge=3600)](https://coveralls.io/github/buehler/node-typescript-parser)
9+
[![codecov](https://codecov.io/gh/buehler/node-typescript-parser/branch/master/graph/badge.svg)](https://codecov.io/gh/buehler/node-typescript-parser)
1010
[![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)
1111
[![Greenkeeper badge](https://badges.greenkeeper.io/buehler/node-typescript-parser.svg)](https://greenkeeper.io/)
1212
[![Gitter](https://img.shields.io/gitter/room/node-typescript-parser/Lobby.svg)](https://gitter.im/node-typescript-parser/Lobby)
@@ -38,10 +38,10 @@ After the parsing is done, you'll get an index with resolved
3838
exports and declarations.
3939

4040
Keep in mind, that the index'll only contain exported declarations.
41-
41+
4242
## Changelog
4343

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

4747
## Licence

jest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"collectCoverage": true,
3-
"mapCoverage": true,
43
"transform": {
54
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
65
},

package.json

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@
3131
},
3232
"homepage": "https://github.com/TypeScript-Heroes/node-typescript-parser#readme",
3333
"devDependencies": {
34+
"@smartive/tslint-config": "^2.0.0",
35+
"@types/jest": "^22.1.4",
3436
"@types/lodash-es": "^4.17.0",
35-
"@types/jest": "^21.1.8",
3637
"@types/mock-fs": "^3.6.30",
37-
"@types/node": "^8.0.57",
38+
"@types/node": "^9.4.6",
3839
"del-cli": "^1.1.0",
39-
"jest": "^21.2.1",
40+
"jest": "^22.4.2",
4041
"mock-fs": "^4.4.2",
41-
"semantic-release": "^9.0.0",
42-
"ts-jest": "^21.2.4",
43-
"tslint": "^5.8.0",
44-
"tslint-config-airbnb": "^5.4.2",
45-
"tsutils": "^2.13.0",
46-
"typedoc": "^0.9.0"
42+
"semantic-release": "^15.0.0",
43+
"ts-jest": "^22.4.0",
44+
"tslint": "^5.9.1",
45+
"tsutils": "^2.22.0",
46+
"typedoc": "^0.10.0"
4747
},
4848
"dependencies": {
4949
"lodash": "^4.17.5",
5050
"lodash-es": "^4.17.5",
51-
"tslib": "^1.8.1",
52-
"typescript": "^2.6.2"
51+
"tslib": "^1.9.0",
52+
"typescript": "^2.7.2"
5353
}
5454
}

src/code-generators/typescript-generators/namedImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { NamedImport } from '../../imports/NamedImport';
22
import { SymbolSpecifier } from '../../SymbolSpecifier';
33
import { stringTemplate } from '../../utilities/StringTemplate';
4-
import { TypescriptGenerationOptions, MultiLineImportRule } from '../TypescriptGenerationOptions';
4+
import { MultiLineImportRule, TypescriptGenerationOptions } from '../TypescriptGenerationOptions';
55
import { generateSymbolSpecifier } from './symbolSpecifier';
66

77
const oneLinerImportTemplate = stringTemplate`import ${0} from ${1}`;

src/node-parser/class-parser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import { Resource } from '../resources/Resource';
2020
import {
2121
isArrayBindingPattern,
2222
isConstructorDeclaration,
23+
isGetAccessorDeclaration,
2324
isIdentifier,
2425
isMethodDeclaration,
2526
isObjectBindingPattern,
2627
isPropertyDeclaration,
27-
isGetAccessorDeclaration,
2828
isSetAccessorDeclaration,
2929
} from '../type-guards/TypescriptGuards';
3030
import { parseFunctionParts, parseMethodParams } from './function-parser';

src/node-parser/export-parser.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import { getDefaultResourceIdentifier } from './parse-utilities';
1111

1212
/**
1313
* Parses an export node into the declaration.
14-
*
14+
*
1515
* @export
1616
* @param {Resource} resource
1717
* @param {(ExportDeclaration | ExportAssignment)} node
1818
*/
1919
export function parseExport(resource: Resource, node: ExportDeclaration | ExportAssignment): void {
2020
if (isExportDeclaration(node)) {
2121
const tsExport = node as ExportDeclaration;
22-
if (!isStringLiteral(tsExport.moduleSpecifier)) {
22+
if (!isStringLiteral(tsExport.moduleSpecifier) && !tsExport.exportClause) {
2323
return;
2424
}
2525
if (tsExport.getText().indexOf('*') > -1) {
@@ -30,22 +30,38 @@ export function parseExport(resource: Resource, node: ExportDeclaration | Export
3030
);
3131
} else if (tsExport.exportClause && isNamedExports(tsExport.exportClause)) {
3232
const lib = tsExport.moduleSpecifier as StringLiteral;
33-
const ex = new NamedExport(node.getStart(), node.getEnd(), lib.text);
33+
const ex = new NamedExport(
34+
node.getStart(),
35+
node.getEnd(),
36+
lib ? lib.text : getDefaultResourceIdentifier(resource),
37+
);
3438

3539
ex.specifiers = tsExport.exportClause.elements.map(
3640
o => o.propertyName && o.name ?
3741
new SymbolSpecifier(o.propertyName.text, o.name.text) :
3842
new SymbolSpecifier(o.name.text),
3943
);
4044

45+
for (const spec of ex.specifiers) {
46+
if (resource.usages.indexOf(spec.alias || spec.specifier) === -1) {
47+
resource.usages.push(spec.alias || spec.specifier);
48+
}
49+
}
50+
4151
resource.exports.push(ex);
4252
}
4353
} else {
4454
const literal = node.expression as Identifier;
4555
if (node.isExportEquals) {
4656
resource.exports.push(new AssignedExport(node.getStart(), node.getEnd(), literal.text, resource));
57+
if (resource.usages.indexOf(literal.text) === -1) {
58+
resource.usages.push(literal.text);
59+
}
4760
} else {
4861
const name = (literal && literal.text) ? literal.text : getDefaultResourceIdentifier(resource);
62+
if (resource.usages.indexOf(name) === -1) {
63+
resource.usages.push(name);
64+
}
4965
resource.declarations.push(new DefaultDeclaration(name, resource));
5066
}
5167
}

src/resources/File.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { Resource } from './Resource';
1111

1212
/**
1313
* TypeScript resource. Basically a file that is located somewhere.
14-
*
14+
*
1515
* @export
1616
* @class File
1717
* @implements {Resource}
@@ -32,15 +32,16 @@ export class File implements Resource, Node {
3232
return this.usages
3333
.filter(usage =>
3434
!this.declarations.some(o => o.name === usage) &&
35-
!this.resources.some(o => (o instanceof Module || o instanceof Namespace) && o.name === usage))
35+
!this.resources.some(o => (o instanceof Module || o instanceof Namespace) && o.name === usage),
36+
)
3637
.concat(
37-
this.resources.reduce((all, cur) => all.concat(cur.nonLocalUsages), [] as string[]),
38+
this.resources.reduce((all, cur) => all.concat(cur.nonLocalUsages), [] as string[]),
3839
);
3940
}
4041

4142
/**
4243
* Returns the parsed path of a resource.
43-
*
44+
*
4445
* @readonly
4546
* @type {ParsedPath}
4647
* @memberof File
@@ -51,7 +52,7 @@ export class File implements Resource, Node {
5152

5253
/**
5354
* Determines if a file is a workspace file or an external resource.
54-
*
55+
*
5556
* @readonly
5657
* @type {boolean}
5758
* @memberof File

test/SpecificUsageCases.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,22 @@ describe('Specific usage cases', () => {
2020

2121
});
2222

23+
describe('get usage for directly reexported elements', () => {
24+
25+
it('should contain imported elements in usages', async () => {
26+
const file = getWorkspaceFile('specific-usage-cases/reexport/reexport-import.ts');
27+
const parsed = await parser.parseFile(file, rootPath);
28+
expect(parsed.exports).toMatchSnapshot();
29+
expect(parsed.usages).toMatchSnapshot();
30+
});
31+
32+
it('should contain imported default elements in usages', async () => {
33+
const file = getWorkspaceFile('specific-usage-cases/reexport/reexport-default.ts');
34+
const parsed = await parser.parseFile(file, rootPath);
35+
expect(parsed.exports).toMatchSnapshot();
36+
expect(parsed.usages).toMatchSnapshot();
37+
});
38+
39+
});
40+
2341
});

0 commit comments

Comments
 (0)