-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from near/feat/export
feat: add support for `export` keyword
- Loading branch information
Showing
4 changed files
with
69 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Supported export syntax: | ||
* - export default X; | ||
* - export function X... | ||
* - export const X... | ||
*/ | ||
const EXPORT_REGEX = | ||
/^export\s+(const|default|function)\s+(?<identifier>[\w$_]+)/g; | ||
|
||
/** | ||
* Extract the name of the exported reference and strip the export keyword(s) from the source | ||
* @param source BOS Component source | ||
*/ | ||
export const extractExport = (source: string) => { | ||
const [match, ...matches] = [...source.matchAll(EXPORT_REGEX)]; | ||
if (matches.length) { | ||
throw new Error(`Multiple exports not permitted: ${matches.join(', ')}`); | ||
} | ||
|
||
if (!match?.groups?.identifier) { | ||
return { exported: null, source }; | ||
} | ||
|
||
return { | ||
exported: match.groups.identifier, | ||
source: source.replace( | ||
match[0], | ||
match[0].replace(/export(\s+default)?\s+/, '') | ||
), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters