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 02c05b3 commit 6a69b49
Showing 1 changed file with 35 additions and 11 deletions.
46 changes: 35 additions & 11 deletions sapling/src/SaplingParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,42 @@ export class SaplingParser {
return componentTree;
}

// Finds files where import string does not include a file extension
private getFileName(componentTree: Tree) : string | undefined {
const ext = path.extname(componentTree.filePath);
let fileName = componentTree.fileName;

if (!ext) {
// Try and find file extension that exists in directory:
const fileArray = fs.readdirSync(path.dirname(componentTree.filePath));
const regEx = new RegExp(`${componentTree.fileName}.(j|t)sx?$`);
fileName = fileArray.find(fileStr => fileStr.match(regEx));
fileName ? componentTree.filePath += path.extname(fileName) : null;
private validateFilePath(filePath: string, importName: string): string {
const fileArray = [];
let parsedFileName = '';
// Handles Next.js component imports
try {
fileArray.push(...fs.readdirSync(path.dirname(filePath)));
} catch {
return filePath;
}
// Checks that file exists and appends file extension to path if not given in import declaration
parsedFileName =
fileArray.find((str) => new RegExp(`${path.basename(filePath)}\\.(j|t)sx?$`).test(str)) || '';
if (parsedFileName.length) return (filePath += path.extname(parsedFileName));
/**
* 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;
}

return fileName;
}
Expand Down

0 comments on commit 6a69b49

Please sign in to comment.