From 2418c6aca134a609e41debae284431022d0a621d Mon Sep 17 00:00:00 2001 From: Sonu Kapoor Date: Mon, 11 May 2020 07:41:25 -0400 Subject: [PATCH] refactor(docs-infra): refactors `addImageDimensionsImpl` (#37046) This commit changes the `addImageDimensionsImpl` method to reduce nesting. It will exit early, if the conditions are not matched. PR Close #37046 --- .../post-processors/add-image-dimensions.js | 39 ++++++++++--------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/aio/tools/transforms/angular-base-package/post-processors/add-image-dimensions.js b/aio/tools/transforms/angular-base-package/post-processors/add-image-dimensions.js index 075c0de44d..470652b12c 100644 --- a/aio/tools/transforms/angular-base-package/post-processors/add-image-dimensions.js +++ b/aio/tools/transforms/angular-base-package/post-processors/add-image-dimensions.js @@ -15,25 +15,28 @@ module.exports = function addImageDimensions(getImageDimensions) { return (ast, file) => { visit(ast, node => { - if (is(node, 'img')) { - const props = node.properties; - const src = props.src; - if (!src) { - file.message('Missing src in image tag `' + source(node, file) + '`'); + if (!is(node, 'img')) { + return; + } + + const props = node.properties; + const src = props.src; + if (!src) { + file.message('Missing src in image tag `' + source(node, file) + '`'); + return; + } + + try { + const dimensions = getImageDimensions(addImageDimensionsImpl.basePath, src); + if (props.width === undefined && props.height === undefined) { + props.width = '' + dimensions.width; + props.height = '' + dimensions.height; + } + } catch(e) { + if (e.code === 'ENOENT') { + file.fail('Unable to load src in image tag `' + source(node, file) + '`'); } else { - try { - const dimensions = getImageDimensions(addImageDimensionsImpl.basePath, src); - if (props.width === undefined && props.height === undefined) { - props.width = '' + dimensions.width; - props.height = '' + dimensions.height; - } - } catch(e) { - if (e.code === 'ENOENT') { - file.fail('Unable to load src in image tag `' + source(node, file) + '`'); - } else { - file.fail(e.message); - } - } + file.fail(e.message); } } });