Skip to content

Commit

Permalink
Apply new unicorn linting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalborczyk committed Dec 31, 2022
1 parent e79e92a commit 7f6d0c6
Show file tree
Hide file tree
Showing 17 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion 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
Expand Down
2 changes: 1 addition & 1 deletion cli/run/timings.ts
Expand Up @@ -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));
}
Expand Down
6 changes: 3 additions & 3 deletions scripts/perf.js
Expand Up @@ -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];
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Chunk.ts
Expand Up @@ -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']
Expand All @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/ModuleLoader.ts
Expand Up @@ -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 }) =>
Expand Down
6 changes: 3 additions & 3 deletions src/ast/nodes/ArrayExpression.ts
Expand Up @@ -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));
Expand Down
22 changes: 11 additions & 11 deletions src/ast/nodes/ConditionalExpression.ts
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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,
Expand All @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ast/nodes/LogicalExpression.ts
Expand Up @@ -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);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/ast/nodes/VariableDeclaration.ts
Expand Up @@ -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"*/ ||
Expand All @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions src/ast/nodes/shared/ClassNode.ts
Expand Up @@ -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);
}
Expand Down
20 changes: 10 additions & 10 deletions src/ast/nodes/shared/ObjectEntity.ts
Expand Up @@ -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];
Expand All @@ -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);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/utils/FileEmitter.ts
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions src/utils/collapseSourcemaps.ts
Expand Up @@ -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) || '.';
Expand All @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/error.ts
Expand Up @@ -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}`
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/options/normalizeInputOptions.ts
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/utils/relativeId.ts
Expand Up @@ -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) : '.';
}
8 changes: 4 additions & 4 deletions test/function/index.js
Expand Up @@ -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 };
Expand Down

0 comments on commit 7f6d0c6

Please sign in to comment.