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

🏗🐛 Lint the contents of validator/ #15444

Merged
merged 3 commits into from
May 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Auto-fix all possible lint errors in validator
  • Loading branch information
rsimha committed May 19, 2018
commit bffd401a7e2a0d10c7d99a482e371d83bd4d7763
114 changes: 57 additions & 57 deletions validator/chromeextension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,27 @@
* See the License for the specific language governing permissions and
* limitations under the license.
*/
var globals = {};
globals.ampCacheBgcolor = "#ffffff";
globals.ampCacheIconPrefix = "amp-link";
globals.ampCacheTitle = chrome.i18n.getMessage("pageFromAmpCacheTitle");
globals.invalidAmpBgcolor = "#8b0000";
globals.invalidAmpIconPrefix = "invalid";
globals.invalidAmpTitle = chrome.i18n.getMessage("pageFailsValidationTitle");
globals.linkToAmpBgColor = "#ffffff";
globals.linkToAmpIconPrefix = "amp-link";
globals.linkToAmpTitle = chrome.i18n.getMessage("pageHasAmpAltTitle");
const globals = {};
globals.ampCacheBgcolor = '#ffffff';
globals.ampCacheIconPrefix = 'amp-link';
globals.ampCacheTitle = chrome.i18n.getMessage('pageFromAmpCacheTitle');
globals.invalidAmpBgcolor = '#8b0000';
globals.invalidAmpIconPrefix = 'invalid';
globals.invalidAmpTitle = chrome.i18n.getMessage('pageFailsValidationTitle');
globals.linkToAmpBgColor = '#ffffff';
globals.linkToAmpIconPrefix = 'amp-link';
globals.linkToAmpTitle = chrome.i18n.getMessage('pageHasAmpAltTitle');
globals.tabToUrl = {};
globals.userAgentHeader = 'X-AMP-Validator-UA';
globals.validAmpBgcolor = "#ffd700";
globals.validAmpIconPrefix = "valid";
globals.validAmpTitle = chrome.i18n.getMessage("pagePassesValidationTitle");
globals.validatorNotPresentBadge = chrome.i18n.getMessage("validatorNotPresentBadge");
globals.validatorNotPresentBgColor = "#b71c1c";
globals.validatorNotPresentIconPrefix = "validator-not-present";
globals.validatorNotPresentPopup = "popup-validator-not-present.build.html";
globals.validatorNotPresentTitle = chrome.i18n.getMessage("validatorNotPresentTitle");
globals.validatorPopup = "popup-validator.build.html";
globals.validAmpBgcolor = '#ffd700';
globals.validAmpIconPrefix = 'valid';
globals.validAmpTitle = chrome.i18n.getMessage('pagePassesValidationTitle');
globals.validatorNotPresentBadge = chrome.i18n.getMessage('validatorNotPresentBadge');
globals.validatorNotPresentBgColor = '#b71c1c';
globals.validatorNotPresentIconPrefix = 'validator-not-present';
globals.validatorNotPresentPopup = 'popup-validator-not-present.build.html';
globals.validatorNotPresentTitle = chrome.i18n.getMessage('validatorNotPresentTitle');
globals.validatorPopup = 'popup-validator.build.html';

/**
* Format a hex value (HTML colors such as #ffffff) as an RGBA.
Expand All @@ -44,9 +44,9 @@ globals.validatorPopup = "popup-validator.build.html";
*/
function hex2rgba(hex) {
// Remove the '#' char if necessary.
if (hex.charAt(0) === "#") { hex = hex.slice(1); }
if (hex.charAt(0) === '#') { hex = hex.slice(1); }
hex = hex.toUpperCase();
var hexAlpha = "0123456789ABCDEF", value = new Array(4), k = 0, int1, int2, i;
let hexAlpha = '0123456789ABCDEF', value = new Array(4), k = 0, int1, int2, i;
for (i = 0; i < 6; i += 2) {
int1 = hexAlpha.indexOf(hex.charAt(i));
int2 = hexAlpha.indexOf(hex.charAt(i + 1));
Expand All @@ -65,11 +65,11 @@ function hex2rgba(hex) {
* @return {Object}
*/
function getErrorSeverityCounts(errors) {
var numErrors = 0;
var numWarnings = 0;
for (var error in errors) {
if (errors[error].severity == 'ERROR') numErrors += 1;
if (errors[error].severity == 'WARNING') numWarnings += 1;
let numErrors = 0;
let numWarnings = 0;
for (const error in errors) {
if (errors[error].severity == 'ERROR') {numErrors += 1;}
if (errors[error].severity == 'WARNING') {numWarnings += 1;}
}
return {'ERROR': numErrors, 'WARNING': numWarnings};
}
Expand Down Expand Up @@ -123,7 +123,7 @@ function handleAmpCache(tabId, ampHref) {
* @param {!Object<!ValidationResult>} validationResult
*/
function handleAmpFail(tabId, validationResult) {
var numErrors = getNumberOfErrors(validationResult.errors);
const numErrors = getNumberOfErrors(validationResult.errors);
updateTabStatus(
tabId, globals.invalidAmpIconPrefix, globals.invalidAmpTitle,
numErrors.toString(), globals.invalidAmpBgcolor);
Expand Down Expand Up @@ -158,13 +158,13 @@ function handleAmpLink(tabId, ampHref) {
* @param {!Object<!ValidationResult>} validationResult
*/
function handleAmpPass(tabId, validationResult) {
var badgeTitle = '';
var numWarnings = getNumberOfWarnings(validationResult.errors);
if (numWarnings > 0) badgeTitle = numWarnings.toString();
let badgeTitle = '';
const numWarnings = getNumberOfWarnings(validationResult.errors);
if (numWarnings > 0) {badgeTitle = numWarnings.toString();}
updateTabStatus(
tabId, globals.validAmpIconPrefix, globals.validAmpTitle,
badgeTitle, globals.validAmpBgcolor);
if (numWarnings > 0) updateTabPopup(tabId);
if (numWarnings > 0) {updateTabPopup(tabId);}
}

function handleValidatorNotPresent(tabId) {
Expand All @@ -174,7 +174,7 @@ function handleValidatorNotPresent(tabId) {
chrome.tabs.get(tabId, function(tab) {
if (!chrome.runtime.lastError) {
chrome.browserAction.setPopup(
{tabId: tabId, popup: globals.validatorNotPresentPopup});
{tabId, popup: globals.validatorNotPresentPopup});
}
});
}
Expand All @@ -197,17 +197,17 @@ function isForbiddenUrl(url) {
*/
function updateTab(tab) {
if (!isForbiddenUrl(tab.url))
chrome.tabs.sendMessage(
tab.id, {'getAmpDetails': true}, function(response) {
if (response && response.fromAmpCache && response.ampHref) {
handleAmpCache(tab.id, response.ampHref);
} else if (response && response.isAmp) {
validateUrlFromTab(tab, response.userAgent);
} else if (response && !response.isAmp && response.ampHref) {
handleAmpLink(tab.id, response.ampHref);
}
{chrome.tabs.sendMessage(
tab.id, {'getAmpDetails': true}, function(response) {
if (response && response.fromAmpCache && response.ampHref) {
handleAmpCache(tab.id, response.ampHref);
} else if (response && response.isAmp) {
validateUrlFromTab(tab, response.userAgent);
} else if (response && !response.isAmp && response.ampHref) {
handleAmpLink(tab.id, response.ampHref);
}
);
}
);}
}

/**
Expand All @@ -220,7 +220,7 @@ function updateTabPopup(tabId) {
chrome.tabs.get(tabId, function(tab) {
if (!chrome.runtime.lastError) {
chrome.browserAction.setPopup(
{tabId: tabId, popup: globals.validatorPopup});
{tabId, popup: globals.validatorPopup});
}
});
}
Expand All @@ -238,16 +238,16 @@ function updateTabStatus(tabId, iconPrefix, title, text, color) {
// Verify tab still exists
chrome.tabs.get(tabId, function(tab) {
if (!chrome.runtime.lastError) {
chrome.browserAction.setIcon({path: {"19": iconPrefix + "-128.png",
"38": iconPrefix + "-38.png"},
tabId: tabId});
chrome.browserAction.setIcon({path: {'19': iconPrefix + '-128.png',
'38': iconPrefix + '-38.png'},
tabId});
if (title !== undefined)
chrome.browserAction.setTitle({title: title, tabId: tabId});
{chrome.browserAction.setTitle({title, tabId});}
if (text !== undefined)
chrome.browserAction.setBadgeText({text: text, tabId: tabId});
{chrome.browserAction.setBadgeText({text, tabId});}
if (color !== undefined)
chrome.browserAction.setBadgeBackgroundColor(
{color: hex2rgba(color), tabId: tabId});
{chrome.browserAction.setBadgeBackgroundColor(
{color: hex2rgba(color), tabId});}
}
});
}
Expand All @@ -265,8 +265,8 @@ function validateUrlFromTab(tab, userAgent) {
handleValidatorNotPresent(tab.id);
return;
}
var xhr = new XMLHttpRequest();
var url = tab.url.split('#')[0];
const xhr = new XMLHttpRequest();
const url = tab.url.split('#')[0];
xhr.open('GET', url, true);

// We can't set the User-Agent header directly, but we can set this header
Expand All @@ -276,9 +276,9 @@ function validateUrlFromTab(tab, userAgent) {
// traffic, so this approach will interfere as little as possible with the
// 99.9% of requests which aren't for AMP validation.
chrome.webRequest.onBeforeSendHeaders.addListener(
updateSendHeadersUserAgent,
{urls: [url], types: ["xmlhttprequest"], tabId: -1},
["requestHeaders", "blocking"]
updateSendHeadersUserAgent,
{urls: [url], types: ['xmlhttprequest'], tabId: -1},
['requestHeaders', 'blocking']
);
// Add the temporary header to the request
xhr.setRequestHeader(globals.userAgentHeader, userAgent);
Expand Down Expand Up @@ -313,7 +313,7 @@ function validateUrlFromTab(tab, userAgent) {
*/
function updateSendHeadersUserAgent(details) {
let newUserAgent,
headers = details.requestHeaders;
headers = details.requestHeaders;
// Using var instead of let keeps the index in scope for later
for (var i = 0; i < headers.length; i++) {
if (headers[i].name === globals.userAgentHeader) {
Expand Down Expand Up @@ -371,4 +371,4 @@ chrome.tabs.onReplaced.addListener(function(addedTabId, removedTabId) {
/**
* Reload every hour to retrieve the most recent AMP Validator.
*/
window.setTimeout(() => { location.reload(); } , 60*60*1000);
window.setTimeout(() => { location.reload(); } , 60 * 60 * 1000);
30 changes: 15 additions & 15 deletions validator/chromeextension/content_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the license.
*/
var globals = {};
const globals = {};
globals.amphtmlRegex = new RegExp('(^\s*)amphtml(\s*$)');
globals.ampCaches = [
{
Expand All @@ -32,7 +32,7 @@ globals.ampCaches = [
'isAmpCache': function() {
return window.location.hostname.endsWith('cdn.ampproject.org');
},
}
},
];

/**
Expand All @@ -42,8 +42,8 @@ globals.ampCaches = [
* @private
*/
function getAmpCacheHref() {
for (var index in globals.ampCaches) {
var ampCache = globals.ampCaches[index];
for (const index in globals.ampCaches) {
const ampCache = globals.ampCaches[index];
if (ampCache.isAmpCache()) {
return ampCache.getAmpHref();
}
Expand All @@ -59,17 +59,17 @@ function getAmpCacheHref() {
* @private
*/
function getAmpHtmlLinkHref() {
var ampHtmlLinkHref = '';
var headLinks = document.head.getElementsByTagName('link');
let ampHtmlLinkHref = '';
const headLinks = document.head.getElementsByTagName('link');
if (headLinks.length > 0) {
for (var index in headLinks) {
var link = headLinks[index];
for (const index in headLinks) {
const link = headLinks[index];
if (link instanceof HTMLLinkElement &&
link.hasAttribute('rel') &&
globals.amphtmlRegex.test(link.getAttribute('rel')) &&
link.hasAttribute('href')) {
ampHtmlLinkHref = link.getAttribute('href');
break;
ampHtmlLinkHref = link.getAttribute('href');
break;
}
}
}
Expand All @@ -83,8 +83,8 @@ function getAmpHtmlLinkHref() {
* @private
*/
function isAmpCache() {
for (var index in globals.ampCaches) {
var ampCache = globals.ampCaches[index];
for (const index in globals.ampCaches) {
const ampCache = globals.ampCaches[index];
if (ampCache.isAmpCache()) {
return true;
}
Expand Down Expand Up @@ -120,9 +120,9 @@ chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.getAmpDetails) {
const isAmp = isAmpDocument();
const fromAmpCache = isAmpCache();
var ampHref = '';
if (!isAmp) ampHref = getAmpHtmlLinkHref();
if (fromAmpCache) ampHref = getAmpCacheHref();
let ampHref = '';
if (!isAmp) {ampHref = getAmpHtmlLinkHref();}
if (fromAmpCache) {ampHref = getAmpCacheHref();}
sendResponse({
'isAmp': isAmp, 'fromAmpCache': fromAmpCache, 'ampHref': ampHref,
'userAgent': navigator.userAgent,
Expand Down
10 changes: 5 additions & 5 deletions validator/engine/amp4ads-parse-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
}
this.errors.push(createParseErrorTokenAt(
declaration, amp.validator.ValidationError.Code
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE,
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE,
['style', 'position', ident]));
}
}
Expand All @@ -112,10 +112,10 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
}
this.errors.push(createParseErrorTokenAt(
decl, amp.validator.ValidationError.Code
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE_WITH_HINT,
.CSS_SYNTAX_DISALLOWED_PROPERTY_VALUE_WITH_HINT,
[
'style', 'transition', transitionedProperty,
'[\'opacity\', \'transform\']'
'[\'opacity\', \'transform\']',
]));
}
}
Expand All @@ -130,10 +130,10 @@ class Amp4AdsVisitor extends parse_css.RuleVisitor {
}
this.errors.push(createParseErrorTokenAt(
decl, amp.validator.ValidationError.Code
.CSS_SYNTAX_PROPERTY_DISALLOWED_WITHIN_AT_RULE,
.CSS_SYNTAX_PROPERTY_DISALLOWED_WITHIN_AT_RULE,
[
'style', decl.name, this.inKeyframes.name,
'[\'animation-timing-function\', \'opacity\', \'transform\']'
'[\'animation-timing-function\', \'opacity\', \'transform\']',
]));
}
}
Expand Down
Loading