-
Notifications
You must be signed in to change notification settings - Fork 6
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 #158 from near/feat/require-export
feat: require default or named export
- Loading branch information
Showing
4 changed files
with
169 additions
and
75 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 |
---|---|---|
@@ -1,31 +1,56 @@ | ||
/** | ||
* Supported export syntax: | ||
* - export default X; | ||
* - export function X... | ||
* - export const X... | ||
* - export function BWEComponent... | ||
* - export const BWEComponent... | ||
*/ | ||
const EXPORT_REGEX = | ||
/^export\s+(const|default|function)\s+(?<identifier>[\w$_]+)/g; | ||
/^export(?<defaultExport>\s+default)?\s+(const|function)\s+(?<identifier>[\w$_]+)?/gm; | ||
|
||
/** | ||
* 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(', ')}`); | ||
} | ||
return [...source.matchAll(EXPORT_REGEX)].reduce( | ||
(exported, match) => { | ||
if (!exported.hasExport) { | ||
const { defaultExport, identifier } = match.groups as { | ||
defaultExport: string; | ||
identifier: string; | ||
}; | ||
|
||
if (!match?.groups?.identifier) { | ||
return { exported: null, source }; | ||
} | ||
if (defaultExport) { | ||
if (!identifier) { | ||
exported.source = exported.source | ||
.replace( | ||
/export\s+default\s+function\s*\(/, | ||
'function BWEComponent(' | ||
) | ||
.replace(/export\s+default\s+\(/, 'const BWEComponent = ('); | ||
} else { | ||
exported.exportedReference = identifier; | ||
} | ||
exported.hasExport = true; | ||
} else if (identifier === 'BWEComponent') { | ||
exported.hasExport = true; | ||
} | ||
} | ||
|
||
return { | ||
exported: match.groups.identifier, | ||
source: source.replace( | ||
match[0], | ||
match[0].replace(/export(\s+default)?\s+/, '') | ||
), | ||
}; | ||
return { | ||
...exported, | ||
source: exported.source.replace( | ||
match[0], | ||
match[0] | ||
.replace(/export\s+default\s+/, '') // replace "export default" | ||
.replace(/export\s+(const|function)/, '$1') // remove "export" from export statements | ||
), | ||
}; | ||
}, | ||
{ | ||
exportedReference: '', | ||
hasExport: false, | ||
source, | ||
} | ||
); | ||
}; |
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