Skip to content

Commit

Permalink
Bring back the type compatibility check
Browse files Browse the repository at this point in the history
  • Loading branch information
papandreou committed Apr 4, 2019
1 parent 7bc868c commit 58f9f88
Showing 1 changed file with 44 additions and 24 deletions.
68 changes: 44 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,21 @@ const defaultSkipFilters = [require('./known-culprits/linkedin')];

const hyperlinkUserAgent = `Hyperlink v${version} (https://www.npmjs.com/package/hyperlink)`;

function checkCompatibility(asset, Class) {
if (typeof Class === 'undefined') {
Class = AssetGraph.Asset;
} else if (typeof Class === 'string') {
Class = AssetGraph[Class];
}
return (
asset instanceof Class ||
!asset._type ||
Class.prototype instanceof AssetGraph[asset._type] ||
!!(asset.isImage && Class === AssetGraph.Image) || // Svg is modelled as a subclass of Xml, not Image
!!(asset.isImage && Class === AssetGraph.Font) // Svg can be used as a font as well
);
}

function returnFalse() {
return false;
}
Expand Down Expand Up @@ -353,30 +368,6 @@ async function hyperlink(
}

function handleError(error) {
// Explicitly handle incompatible types warning
if (error.stack && error.stack.includes('_warnIncompatibleTypes')) {
const asset = error.asset;
const expected =
asset.contentType || `A Content-Type compatible with ${asset.type}`;

const contentTypeMismatchReport = {
ok: false,
operator: 'content-type-mismatch',
name: `content-type-mismatch ${asset.urlOrDescription}`,
expected,
actual: error.message,
at: [...new Set(asset._incoming.map(r => r.debugDescription))].join(
'\n '
)
};

if (!shouldSkip(contentTypeMismatchReport)) {
reportTest(contentTypeMismatchReport);
}

return;
}

const message = error.message || error;
const asset = error.asset || (error.relation && error.relation.to);
const report = {
Expand Down Expand Up @@ -732,6 +723,35 @@ async function hyperlink(
);
}

// Check Content-Type vs. incoming relation targetTypes:

for (const asset of ag.findAssets({ expectedTypes: { $exists: true } })) {
const incompatibleTypes = [...asset.expectedTypes].filter(
expectedType => !checkCompatibility(asset, expectedType)
);
if (incompatibleTypes.length > 0) {
const expected =
asset.contentType || `A Content-Type compatible with ${asset.type}`;

const contentTypeMismatchReport = {
ok: false,
operator: 'content-type-mismatch',
name: `content-type-mismatch ${asset.urlOrDescription}`,
expected,
actual: `Asset is used as both ${[...incompatibleTypes, asset.type]
.sort()
.join(' and ')}`,
at: [...new Set(asset._incoming.map(r => r.debugDescription))].join(
'\n '
)
};

if (!shouldSkip(contentTypeMismatchReport)) {
reportTest(contentTypeMismatchReport);
}
}
}

// Check preconnects:

const preconnectAssetsToCheck = ag.findAssets({
Expand Down

0 comments on commit 58f9f88

Please sign in to comment.