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

Enforce no mix interpolation #19700

Merged
merged 4 commits into from Dec 20, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Expand Up @@ -63,7 +63,7 @@
"amphtml-internal/no-is-amp-alt": 2,
"amphtml-internal/no-mixed-operators": 2,
"amphtml-internal/no-module-exports": 2,
"amphtml-internal/no-non-string-log-args": 0,
"amphtml-internal/no-mixed-interpolation": 2,
"amphtml-internal/no-spread": 2,
"amphtml-internal/no-style-display": 2,
"amphtml-internal/no-style-property-setting": 2,
Expand Down
5 changes: 2 additions & 3 deletions ads/inabox/inabox-host.js
Expand Up @@ -56,8 +56,7 @@ export class InaboxHost {
setReportError(reportError);

if (win[INABOX_IFRAMES] && !Array.isArray(win[INABOX_IFRAMES])) {
dev().info(TAG, `Invalid ${INABOX_IFRAMES}`,
win[INABOX_IFRAMES]);
dev().info(TAG, 'Invalid %s. %s', INABOX_IFRAMES, win[INABOX_IFRAMES]);
win[INABOX_IFRAMES] = [];
}
const host = new InaboxMessagingHost(win, win[INABOX_IFRAMES]);
Expand Down Expand Up @@ -88,7 +87,7 @@ export class InaboxHost {
processMessageFn(message);
});
} else {
dev().info(TAG, `Invalid ${PENDING_MESSAGES}`, queuedMsgs);
dev().info(TAG, 'Invalid %s %s', PENDING_MESSAGES, queuedMsgs);
}
}
// Empty and ensure that future messages are no longer stored in the array.
Expand Down
2 changes: 1 addition & 1 deletion ads/inabox/inabox-messaging-host.js
Expand Up @@ -178,7 +178,7 @@ export class InaboxMessagingHost {
* @param {?JsonObject} data
*/
sendPosition_(request, source, origin, data) {
dev().fine(TAG, `Sent position data to [${request.sentinel}]`, data);
dev().fine(TAG, 'Sent position data to [%s] %s', request.sentinel, data);
source./*OK*/postMessage(
serializeMessage(MessageType.POSITION, request.sentinel, data),
origin);
Expand Down
136 changes: 136 additions & 0 deletions build-system/eslint-rules/no-mixed-interpolation.js
@@ -0,0 +1,136 @@
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const transformableMethods =
require('../log-module-metadata').transformableMethods;

/**
* @typedef {{
* name: string.
* variadle: boolean,
* startPos: number
* }}
*/
let LogMethodMetadataDef;

/**
* @param {!Node} node
* @return {boolean}
*/
function isBinaryConcat(node) {
return node.type === 'BinaryExpression' && node.operator === '+';
}

/**
* @param {!Node} node
* @return {boolean}
*/
function isLiteralString(node) {
return node.type === 'Literal' && typeof node.value === 'string';
}

/**
* @param {!Node} node
* @return {boolean}
*/
function hasTemplateLiteral(node) {
if (node.type === 'TemplateLiteral') {
return true;
}

// Allow for string concatenation operations.
if (isBinaryConcat(node)) {
return hasTemplateLiteral(node.left) && hasTemplateLiteral(node.right);
}

return false;
}

/**
* @param {string} name
* @return {!LogMethodMetadataDef}
*/
function getMetadata(name) {
return transformableMethods.find(cur => cur.name === name);
}

const selector = transformableMethods.map(method => {
return `CallExpression[callee.property.name=${method.name}]`;
}).join(',');


module.exports = {
create(context) {
return {
[selector]: function(node) {
// Don't evaluate or transform log.js
if (context.getFilename().endsWith('src/log.js')) {
return;
}
// Make sure that callee is a CallExpression as well.
// dev().assert() // enforce rule
// dev.assert() // ignore
const callee = node.callee;
const calleeObject = callee.object;
if (!calleeObject || calleeObject.type !== 'CallExpression') {
return;
}

// Make sure that the CallExpression is one of dev() or user().
if(!['dev', 'user'].includes(calleeObject.callee.name)) {
return;
}

const methodInvokedName = callee.property.name;
// Find the position of the argument we care about.
const metadata = getMetadata(methodInvokedName);

// If there's no metadata, this is most likely a test file running
// private methods on log.
if (!metadata) {
return;
}

const argToEval = node.arguments[metadata.startPos];

if (!argToEval) {
return;
}

let errMsg = [
'Mixing Template Strings and %s interpolation for log methods is',
`not supported on ${metadata.name}. Please either use template `,
'literals or use the log strformat(%s) style interpolation ',
'exclusively',
].join(' ');

// If method is not variadic we don't need to check.
if (!metadata.variadic) {
return;
}

const hasVariadicInterpolation = node.arguments[metadata.startPos + 1];

if (hasVariadicInterpolation && hasTemplateLiteral(argToEval)) {
context.report({
node: argToEval,
message: errMsg,
});
}
},
};
},
};
211 changes: 0 additions & 211 deletions build-system/eslint-rules/no-non-string-log-args.js

This file was deleted.