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
This commit is contained in:
Sonu Kapoor 2020-05-11 07:41:25 -04:00 committed by Kara Erickson
parent 90205079d7
commit 2418c6aca1
1 changed files with 21 additions and 18 deletions

View File

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