Skip to content

Commit

Permalink
feat: handle export batch declarations / barrel files
Browse files Browse the repository at this point in the history
- passes test 14
- glob exports in general not yet supported

Fixes: Issue oslabs-beta#85
See: Issue oslabs-beta#99
  • Loading branch information
MajorLift committed Nov 30, 2021
1 parent 1cd3446 commit 2167e91
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions sapling/src/SaplingParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,27 @@ export class SaplingParser {
parsedFileName =
fileArray.find((str) => new RegExp(`${path.basename(filePath)}\\.(j|t)sx?$`).test(str)) || '';
if (parsedFileName.length) return (filePath += path.extname(parsedFileName));
// Finds files where import string does not include a file extension
private getFileName(componentTree: Tree) : string | null {
const ext = path.extname(componentTree.filePath);
/**
* Handles Export Batch Declarations / 'Barrel' Files (index.(j|t)s)
* Issues #85, #99
* e.g. export * from './dir'
* e.g. export { default as alias } from './dir'
*/
if (
!path.extname(filePath) &&
fs
.readdirSync(path.dirname(filePath)) // will list elements of module's parent dir
.find((str) => str.match(new RegExp(`${path.basename(filePath)}`))) && // there exists a subdir with module's name
fs
.readdirSync(path.dirname(filePath) + '/' + path.basename(filePath))
.find((str) => /index\\.((j|t)sx?|node)$/.test(str)) // module folder contains a barrel file
) {
parsedFileName =
fs
.readdirSync(path.dirname(filePath) + '/' + path.basename(filePath))
.find((str) => new RegExp(`${path.basename(importName)}\\.(j|t)sx?$`).test(str)) || '';
if (parsedFileName.length) return (filePath += path.extname(parsedFileName));
}
return filePath;
}

Expand Down

0 comments on commit 2167e91

Please sign in to comment.