-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
🐛 Fix instances where allowConsoleError
swallows return values
#15228
Conversation
@@ -596,6 +596,10 @@ const forbiddenTerms = { | |||
'extensions/amp-access/0.1/iframe-api/access-controller.js', | |||
], | |||
}, | |||
'(?<!return) allowConsoleError\\(\\(\\) => \\{[\\n\\r\\s]*return': { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lookbehinds are brand new, impossible to polyfill.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this a problem? This is just a presubmit check running on Travis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only available on Node 9, and needs a flag on Node 8. http://node.green/#ES2018-features--RegExp-Lookbehind-Assertions
@@ -596,6 +596,10 @@ const forbiddenTerms = { | |||
'extensions/amp-access/0.1/iframe-api/access-controller.js', | |||
], | |||
}, | |||
'(?<!return) allowConsoleError\\(\\(\\) => \\{[\\n\\r\\s]*return': { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
\\(\\)=> \\{[\\n\\r\\s]*return
fails to capture expression bodies, non-arrows, parameters, any anything that's not immediately returning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true. It's a simple check for the kind of usage we currently have. What do you suggest this be changed to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An eslint plugin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, that's out of scope of this PR, so I'll revert this presubmit check and cough let you write a new plugin since you're more familiar with this than I am :)
allowConsoleError
from swallowing return valuesallowConsoleError
swallows return values
/**
* 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.
*/
'use strict';
module.exports = {
meta: {
fixable: 'code',
},
create(context) {
return {
CallExpression(node) {
//if (!/test-/.test(context.getFilename())) {
// return;
//}
debugger
const {callee} = node;
if (callee.type !== 'Identifier' ||
callee.name !== 'allowConsoleError') {
return;
}
const {parent} = node;
if (!parent || parent.type === 'ReturnStatement') {
return;
}
const sourceCode = context.getSourceCode();
const fn = sourceCode.getText(node);
if (!fn.includes('return')) {
return;
}
context.report({
node,
message: 'Must return allowConsoleError if callback contains a return',
fix(fixer) {
return fixer.replaceText(node, `return ${fn}`);
},
});
}
};
},
}; |
That won't work in cases like this: amphtml/test/functional/utils/test-bytes.js Lines 50 to 52 in 18520b4
I've logged #15242 to track the addition of an eslint rule. |
#14592 (comment) raised the issue that
allowConsoleError(func)
as written today will swallow the return value offunc
if it were to return something, like a Promise.This PR does
threetwo things:func
at the end ofallowConsoleError
.Adds a presubmit check to prevent tests from failing to return the value offunc
fromallowConsoleError
in cases wherefunc
does return something.This will be added as a new
.eslint
plugin in a separate PR. See Write an eslint plugin to preventallowConsoleError
from swallowing return values #15242.Related to #14592 (comment)