mirror of
https://github.com/Snigdha-OS/documentation.git
synced 2025-09-08 19:34:56 +02:00
32 lines
1.2 KiB
JavaScript
32 lines
1.2 KiB
JavaScript
/**
|
||
* @typedef {import('estree-jsx').Declaration} Declaration
|
||
* @typedef {import('estree-jsx').Expression} Expression
|
||
* @typedef {import('estree-jsx').MaybeNamedClassDeclaration} MaybeNamedClassDeclaration
|
||
* @typedef {import('estree-jsx').MaybeNamedFunctionDeclaration} MaybeNamedFunctionDeclaration
|
||
*/
|
||
|
||
import {ok as assert} from 'devlop'
|
||
|
||
/**
|
||
* Turn a declaration into an expression.
|
||
*
|
||
* Doesn’t work for variable declarations, but that’s fine for our use case
|
||
* because currently we’re using this utility for export default declarations,
|
||
* which can’t contain variable declarations.
|
||
*
|
||
* @param {Readonly<Declaration | MaybeNamedClassDeclaration | MaybeNamedFunctionDeclaration>} declaration
|
||
* Declaration.
|
||
* @returns {Expression}
|
||
* Expression.
|
||
*/
|
||
export function declarationToExpression(declaration) {
|
||
if (declaration.type === 'FunctionDeclaration') {
|
||
return {...declaration, type: 'FunctionExpression'}
|
||
}
|
||
|
||
// This is currently an internal utility so the next shouldn’t happen or a
|
||
// maintainer is making a mistake.
|
||
assert(declaration.type === 'ClassDeclaration', 'unexpected node type')
|
||
return {...declaration, type: 'ClassExpression'}
|
||
}
|