Skip to content

Commit

Permalink
refactor: Use ?. for terser conditionals (#3343)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 5, 2024
2 parents 9528b38 + b37315e commit b76553a
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/EleventyFiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Engines/JavaScript.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,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];
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Engines/Nunjucks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Filters/GetCollectionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
4 changes: 2 additions & 2 deletions src/GlobalDependencyMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Plugins/RenderPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

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

Expand Down
8 changes: 4 additions & 4 deletions src/TemplateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}

Expand Down
6 changes: 3 additions & 3 deletions src/TemplateMap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand 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);
}
}
Expand Down Expand Up @@ -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]) ||
Expand Down
2 changes: 1 addition & 1 deletion src/TemplatePassthroughManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/UserConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,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") {
Expand Down

0 comments on commit b76553a

Please sign in to comment.