Skip to content

Commit

Permalink
Merge helper strings
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 7, 2021
1 parent 59f5ca1 commit 9d3b104
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/utils/generateCodeSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ export interface GenerateCodeSnippets {
_: string;
directReturnFunctionRight: string;
n: string;
// TODO Lukas replace with namedDirectReturnFunctionRight?
namedFunctionSemicolon: string;
namedDirectReturnFunctionRight: string;
s: string;
getDirectReturnFunctionLeft(
params: string[],
Expand All @@ -18,7 +17,6 @@ export interface GenerateCodeSnippets {
fields: [key: string | null, value: string][],
options: { indent: string; lineBreaks: boolean }
): string;
// TODO Lukas also for default access as constant
getPropertyAccess(name: string): string;
renderDirectReturnIife(
params: string[],
Expand Down Expand Up @@ -88,7 +86,7 @@ export function getGenerateCodeSnippets({
getPropertyAccess: (name: string): string =>
isValidPropName(name) ? `.${name}` : `[${JSON.stringify(name)}]`,
n,
namedFunctionSemicolon: arrowFunctions ? ';' : '',
namedDirectReturnFunctionRight: `${directReturnFunctionRight}${arrowFunctions ? ';' : ''}`,
renderDirectReturnIife: (
params,
returned,
Expand Down
26 changes: 8 additions & 18 deletions src/utils/interopHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,41 +70,32 @@ const HELPER_GENERATORS: {
) => string;
} = {
[INTEROP_DEFAULT_LEGACY_VARIABLE]: (_t, snippets, liveBindings) => {
const { _, directReturnFunctionRight, getDirectReturnFunctionLeft, n, namedFunctionSemicolon } =
snippets;
const { _, getDirectReturnFunctionLeft, n, namedDirectReturnFunctionRight } = snippets;
return (
`${getDirectReturnFunctionLeft(['e'], {
functionReturn: true,
name: INTEROP_DEFAULT_LEGACY_VARIABLE
})}e${_}&&${_}typeof e${_}===${_}'object'${_}&&${_}'default'${_}in e${_}?${_}` +
`${
liveBindings ? getDefaultLiveBinding(snippets) : getDefaultStatic(snippets)
}${directReturnFunctionRight}${namedFunctionSemicolon}${n}${n}`
}${namedDirectReturnFunctionRight}${n}${n}`
);
},
[INTEROP_DEFAULT_VARIABLE]: (_t, snippets, liveBindings) => {
const { _, directReturnFunctionRight, getDirectReturnFunctionLeft, n, namedFunctionSemicolon } =
snippets;
const { _, getDirectReturnFunctionLeft, n, namedDirectReturnFunctionRight } = snippets;
return (
`${getDirectReturnFunctionLeft(['e'], {
functionReturn: true,
name: INTEROP_DEFAULT_VARIABLE
})}e${_}&&${_}e.__esModule${_}?${_}` +
`${
liveBindings ? getDefaultLiveBinding(snippets) : getDefaultStatic(snippets)
}${directReturnFunctionRight}${namedFunctionSemicolon}${n}${n}`
}${namedDirectReturnFunctionRight}${n}${n}`
);
},
[INTEROP_NAMESPACE_DEFAULT_ONLY_VARIABLE]: (
_t,
{
_,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getObject,
n,
namedFunctionSemicolon
},
{ _, getDirectReturnFunctionLeft, getObject, n, namedDirectReturnFunctionRight },
_liveBindings: boolean,
freeze: boolean,
namespaceToStringTag: boolean
Expand All @@ -124,7 +115,7 @@ const HELPER_GENERATORS: {
{ indent: _, lineBreaks: false }
),
freeze
)}${directReturnFunctionRight}${namedFunctionSemicolon}${n}${n}`,
)}${namedDirectReturnFunctionRight}${n}${n}`,
[INTEROP_NAMESPACE_DEFAULT_VARIABLE]: (
t,
snippets,
Expand All @@ -147,13 +138,12 @@ const HELPER_GENERATORS: {
namespaceToStringTag,
usedHelpers
) => {
const { _, directReturnFunctionRight, getDirectReturnFunctionLeft, n, namedFunctionSemicolon } =
snippets;
const { _, getDirectReturnFunctionLeft, n, namedDirectReturnFunctionRight } = snippets;
return usedHelpers.has(INTEROP_NAMESPACE_DEFAULT_VARIABLE)
? `${getDirectReturnFunctionLeft(['e'], {
functionReturn: true,
name: INTEROP_NAMESPACE_VARIABLE
})}e${_}&&${_}e.__esModule${_}?${_}e${_}:${_}${INTEROP_NAMESPACE_DEFAULT_VARIABLE}(e)${directReturnFunctionRight}${namedFunctionSemicolon}${n}${n}`
})}e${_}&&${_}e.__esModule${_}?${_}e${_}:${_}${INTEROP_NAMESPACE_DEFAULT_VARIABLE}(e)${namedDirectReturnFunctionRight}${n}${n}`
: `function ${INTEROP_NAMESPACE_VARIABLE}(e)${_}{${n}` +
`${t}if${_}(e${_}&&${_}e.__esModule)${_}return e;${n}` +
createNamespaceObject(t, t, snippets, liveBindings, freeze, namespaceToStringTag) +
Expand Down

0 comments on commit 9d3b104

Please sign in to comment.