This commit is contained in:
Alex
2023-11-13 18:23:25 -05:00
parent 60b1b8fde3
commit a022050ab4
9 changed files with 518 additions and 408 deletions

View File

@ -32,6 +32,8 @@ export interface LocalRuleOptions {
* @see [restrict-template-expressions](https://typescript-eslint.io/rules/restrict-template-expressions)
*/
'rules/restrict-template-expressions': RuleEntry<{ allow: string[] }>;
/** Ban assignment of empty object literals `{}` and replace them with `Object.create(null)` */
'rules/no-empty-object-literal': RuleEntry<unknown>;
}
export type RuleOptions = Rules & Partial<LocalRuleOptions>;

View File

@ -27,10 +27,7 @@ export const typescriptRules: Partial<TypeScriptRules> = {
'@typescript-eslint/no-unsafe-call': off,
'@typescript-eslint/no-unsafe-member-access': off,
'@typescript-eslint/no-unsafe-return': off,
'@typescript-eslint/no-unused-vars': [
error,
{ ignoreRestSiblings: true, varsIgnorePattern: '^_' },
],
'@typescript-eslint/no-unused-vars': off,
'@typescript-eslint/no-use-before-define': off,
'@typescript-eslint/no-var-requires': off,
'@typescript-eslint/restrict-template-expressions': off,

View File

@ -1,6 +1,7 @@
import type { Rule } from 'eslint';
import type { ESLintUtils } from '@typescript-eslint/utils';
import noEmptyObjectLiteral from './no-empty-object-literal';
import noImportDot from './no-import-dot';
import restrictTemplateExpressions from './restrict-template-expressions';
@ -8,6 +9,7 @@ export const rules: Record<
string,
Rule.RuleModule | ESLintUtils.RuleModule<string, unknown[]>
> = {
'no-empty-object-literal': noEmptyObjectLiteral,
'no-import-dot': noImportDot,
'restrict-template-expressions': restrictTemplateExpressions,
};

View File

@ -0,0 +1,32 @@
import type { Rule } from 'eslint';
const rule: Rule.RuleModule = {
meta: {
type: 'problem',
docs: {
description:
'Ban assignment of empty object literals `{}` and replace them with `Object.create(null)`',
category: 'Best Practices',
recommended: true,
},
fixable: 'code',
},
create: context => ({
AssignmentExpression(node) {
if (
node.operator === '=' &&
node.right.type === 'ObjectExpression' &&
node.right.properties.length === 0
) {
context.report({
node,
message:
'Assigning empty object literals are not allowed, use `Object.create(null)` instead.',
fix: fixer => fixer.replaceText(node.right, 'Object.create(null)'),
});
}
},
}),
};
export default rule;