Skip to content

Commit 0229e12

Browse files
author
Christoph Bühler
committed
feat(parsing): Enable javascript parsing (#13)
Adds support for correctly parse javascript files (actually get rid of the `js` and `jsx` extension)
1 parent 4112748 commit 0229e12

File tree

8 files changed

+505
-10
lines changed

8 files changed

+505
-10
lines changed

src/DeclarationIndex.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { Namespace } from './resources/Namespace';
1212
import { Resource } from './resources/Resource';
1313
import { isExportableDeclaration } from './type-guards/TypescriptHeroGuards';
1414
import { TypescriptParser } from './TypescriptParser';
15-
import { normalizePathUri } from './utilities/PathHelpers';
15+
import { normalizeFilename, normalizePathUri } from './utilities/PathHelpers';
1616

1717
/**
1818
* Returns the name of the node folder. Is used as the library name for indexing.
@@ -25,8 +25,7 @@ function getNodeLibraryName(path: string): string {
2525
const dirs = path.split(/\/|\\/);
2626
const nodeIndex = dirs.indexOf('node_modules');
2727

28-
return dirs.slice(nodeIndex + 1).join('/')
29-
.replace(/([.]d)?([.]tsx?)?/g, '')
28+
return normalizeFilename(dirs.slice(nodeIndex + 1).join('/'))
3029
.replace(new RegExp(`/(index|${dirs[nodeIndex + 1]}|${dirs[dirs.length - 2]})$`), '');
3130
}
3231

@@ -222,7 +221,7 @@ export class DeclarationIndex {
222221

223222
for (const change of changes.deleted) {
224223
const filePath = normalizePathUri(change);
225-
const resource = '/' + relative(this.rootPath, filePath).replace(/[.]tsx?/g, '');
224+
const resource = '/' + normalizeFilename(relative(this.rootPath, filePath));
226225

227226
if (removeResources.indexOf(resource) < 0) {
228227
removeResources.push(resource);
@@ -237,7 +236,7 @@ export class DeclarationIndex {
237236

238237
for (const change of (changes.created || []).concat(changes.updated)) {
239238
const filePath = normalizePathUri(change);
240-
const resource = '/' + relative(this.rootPath, filePath).replace(/[.]tsx?/g, '');
239+
const resource = '/' + normalizeFilename(relative(this.rootPath, filePath));
241240

242241
if (rebuildResources.indexOf(resource) < 0) {
243242
rebuildResources.push(resource);
@@ -427,7 +426,7 @@ export class DeclarationIndex {
427426
if (sourceLib.indexOf('node_modules') > -1) {
428427
sourceLib = getNodeLibraryName(sourceLib);
429428
} else {
430-
sourceLib = '/' + relative(this.rootPath, sourceLib).replace(/([.]d)?[.]tsx?/g, '');
429+
sourceLib = '/' + normalizeFilename(relative(this.rootPath, sourceLib));
431430
}
432431

433432
if (!parsedResources[sourceLib]) {

src/resources/File.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { Declaration } from '../declarations/Declaration';
44
import { Export } from '../exports/Export';
55
import { Import } from '../imports/Import';
66
import { Node } from '../Node';
7+
import { normalizeFilename } from '../utilities/PathHelpers';
78
import { Module } from './Module';
89
import { Namespace } from './Namespace';
910
import { Resource } from './Resource';
@@ -24,7 +25,7 @@ export class File implements Resource, Node {
2425
public usages: string[] = [];
2526

2627
public get identifier(): string {
27-
return '/' + relative(this.rootPath, this.filePath).replace(/([.]d)?[.]tsx?/g, '');
28+
return '/' + normalizeFilename(relative(this.rootPath, this.filePath));
2829
}
2930

3031
public get nonLocalUsages(): string[] {
@@ -33,8 +34,8 @@ export class File implements Resource, Node {
3334
!this.declarations.some(o => o.name === usage) &&
3435
!this.resources.some(o => (o instanceof Module || o instanceof Namespace) && o.name === usage))
3536
.concat(
36-
this.resources.reduce((all, cur) => all.concat(cur.nonLocalUsages), [] as string[]),
37-
);
37+
this.resources.reduce((all, cur) => all.concat(cur.nonLocalUsages), [] as string[]),
38+
);
3839
}
3940

4041
/**
@@ -59,5 +60,5 @@ export class File implements Resource, Node {
5960
return ['node_modules', 'typings'].every(o => this.filePath.indexOf(o) === -1);
6061
}
6162

62-
constructor(public filePath: string, private rootPath: string, public start: number, public end: number) {}
63+
constructor(public filePath: string, private rootPath: string, public start: number, public end: number) { }
6364
}

src/utilities/PathHelpers.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,15 @@ export function normalizePathUri(uri: string): string {
1515
}
1616
return decoded.replace('file://', '');
1717
}
18+
19+
/**
20+
* Returns an adjusted and normalized filepath to use within the index.
21+
* Essentially does remove `.tsx` `.ts` `.js` `.jsx` endings and other adjustments.
22+
*
23+
* @export
24+
* @param {string} filepath
25+
* @returns {string}
26+
*/
27+
export function normalizeFilename(filepath: string): string {
28+
return filepath.replace(/([.]d)?[.](t|j)sx?$/g, '');
29+
}

test/TypescriptParser.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,4 +664,62 @@ describe('TypescriptParser', () => {
664664

665665
});
666666

667+
describe('JavaScript parsing', () => {
668+
669+
const file = getWorkspaceFile('typescript-parser/javascript.js');
670+
671+
it('should parse a simple javascript file correctly with "parseFile"', async () => {
672+
const parsed = await parser.parseFile(file, rootPath);
673+
delete parsed.filePath;
674+
delete (parsed as any).rootPath;
675+
676+
expect(parsed).toMatchSnapshot();
677+
});
678+
679+
it('should parse a simple javascript file correctly with "parseFiles"', async () => {
680+
const parsed = await parser.parseFiles([file], rootPath);
681+
delete parsed[0].filePath;
682+
delete (parsed[0] as any).rootPath;
683+
684+
expect(parsed).toMatchSnapshot();
685+
});
686+
687+
it('should parse a simple javascript file correctly with "parseSource"', async () => {
688+
const content = readFileSync(file).toString();
689+
const parsed = await parser.parseSource(content);
690+
691+
expect(parsed).toMatchSnapshot();
692+
});
693+
694+
});
695+
696+
describe('JSX parsing', () => {
697+
698+
const file = getWorkspaceFile('typescript-parser/jsx.jsx');
699+
700+
it('should parse a simple javascript react file correctly with "parseFile"', async () => {
701+
const parsed = await parser.parseFile(file, rootPath);
702+
delete parsed.filePath;
703+
delete (parsed as any).rootPath;
704+
705+
expect(parsed).toMatchSnapshot();
706+
});
707+
708+
it('should parse a simple javascript react file correctly with "parseFiles"', async () => {
709+
const parsed = await parser.parseFiles([file], rootPath);
710+
delete parsed[0].filePath;
711+
delete (parsed[0] as any).rootPath;
712+
713+
expect(parsed).toMatchSnapshot();
714+
});
715+
716+
it('should parse a simple javascript react file correctly with "parseSource"', async () => {
717+
const content = readFileSync(file).toString();
718+
const parsed = await parser.parseSource(content);
719+
720+
expect(parsed).toMatchSnapshot();
721+
});
722+
723+
});
724+
667725
});

0 commit comments

Comments
 (0)