From 7f6d0c6dbf006ccbf573e1047aba6352dac998c6 Mon Sep 17 00:00:00 2001 From: Daniel Nalborczyk Date: Sat, 31 Dec 2022 09:48:00 -0500 Subject: [PATCH] Apply new unicorn linting rules --- browser/src/performance.ts | 2 +- cli/run/timings.ts | 2 +- scripts/perf.js | 6 +++--- src/Chunk.ts | 6 +++--- src/ModuleLoader.ts | 12 ++++++------ src/ast/nodes/ArrayExpression.ts | 6 +++--- src/ast/nodes/ConditionalExpression.ts | 22 +++++++++++----------- src/ast/nodes/LogicalExpression.ts | 6 +++--- src/ast/nodes/VariableDeclaration.ts | 6 +++--- src/ast/nodes/shared/ClassNode.ts | 6 +++--- src/ast/nodes/shared/ObjectEntity.ts | 20 ++++++++++---------- src/utils/FileEmitter.ts | 6 +++--- src/utils/collapseSourcemaps.ts | 6 +++--- src/utils/error.ts | 2 +- src/utils/options/normalizeInputOptions.ts | 2 +- src/utils/relativeId.ts | 2 +- test/function/index.js | 8 ++++---- 17 files changed, 60 insertions(+), 60 deletions(-) diff --git a/browser/src/performance.ts b/browser/src/performance.ts index aec23b86950..c9b42e2d3f3 100644 --- a/browser/src/performance.ts +++ b/browser/src/performance.ts @@ -1,5 +1,5 @@ const global = - typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : {}; + typeof globalThis === 'undefined' ? (typeof window === 'undefined' ? {} : window) : globalThis; export default 'performance' in global ? performance diff --git a/cli/run/timings.ts b/cli/run/timings.ts index 7797b36f677..7b174cfa453 100644 --- a/cli/run/timings.ts +++ b/cli/run/timings.ts @@ -5,7 +5,7 @@ import { bold, underline } from '../../src/utils/colors'; export function printTimings(timings: SerializedTimings): void { for (const [label, [time, memory, total]] of Object.entries(timings)) { const appliedColor = - label[0] === '#' ? (label[1] !== '#' ? underline : bold) : (text: string) => text; + label[0] === '#' ? (label[1] === '#' ? bold : underline) : (text: string) => text; const row = `${label}: ${time.toFixed(0)}ms, ${prettyBytes(memory)} / ${prettyBytes(total)}`; console.info(appliedColor(row)); } diff --git a/scripts/perf.js b/scripts/perf.js index 66501115808..307e644d950 100644 --- a/scripts/perf.js +++ b/scripts/perf.js @@ -96,10 +96,10 @@ async function calculatePrintAndPersistTimings(config, existingTimings) { const currentTimings = await buildAndGetTimings(config); clearLines(numberOfLinesToClear); for (const label of Object.keys(timings)) { - if (!currentTimings.hasOwnProperty(label)) { - delete timings[label]; - } else { + if (currentTimings.hasOwnProperty(label)) { timings[label].push(currentTimings[label]); + } else { + delete timings[label]; } } } diff --git a/src/Chunk.ts b/src/Chunk.ts index 359e02716ed..f8bc57eb62d 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -506,9 +506,7 @@ export default class Chunk { const { chunkFileNames, entryFileNames, file, format, preserveModules } = this.outputOptions; if (file) { fileName = basename(file); - } else if (this.fileName !== null) { - fileName = this.fileName; - } else { + } else if (this.fileName === null) { const [pattern, patternName] = preserveModules || this.facadeModule?.isUserDefinedEntryPoint ? [entryFileNames, 'output.entryFileNames'] @@ -526,6 +524,8 @@ export default class Chunk { if (!hashPlaceholder) { fileName = makeUnique(fileName, this.bundle); } + } else { + fileName = this.fileName; } if (!hashPlaceholder) { this.bundle[fileName] = FILE_PLACEHOLDER; diff --git a/src/ModuleLoader.ts b/src/ModuleLoader.ts index 09dd56b1a05..184fb2075b1 100644 --- a/src/ModuleLoader.ts +++ b/src/ModuleLoader.ts @@ -131,16 +131,16 @@ export class ModuleLoader { const existingIndexedModule = this.indexedEntryModules.find( indexedModule => indexedModule.module === entryModule ); - if (!existingIndexedModule) { - this.indexedEntryModules.push({ - index: firstEntryModuleIndex + index, - module: entryModule - }); - } else { + if (existingIndexedModule) { existingIndexedModule.index = Math.min( existingIndexedModule.index, firstEntryModuleIndex + index ); + } else { + this.indexedEntryModules.push({ + index: firstEntryModuleIndex + index, + module: entryModule + }); } } this.indexedEntryModules.sort(({ index: indexA }, { index: indexB }) => diff --git a/src/ast/nodes/ArrayExpression.ts b/src/ast/nodes/ArrayExpression.ts index cddbdd70117..732b09fe027 100644 --- a/src/ast/nodes/ArrayExpression.ts +++ b/src/ast/nodes/ArrayExpression.ts @@ -94,10 +94,10 @@ export default class ArrayExpression extends NodeBase { hasSpread = true; properties.unshift({ key: UnknownInteger, kind: 'init', property: element }); } - } else if (!element) { - properties.push({ key: String(index), kind: 'init', property: UNDEFINED_EXPRESSION }); - } else { + } else if (element) { properties.push({ key: String(index), kind: 'init', property: element }); + } else { + properties.push({ key: String(index), kind: 'init', property: UNDEFINED_EXPRESSION }); } } return (this.objectEntity = new ObjectEntity(properties, ARRAY_PROTOTYPE)); diff --git a/src/ast/nodes/ConditionalExpression.ts b/src/ast/nodes/ConditionalExpression.ts index f605dd11bd6..6489cd3b3cb 100644 --- a/src/ast/nodes/ConditionalExpression.ts +++ b/src/ast/nodes/ConditionalExpression.ts @@ -47,11 +47,11 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz deoptimizePath(path: ObjectPath): void { const usedBranch = this.getUsedBranch(); - if (!usedBranch) { + if (usedBranch) { + usedBranch.deoptimizePath(path); + } else { this.consequent.deoptimizePath(path); this.alternate.deoptimizePath(path); - } else { - usedBranch.deoptimizePath(path); } } @@ -150,11 +150,11 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz parameters: readonly (ExpressionEntity | SpreadElement)[] ): void { const usedBranch = this.getUsedBranch(); - if (!usedBranch) { + if (usedBranch) { + usedBranch.includeCallArguments(context, parameters); + } else { this.consequent.includeCallArguments(context, parameters); this.alternate.includeCallArguments(context, parameters); - } else { - usedBranch.includeCallArguments(context, parameters); } } @@ -169,7 +169,11 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz }: NodeRenderOptions = BLANK ): void { const usedBranch = this.getUsedBranch(); - if (!this.test.included) { + if (this.test.included) { + this.test.render(code, options, { renderedSurroundingElement }); + this.consequent.render(code, options); + this.alternate.render(code, options); + } else { const colonPos = findFirstOccurrenceOutsideComment(code.original, ':', this.consequent.end); const inclusionStart = findNonWhiteSpace( code.original, @@ -191,10 +195,6 @@ export default class ConditionalExpression extends NodeBase implements Deoptimiz renderedParentType: renderedParentType || this.parent.type, renderedSurroundingElement: renderedSurroundingElement || this.parent.type }); - } else { - this.test.render(code, options, { renderedSurroundingElement }); - this.consequent.render(code, options); - this.alternate.render(code, options); } } diff --git a/src/ast/nodes/LogicalExpression.ts b/src/ast/nodes/LogicalExpression.ts index cbc8625e436..293d6243454 100644 --- a/src/ast/nodes/LogicalExpression.ts +++ b/src/ast/nodes/LogicalExpression.ts @@ -60,11 +60,11 @@ export default class LogicalExpression extends NodeBase implements Deoptimizable deoptimizePath(path: ObjectPath): void { const usedBranch = this.getUsedBranch(); - if (!usedBranch) { + if (usedBranch) { + usedBranch.deoptimizePath(path); + } else { this.left.deoptimizePath(path); this.right.deoptimizePath(path); - } else { - usedBranch.deoptimizePath(path); } } diff --git a/src/ast/nodes/VariableDeclaration.ts b/src/ast/nodes/VariableDeclaration.ts index fafc136548c..0265bae99c6 100644 --- a/src/ast/nodes/VariableDeclaration.ts +++ b/src/ast/nodes/VariableDeclaration.ts @@ -122,7 +122,9 @@ export default class VariableDeclaration extends NodeBase { code.remove(this.end - 1, this.end); } separatorString += ';'; - if (lastSeparatorPos !== null) { + if (lastSeparatorPos === null) { + code.appendLeft(renderedContentEnd, separatorString); + } else { if ( code.original.charCodeAt(actualContentEnd - 1) === 10 /*"\n"*/ && (code.original.charCodeAt(this.end) === 10 /*"\n"*/ || @@ -139,8 +141,6 @@ export default class VariableDeclaration extends NodeBase { code.overwrite(lastSeparatorPos, lastSeparatorPos + 1, separatorString); code.remove(actualContentEnd, renderedContentEnd); } - } else { - code.appendLeft(renderedContentEnd, separatorString); } if (systemPatternExports.length > 0) { code.appendLeft( diff --git a/src/ast/nodes/shared/ClassNode.ts b/src/ast/nodes/shared/ClassNode.ts index d9a7ab72c56..8550ae29ea6 100644 --- a/src/ast/nodes/shared/ClassNode.ts +++ b/src/ast/nodes/shared/ClassNode.ts @@ -89,9 +89,9 @@ export default class ClassNode extends NodeBase implements DeoptimizableEntity { ): boolean { return interaction.type === INTERACTION_CALLED && path.length === 0 ? !interaction.withNew || - (this.classConstructor !== null - ? this.classConstructor.hasEffectsOnInteractionAtPath(path, interaction, context) - : this.superClass?.hasEffectsOnInteractionAtPath(path, interaction, context)) || + (this.classConstructor === null + ? this.superClass?.hasEffectsOnInteractionAtPath(path, interaction, context) + : this.classConstructor.hasEffectsOnInteractionAtPath(path, interaction, context)) || false : this.getObjectEntity().hasEffectsOnInteractionAtPath(path, interaction, context); } diff --git a/src/ast/nodes/shared/ObjectEntity.ts b/src/ast/nodes/shared/ObjectEntity.ts index 4ee27f728f9..abbd908c75e 100644 --- a/src/ast/nodes/shared/ObjectEntity.ts +++ b/src/ast/nodes/shared/ObjectEntity.ts @@ -342,16 +342,7 @@ export class ObjectEntity extends ExpressionEntity { for (let index = properties.length - 1; index >= 0; index--) { const { key, kind, property } = properties[index]; allProperties.push(property); - if (typeof key !== 'string') { - if (key === UnknownInteger) { - unknownIntegerProps.push(property); - continue; - } - if (kind === 'set') unmatchableSetters.push(property); - if (kind === 'get') unmatchableGetters.push(property); - if (kind !== 'get') unmatchablePropertiesAndSetters.push(property); - if (kind !== 'set') unmatchablePropertiesAndGetters.push(property); - } else { + if (typeof key === 'string') { if (kind === 'set') { if (!propertiesAndSettersByKey[key]) { propertiesAndSettersByKey[key] = [property, ...unmatchablePropertiesAndSetters]; @@ -370,6 +361,15 @@ export class ObjectEntity extends ExpressionEntity { propertiesAndGettersByKey[key] = [property, ...unmatchablePropertiesAndGetters]; } } + } else { + if (key === UnknownInteger) { + unknownIntegerProps.push(property); + continue; + } + if (kind === 'set') unmatchableSetters.push(property); + if (kind === 'get') unmatchableGetters.push(property); + if (kind !== 'get') unmatchablePropertiesAndSetters.push(property); + if (kind !== 'set') unmatchablePropertiesAndGetters.push(property); } } } diff --git a/src/utils/FileEmitter.ts b/src/utils/FileEmitter.ts index 5de09725bc2..9faa59d5c5f 100644 --- a/src/utils/FileEmitter.ts +++ b/src/utils/FileEmitter.ts @@ -285,9 +285,9 @@ export class FileEmitter { private emitAsset(emittedAsset: EmittedFile): string { const source = - typeof emittedAsset.source !== 'undefined' - ? getValidSource(emittedAsset.source, emittedAsset, null) - : undefined; + emittedAsset.source === undefined + ? undefined + : getValidSource(emittedAsset.source, emittedAsset, null); const consumedAsset: ConsumedAsset = { fileName: emittedAsset.fileName, name: emittedAsset.name, diff --git a/src/utils/collapseSourcemaps.ts b/src/utils/collapseSourcemaps.ts index 773dab33e28..d847e41b7ae 100644 --- a/src/utils/collapseSourcemaps.ts +++ b/src/utils/collapseSourcemaps.ts @@ -176,9 +176,7 @@ function getCollapsedSourcemap( ): Source | Link { let source: Source | Link; - if (!originalSourcemap) { - source = new Source(id, originalCode); - } else { + if (originalSourcemap) { const sources = originalSourcemap.sources; const sourcesContent = originalSourcemap.sourcesContent || []; const directory = dirname(id) || '.'; @@ -188,6 +186,8 @@ function getCollapsedSourcemap( (source, index) => new Source(resolve(directory, sourceRoot, source), sourcesContent[index]) ); source = new Link(originalSourcemap, baseSources); + } else { + source = new Source(id, originalCode); } return sourcemapChain.reduce(linkMap, source); } diff --git a/src/utils/error.ts b/src/utils/error.ts index 2abf1cadcc6..9dcd2fe7525 100644 --- a/src/utils/error.ts +++ b/src/utils/error.ts @@ -460,7 +460,7 @@ export function errorInvalidOption( return { code: INVALID_OPTION, message: `Invalid value ${ - value !== undefined ? `${JSON.stringify(value)} ` : '' + value === undefined ? '' : `${JSON.stringify(value)} ` }for option "${option}" - ${explanation}.`, url: `https://rollupjs.org/guide/en/#${urlHash}` }; diff --git a/src/utils/options/normalizeInputOptions.ts b/src/utils/options/normalizeInputOptions.ts index 500012a5ebd..b684c197b98 100644 --- a/src/utils/options/normalizeInputOptions.ts +++ b/src/utils/options/normalizeInputOptions.ts @@ -281,7 +281,7 @@ const getHasModuleSideEffects = ( } if (typeof moduleSideEffectsOption === 'function') { return (id, external) => - !id.startsWith('\0') ? moduleSideEffectsOption(id, external) !== false : true; + id.startsWith('\0') ? true : moduleSideEffectsOption(id, external) !== false; } if (Array.isArray(moduleSideEffectsOption)) { const ids = new Set(moduleSideEffectsOption); diff --git a/src/utils/relativeId.ts b/src/utils/relativeId.ts index 6914f371be0..5c0e46b2d74 100644 --- a/src/utils/relativeId.ts +++ b/src/utils/relativeId.ts @@ -36,5 +36,5 @@ export function getImportPath( return [...relativePath.split('/'), '..', basename(targetPath)].join('/'); } } - return !relativePath ? '.' : relativePath.startsWith('..') ? relativePath : './' + relativePath; + return relativePath ? (relativePath.startsWith('..') ? relativePath : './' + relativePath) : '.'; } diff --git a/test/function/index.js b/test/function/index.js index c632d8e13fe..7adb8f2f182 100644 --- a/test/function/index.js +++ b/test/function/index.js @@ -27,13 +27,13 @@ function runCodeSplitTest(codeMap, entryId, configContext) { return exportsMap[outputId]; } const code = codeMap[outputId]; - return typeof code !== 'undefined' - ? (exportsMap[outputId] = requireWithContext( + return code === undefined + ? require(importee) + : (exportsMap[outputId] = requireWithContext( code, { require: requireFromOutputVia(outputId), ...context }, (exportsMap[outputId] = {}) - )) - : require(importee); + )); }; const context = { assert, ...configContext };