Skip to content

Commit

Permalink
Refine loop formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 18, 2021
1 parent ef109d1 commit ac37786
Show file tree
Hide file tree
Showing 84 changed files with 782 additions and 557 deletions.
51 changes: 38 additions & 13 deletions src/ast/nodes/ImportExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ export default class ImportExpression extends NodeBase {
render(code: MagicString, options: RenderOptions): void {
if (this.inlineNamespace) {
const {
snippets: { directReturnFunctionRight, getDirectReturnFunctionLeft, getPropertyAccess }
snippets: { getDirectReturnFunctionLeft, getDirectReturnFunctionRight, getPropertyAccess }
} = options;
code.overwrite(
this.start,
this.end,
`Promise.resolve().then(${getDirectReturnFunctionLeft([], {
functionReturn: true,
name: null
})}${this.inlineNamespace.getName(getPropertyAccess)}${directReturnFunctionRight})`,
lineBreakIndent: false,
name: null,
t: ''
})}${this.inlineNamespace.getName(getPropertyAccess)}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`,
{ contentOnly: true }
);
return;
Expand All @@ -78,16 +83,21 @@ export default class ImportExpression extends NodeBase {
code: MagicString,
resolution: string,
namespaceExportName: string | false | undefined,
{ directReturnFunctionRight, getDirectReturnFunctionLeft }: GenerateCodeSnippets
{ getDirectReturnFunctionLeft, getDirectReturnFunctionRight }: GenerateCodeSnippets
): void {
code.overwrite(this.source.start, this.source.end, resolution);
if (namespaceExportName) {
code.prependLeft(
this.end,
`.then(${getDirectReturnFunctionLeft(['n'], {
functionReturn: true,
name: null
})}n.${namespaceExportName}${directReturnFunctionRight})`
lineBreakIndent: false,
name: null,
t: ''
})}n.${namespaceExportName}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`
);
}
}
Expand Down Expand Up @@ -135,8 +145,8 @@ export default class ImportExpression extends NodeBase {
}: NormalizedOutputOptions,
{
_,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getDirectReturnIifeLeft
}: GenerateCodeSnippets,
pluginDriver: PluginDriver
Expand Down Expand Up @@ -165,9 +175,14 @@ export default class ImportExpression extends NodeBase {
}
left = `Promise.resolve().then(${getDirectReturnFunctionLeft([], {
functionReturn: true,
name: null
lineBreakIndent: false,
name: null,
t: ''
})}${left}`;
right += `${directReturnFunctionRight})`;
right += `${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
Expand All @@ -187,14 +202,24 @@ export default class ImportExpression extends NodeBase {
const resolveNamespace = helper
? `${getDirectReturnFunctionLeft(['m'], {
functionReturn: false,
name: null
})}${resolve}(/*#__PURE__*/${helper}(m))${directReturnFunctionRight}`
lineBreakIndent: false,
name: null,
t: ''
})}${resolve}(/*#__PURE__*/${helper}(m))${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}`
: resolve;
let left = `new Promise(${getDirectReturnFunctionLeft([resolve, reject], {
functionReturn: false,
name: null
lineBreakIndent: false,
name: null,
t: ''
})}require([`;
let right = `],${_}${resolveNamespace},${_}${reject})${directReturnFunctionRight})`;
let right = `],${_}${resolveNamespace},${_}${reject})${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})})`;
if (!arrowFunctions && hasDynamicTarget) {
left = getDirectReturnIifeLeft(['t'], `${left}t${right}`, {
needsArrowReturnParens: false,
Expand Down
2 changes: 1 addition & 1 deletion src/ast/variables/NamespaceVariable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export default class NamespaceVariable extends Variable {

members.unshift([null, `__proto__:${_}null`]);

let output = getObject(members, { indent: t, lineBreaks: true });
let output = getObject(members, { lineBreakIndent: t });
if (this.mergedNamespaces.length > 0) {
const assignmentArgs = this.mergedNamespaces.map(variable =>
variable.getName(getPropertyAccess)
Expand Down
20 changes: 15 additions & 5 deletions src/finalisers/shared/getExportBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ export function getExportBlock(
{
_,
cnst,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getFunctionIntro,
getPropertyAccess,
n,
Expand Down Expand Up @@ -69,8 +69,13 @@ export function getExportBlock(
`${t}enumerable:${_}true,${n}` +
`${t}get:${_}${getDirectReturnFunctionLeft([], {
functionReturn: true,
name: null
})}${importName}${directReturnFunctionRight}${n}});`
lineBreakIndent: false,
name: null,
t: ''
})}${importName}${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}${n}});`
: `exports${getPropertyAccess(specifier.reexported)}${_}=${_}${importName};`;
}
}
Expand All @@ -96,8 +101,13 @@ export function getExportBlock(
`${t}${t}enumerable:${_}true,${n}` +
`${t}${t}get:${_}${getDirectReturnFunctionLeft([], {
functionReturn: true,
name: null
})}${name}[k]${directReturnFunctionRight}${n}${t}})`
lineBreakIndent: false,
name: null,
t: ''
})}${name}[k]${getDirectReturnFunctionRight({
lineBreakIndent: false,
name: false
})}${n}${t}})`
: `exports[k]${_}=${_}${name}[k]`;
const copyPropertyIfNecessary = `{${n}${t}if${_}(k${_}!==${_}'default'${_}&&${_}!exports.hasOwnProperty(k))${_}${defineProperty}${s}${n}}`;
exportBlock +=
Expand Down
4 changes: 2 additions & 2 deletions src/finalisers/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function analyzeDependencies(
}
}
if (reexportedNames.length > 1 || hasStarReexport) {
const exportMapping = getObject(reexportedNames, { indent: _, lineBreaks: false });
const exportMapping = getObject(reexportedNames, { lineBreakIndent: false });
if (hasStarReexport) {
if (!starExcludes) {
starExcludes = getStarExcludes({ dependencies, exports });
Expand Down Expand Up @@ -178,7 +178,7 @@ const getStarExcludesBlock = (
starExcludes
? `${n}${t}${cnst} _starExcludes${_}=${_}${getObject(
[...starExcludes].map(prop => [prop, '1']),
{ indent: _, lineBreaks: false }
{ lineBreakIndent: false }
)};`
: '';

Expand Down
46 changes: 32 additions & 14 deletions src/utils/generateCodeSnippets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import { RESERVED_NAMES } from './reservedNames';
export interface GenerateCodeSnippets {
_: string;
cnst: string;
directReturnFunctionRight: string;
n: string;
namedDirectReturnFunctionRight: string;
s: string;
getDirectReturnFunctionLeft(
params: string[],
options: { functionReturn: boolean; name: string | null }
options: {
functionReturn: boolean;
lineBreakIndent: string | false;
name: string | null;
t: string;
}
): string;
getDirectReturnFunctionRight(options: { lineBreakIndent: string | false; name: boolean }): string;
getDirectReturnIifeLeft(
params: string[],
returned: string,
Expand All @@ -25,9 +29,10 @@ export interface GenerateCodeSnippets {
params: string[],
options: { isAsync: boolean; name: string | null }
): string;
// TODO Lukas adjust line-break handling with functions, change in System wrapper
getObject(
fields: [key: string | null, value: string][],
options: { indent: string; lineBreaks: boolean }
options: { lineBreakIndent: string | false }
): string;
getPropertyAccess(name: string): string;
}
Expand Down Expand Up @@ -56,14 +61,26 @@ export function getGenerateCodeSnippets({

const getDirectReturnFunctionLeft: GenerateCodeSnippets['getDirectReturnFunctionLeft'] = (
params,
{ functionReturn, name }
{ functionReturn, t, lineBreakIndent, name }
) =>
`${getFunctionIntro(params, {
isAsync: false,
name
})}${arrowFunctions ? '' : `{${_}${functionReturn ? 'return ' : ''}`}`;
})}${
arrowFunctions
? lineBreakIndent
? `${n}${lineBreakIndent}${t}`
: ''
: `{${lineBreakIndent ? `${n}${lineBreakIndent}${t}` : _}${functionReturn ? 'return ' : ''}`
}`;

const directReturnFunctionRight = arrowFunctions ? '' : `${s}${_}}`;
const getDirectReturnFunctionRight: GenerateCodeSnippets['getDirectReturnFunctionRight'] = ({
lineBreakIndent,
name
}) =>
arrowFunctions
? `${name ? ';' : ''}${lineBreakIndent ? `${n}${lineBreakIndent}` : ''}`
: `${s}${lineBreakIndent ? `${n}${lineBreakIndent}` : _}}`;

const isValidPropName = reservedNamesAsProps
? (name: string): boolean => validPropName.test(name)
Expand All @@ -72,23 +89,25 @@ export function getGenerateCodeSnippets({
return {
_,
cnst,
directReturnFunctionRight,
getDirectReturnFunctionLeft,
getDirectReturnFunctionRight,
getDirectReturnIifeLeft: (params, returned, { needsArrowReturnParens, needsWrappedFunction }) =>
`${wrapIfNeeded(
`${getDirectReturnFunctionLeft(params, {
functionReturn: true,
name: null
lineBreakIndent: false,
name: null,
t: ''
})}${wrapIfNeeded(
returned,
arrowFunctions && needsArrowReturnParens
)}${directReturnFunctionRight}`,
)}${getDirectReturnFunctionRight({ lineBreakIndent: false, name: false })}`,
arrowFunctions || needsWrappedFunction
)}(`,
getFunctionIntro,
getNonArrowFunctionIntro,
getObject(fields, { indent, lineBreaks }) {
const prefix = `${lineBreaks ? n : ''}${indent}`;
getObject(fields, { lineBreakIndent }) {
const prefix = lineBreakIndent === false ? _ : `${n}${lineBreakIndent}`;
return `{${fields
.map(([key, value]) => {
if (key === null) return `${prefix}${value}`;
Expand All @@ -97,12 +116,11 @@ export function getGenerateCodeSnippets({
? prefix + key
: `${prefix}${needsQuotes ? `'${key}'` : key}:${_}${value}`;
})
.join(`,`)}${fields.length === 0 ? '' : lineBreaks ? n : indent}}`;
.join(`,`)}${fields.length === 0 ? '' : lineBreakIndent === false ? _ : n}}`;
},
getPropertyAccess: (name: string): string =>
isValidPropName(name) ? `.${name}` : `[${JSON.stringify(name)}]`,
n,
namedDirectReturnFunctionRight: `${directReturnFunctionRight}${arrowFunctions ? ';' : ''}`,
s
};
}
Expand Down

0 comments on commit ac37786

Please sign in to comment.