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,12 +15,17 @@ module.exports = function addImageDimensions(getImageDimensions) {
return (ast, file) => {
visit(ast, node => {
if (is(node, 'img')) {
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) + '`');
} else {
return;
}
try {
const dimensions = getImageDimensions(addImageDimensionsImpl.basePath, src);
if (props.width === undefined && props.height === undefined) {
@ -34,8 +39,6 @@ module.exports = function addImageDimensions(getImageDimensions) {
file.fail(e.message);
}
}
}
}
});
};
};