From b37315e77aa09f4ade7c3e71a1f93e1cad8781ac Mon Sep 17 00:00:00 2001 From: Zearin Date: Sun, 30 Jun 2024 13:01:44 -0400 Subject: [PATCH] refactor: Use `?.` for terser conditionals --- src/EleventyFiles.js | 2 +- src/Engines/JavaScript.js | 2 +- src/Engines/Nunjucks.js | 4 ++-- src/Filters/GetCollectionItem.js | 2 +- src/GlobalDependencyMap.js | 4 ++-- src/Plugins/Pagination.js | 2 +- src/Plugins/RenderPlugin.js | 2 +- src/Template.js | 4 ++-- src/TemplateConfig.js | 8 ++++---- src/TemplateContent.js | 2 +- src/TemplateMap.js | 6 +++--- src/TemplatePassthroughManager.js | 2 +- src/UserConfig.js | 2 +- 13 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/EleventyFiles.js b/src/EleventyFiles.js index e531c399c..60567de45 100644 --- a/src/EleventyFiles.js +++ b/src/EleventyFiles.js @@ -176,7 +176,7 @@ class EleventyFiles { this.uniqueIgnores = this.getIgnoreGlobs(); // Conditional added for tests that don’t have a config - if (this.config && this.config.events) { + if (this.config?.events) { this.config.events.emit("eleventy.ignores", this.uniqueIgnores); } diff --git a/src/Engines/JavaScript.js b/src/Engines/JavaScript.js index 77eede3bd..59c4e2ad2 100644 --- a/src/Engines/JavaScript.js +++ b/src/Engines/JavaScript.js @@ -135,7 +135,7 @@ class JavaScript extends TemplateEngine { static wrapJavaScriptFunction(inst, fn) { return function (...args) { for (let key of JavaScript.DATA_KEYS_TO_BIND) { - if (inst && inst[key]) { + if (inst?.[key]) { this[key] = inst[key]; } } diff --git a/src/Engines/Nunjucks.js b/src/Engines/Nunjucks.js index 588b9f5c6..c59b0205c 100755 --- a/src/Engines/Nunjucks.js +++ b/src/Engines/Nunjucks.js @@ -101,10 +101,10 @@ class Nunjucks extends TemplateEngine { static wrapFilter(name, fn) { return function (...args) { - if (this.ctx && this.ctx.page) { + if (this.ctx?.page) { this.page = this.ctx.page; } - if (this.ctx && this.ctx.eleventy) { + if (this.ctx?.eleventy) { this.eleventy = this.ctx.eleventy; } diff --git a/src/Filters/GetCollectionItem.js b/src/Filters/GetCollectionItem.js index ac67a14fe..7512940b6 100644 --- a/src/Filters/GetCollectionItem.js +++ b/src/Filters/GetCollectionItem.js @@ -12,7 +12,7 @@ export default function getCollectionItem(collection, page, modifier = 0) { j++; } - if (index !== undefined && collection && collection.length) { + if (index !== undefined && collection?.length) { if (index + modifier >= 0 && index + modifier < collection.length) { return collection[index + modifier]; } diff --git a/src/GlobalDependencyMap.js b/src/GlobalDependencyMap.js index 2b2f25769..f66af2f81 100644 --- a/src/GlobalDependencyMap.js +++ b/src/GlobalDependencyMap.js @@ -236,7 +236,7 @@ class GlobalDependencyMap { this.map.dependantsOf(node).forEach((node) => { let data = this.map.getNodeData(node); // we only want layouts - if (data && data.type && data.type === GlobalDependencyMap.LAYOUT_KEY) { + if (data?.type === GlobalDependencyMap.LAYOUT_KEY) { return layouts.push(node); } }); @@ -289,7 +289,7 @@ class GlobalDependencyMap { // When includeLayouts is `false` we want to filter out layouts let data = this.map.getNodeData(node); - if (data && data.type && data.type === GlobalDependencyMap.LAYOUT_KEY) { + if (data?.type === GlobalDependencyMap.LAYOUT_KEY) { return false; } return true; diff --git a/src/Plugins/Pagination.js b/src/Plugins/Pagination.js index 8353f6113..bf8d865c8 100755 --- a/src/Plugins/Pagination.js +++ b/src/Plugins/Pagination.js @@ -178,7 +178,7 @@ class Pagination { } const chunks = lodashChunk(this.target, this.size); - if (this.data.pagination && this.data.pagination.generatePageOnEmptyData) { + if (this.data.pagination?.generatePageOnEmptyData) { return chunks.length ? chunks : [[]]; } else { return chunks; diff --git a/src/Plugins/RenderPlugin.js b/src/Plugins/RenderPlugin.js index a6f0869f4..29d80cf5f 100644 --- a/src/Plugins/RenderPlugin.js +++ b/src/Plugins/RenderPlugin.js @@ -258,7 +258,7 @@ function eleventyRenderPlugin(eleventyConfig, options = {}) { let [context, ...argArray] = args; let normalizedContext = {}; - if (context.ctx && context.ctx.page) { + if (context.ctx?.page) { normalizedContext.ctx = context.ctx; // TODO .data diff --git a/src/Template.js b/src/Template.js index 4ad76218e..bee7e881d 100755 --- a/src/Template.js +++ b/src/Template.js @@ -747,7 +747,7 @@ class Template extends TemplateContent { rawInput, }; - if (data && this.config.dataFilterSelectors && this.config.dataFilterSelectors.size > 0) { + if (data && this.config.dataFilterSelectors?.size > 0) { ret.data = this.retrieveDataForJsonOutput(data, this.config.dataFilterSelectors); } @@ -811,7 +811,7 @@ class Template extends TemplateContent { content: content, }; - if (this.config.dataFilterSelectors && this.config.dataFilterSelectors.size > 0) { + if (this.config.dataFilterSelectors?.size > 0) { obj.data = this.retrieveDataForJsonOutput(page.data, this.config.dataFilterSelectors); } diff --git a/src/TemplateConfig.js b/src/TemplateConfig.js index dcc76324e..4d82708ed 100644 --- a/src/TemplateConfig.js +++ b/src/TemplateConfig.js @@ -148,7 +148,7 @@ class TemplateConfig { } getLocalProjectConfigFiles() { - if (this.projectConfigPaths && this.projectConfigPaths.length > 0) { + if (this.projectConfigPaths?.length > 0) { return TemplatePath.addLeadingDotSlashArray(this.projectConfigPaths.filter((path) => path)); } return []; @@ -425,7 +425,7 @@ class TemplateConfig { } // `templateFormats` is an override via `setTemplateFormats` - if (this.userConfig && this.userConfig.templateFormats) { + if (this.userConfig?.templateFormats) { this.templateFormats.setViaConfig(this.userConfig.templateFormats); } else if (localConfig?.templateFormats || this.rootConfig?.templateFormats) { // Local project config or defaultConfig.js @@ -435,7 +435,7 @@ class TemplateConfig { } // `templateFormatsAdded` is additive via `addTemplateFormats` - if (this.userConfig && this.userConfig.templateFormatsAdded) { + if (this.userConfig?.templateFormatsAdded) { this.templateFormats.addViaConfig(this.userConfig.templateFormatsAdded); } @@ -475,7 +475,7 @@ class TemplateConfig { pluginsBench.after(); // Template formats added via plugins - if (this.userConfig && this.userConfig.templateFormatsAdded) { + if (this.userConfig?.templateFormatsAdded) { this.templateFormats.addViaConfig(this.userConfig.templateFormatsAdded); mergedConfig.templateFormats = Object.freeze(this.templateFormats.getTemplateFormats()); } diff --git a/src/TemplateContent.js b/src/TemplateContent.js index 540509b28..a2c21e45a 100644 --- a/src/TemplateContent.js +++ b/src/TemplateContent.js @@ -573,7 +573,7 @@ class TemplateContent { `> Render > ${this.inputPath}${this._getPaginationLogSuffix(data)}`, ); let outputPathBenchmark; - if (data.page && data.page.outputPath && logRenderToOutputBenchmark) { + if (data.page?.outputPath && logRenderToOutputBenchmark) { outputPathBenchmark = this.bench.get(`> Render to > ${data.page.outputPath}`); } diff --git a/src/TemplateMap.js b/src/TemplateMap.js index 73a2c8cce..016d620ea 100644 --- a/src/TemplateMap.js +++ b/src/TemplateMap.js @@ -85,7 +85,7 @@ class TemplateMap { * --- */ isPaginationOverAllCollections(entry) { - if (entry.data.pagination && entry.data.pagination.data) { + if (entry.data.pagination?.data) { return ( entry.data.pagination.data === "collections" || entry.data.pagination.data === "collections.all" @@ -94,7 +94,7 @@ class TemplateMap { } getPaginationTagTarget(entry) { - if (entry.data.pagination && entry.data.pagination.data) { + if (entry.data.pagination?.data) { return this.getTagTarget(entry.data.pagination.data); } } @@ -344,7 +344,7 @@ class TemplateMap { } let precompiled = this.config.precompiledCollections; - if (precompiled && precompiled[tagName]) { + if (precompiled?.[tagName]) { if ( tagName === "all" || !Array.isArray(this.collectionsData[tagName]) || diff --git a/src/TemplatePassthroughManager.js b/src/TemplatePassthroughManager.js index a66d0ac25..530ff3188 100644 --- a/src/TemplatePassthroughManager.js +++ b/src/TemplatePassthroughManager.js @@ -250,7 +250,7 @@ class TemplatePassthroughManager { } } - if (paths && paths.length) { + if (paths?.length) { let passthroughPaths = this.getNonTemplatePaths(paths); for (let path of passthroughPaths) { let normalizedPath = this._normalizePaths(path); diff --git a/src/UserConfig.js b/src/UserConfig.js index 30b2acc4f..c4d364654 100644 --- a/src/UserConfig.js +++ b/src/UserConfig.js @@ -492,7 +492,7 @@ class UserConfig { let configFunction = plugin; ret = configFunction(this, options); pluginBenchmark.after(); - } else if (plugin && plugin.configFunction) { + } else if (plugin?.configFunction) { pluginBenchmark.before(); if (options && typeof options.init === "function") {