Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linter output changes #779

Merged
merged 3 commits into from
May 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/linter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { SxgDumpSignedExchangeVerify } from "./rules/SxgDumpSignedExchangeVerify
import { SxgAmppkgIsForwarded } from "./rules/SxgAmppkgIsForwarded";
import { MetadataIncludesOGImageSrc } from "./rules/MetadataIncludesOGImageSrc";
import { ImagesHaveAltText } from "./rules/ImagesHaveAltText";
import { IsValid } from "./rules/IsValid";
import { RuleConstructor } from "./rule";
import { isArray } from "util";

Expand Down Expand Up @@ -77,12 +78,14 @@ export function guessMode($: CheerioStatic): LintMode {
function testsForMode(type: LintMode) {
const tests: Map<LintMode, Array<RuleConstructor>> = new Map();
tests.set(LintMode.Sxg, [
IsValid,
SxgAmppkgIsForwarded,
SxgContentNegotiationIsOk,
SxgVaryOnAcceptAct,
SxgDumpSignedExchangeVerify,
]);
tests.set(LintMode.Amp, [
IsValid,
AmpVideoIsSmall,
AmpVideoIsSpecifiedByAttribute,
MetaCharsetIsFirst,
Expand All @@ -95,6 +98,7 @@ function testsForMode(type: LintMode) {
tests.set(
LintMode.AmpStory,
(tests.get(LintMode.Amp) || []).concat([
IsValid,
LinkRelCanonicalIsOk,
BookendExists,
SchemaMetadataIsNews,
Expand Down
6 changes: 2 additions & 4 deletions packages/linter/src/rules/StoryMetadataIsV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export class StoryMetadataIsV1 extends Rule {
.filter(Boolean) as string[];
if (attr.length > 0) {
return this.warn(
`<amp-story> is missing attribute(s) that will soon be mandatory: [${attr.join(
", "
)}]`
`<amp-story> is missing required attribute(s): [${attr.join(", ")}]`
);
} else {
return this.pass();
Expand All @@ -31,7 +29,7 @@ export class StoryMetadataIsV1 extends Rule {
return {
url:
"https://amp.dev/documentation/components/amp-story/#new-metadata-requirements",
title: "Preview metadata is specified correctly",
title: "Required story metadata attributes in <amp-story> tag",
info: "",
};
}
Expand Down
36 changes: 26 additions & 10 deletions packages/linter/src/rules/StoryMetadataThumbnailsAreOk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ function inlineMetadata($: CheerioStatic) {
};
return metadata;
}
const outputMessageMap: { [key: string]: string } = {
isPortrait: " a 3:4 aspect ratio",
isSquare: " a 1:1 aspect ratio",
isRaster: " of type .jpeg, .gif, .png, or .webp",
isLandscape: " a 4:3 aspect ratio",
isAtLeast96x96: " 96x96 or larger",
isAtLeast696x928: " 696x928px or larger",
isAtLeast928x696: " 928x696px or larger",
isAtLeast928x928: " 928x928px or larger",
};

export class StoryMetadataThumbnailsAreOk extends Rule {
async run(context: Context) {
Expand Down Expand Up @@ -55,21 +65,14 @@ export class StoryMetadataThumbnailsAreOk extends Rule {
): Promise<Result> => {
const url = metadata[attr];
if (!url) {
return isMandatory ? this.fail(`[${attr}] is missing`) : this.pass();
return isMandatory ? this.fail(`${attr} is missing`) : this.pass();
}
try {
const info = await dimensions(context, url);
const failed = expected.filter((fn) => !fn(info)).map((fn) => fn.name);
return failed.length === 0
? this.pass()
: this.fail(
`[${attr} = ${JSON.stringify({
url: url,
width: info.width,
height: info.height,
mime: info.mime,
})}] failed [${failed.join(", ")}]`
);
: this.fail(formatForHumans(attr.toString(), url, failed.join(", ")));
} catch (e) {
const s = absoluteUrl(url, context.url);
switch (e.message) {
Expand All @@ -82,6 +85,19 @@ export class StoryMetadataThumbnailsAreOk extends Rule {
}
}
};
let formatForHumans: (
attr: string,
url: any,
failed: string
) => string = function (attr: string, url: any, failed: string) {
let m = `${attr} should be`;
failed.split(",").forEach(function (el) {
m = m + outputMessageMap[el] + " and";
});
//Remove the last ' and' + tack on the src
m = m.slice(0, m.length - 4) + `\nsrc: ${url}`;
return m;
};
const res = [
assert("publisher-logo-src", true, [isRaster, isSquare, isAtLeast96x96]),
assert("poster-portrait-src", true, [
Expand All @@ -106,7 +122,7 @@ export class StoryMetadataThumbnailsAreOk extends Rule {
return {
url:
"https://amp.dev/documentation/components/amp-story/#new-metadata-requirements",
title: "Preview metadata is specified correctly",
title: "AMP Story preview metadata is correct size and aspect ratio",
info: "",
};
}
Expand Down