Files
flipper/desktop/eslint-plugin-flipper/src/rules/noConsoleErrorWithoutContext.ts
Pascal Hartig 7638afb5d6 Upgrade typescript linter
Summary:
Just shows some warnings when running `yarn lint` otherwise about lack of compatibility.

Required updating the rules because of a breaking change here: https://github.com/typescript-eslint/typescript-eslint/pull/3800/files

Reviewed By: lblasa

Differential Revision: D33533052

fbshipit-source-id: fd73d534bbd6e1933321b5811832b05a9739af95
2022-01-13 11:09:43 -08:00

64 lines
2.0 KiB
TypeScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
*/
import {TSESTree} from '@typescript-eslint/experimental-utils';
import {createESLintRule} from '../utils/createEslintRule';
type Options = [];
export type MessageIds = 'noConsoleErrorWithoutContext';
export const RULE_NAME = 'no-console-error-without-context';
export default createESLintRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'TBD',
recommended: 'warn',
},
schema: [],
messages: {
noConsoleErrorWithoutContext:
'"Naked" console.error calls are hard to identify without providing a context. Please add a static hint, e.g. `console.error("Reading user config failed", err);`',
},
},
defaultOptions: [],
create(context) {
return {
ExpressionStatement(node: TSESTree.ExpressionStatement) {
if (node.expression.type === 'CallExpression') {
const callee = node.expression.callee;
const isConsoleError =
callee.type === 'MemberExpression' &&
callee.object.type === 'Identifier' &&
callee.object.name === 'console' &&
callee.property.type === 'Identifier' &&
callee.property.name === 'error';
if (
isConsoleError &&
// If this is an Identifier it means the first argument is an object, e.g.
// console.error(err);
// Whereas a literal implies there's a static context given, e.g.
// console.error("My identifiable error: ", err);
// Expressions like concatenations are also fine here.
node.expression.arguments[0]?.type === 'Identifier'
) {
context.report({
node,
messageId: 'noConsoleErrorWithoutContext',
});
}
}
},
};
},
});