|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +import type { TSESTree, TSESLint, ParserServices } from '@typescript-eslint/utils'; |
| 5 | +import type * as ts from 'typescript'; |
| 6 | + |
| 7 | +type MessageIds = 'error-object-literal-dictionary'; |
| 8 | +type Options = []; |
| 9 | + |
| 10 | +const nullPrototypeDictionariesRule: TSESLint.RuleModule<MessageIds, Options> = { |
| 11 | + defaultOptions: [], |
| 12 | + meta: { |
| 13 | + type: 'problem', |
| 14 | + messages: { |
| 15 | + 'error-object-literal-dictionary': |
| 16 | + 'Dictionary objects typed as Record<string, T> should be created using Object.create(null)' + |
| 17 | + ' instead of an object literal. This avoids prototype pollution, collisions with' + |
| 18 | + ' Object.prototype members such as "toString", and enables higher performance since runtimes' + |
| 19 | + ' such as V8 process Object.create(null) as opting out of having a hidden class and going' + |
| 20 | + ' directly to dictionary mode.' |
| 21 | + }, |
| 22 | + schema: [], |
| 23 | + docs: { |
| 24 | + description: |
| 25 | + 'Enforce that objects typed as string-keyed dictionaries (Record<string, T>) are instantiated' + |
| 26 | + ' using Object.create(null) instead of object literals, to avoid prototype pollution issues,' + |
| 27 | + ' collisions with Object.prototype members such as "toString", and for higher performance' + |
| 28 | + ' since runtimes such as V8 process Object.create(null) as opting out of having a hidden' + |
| 29 | + ' class and going directly to dictionary mode', |
| 30 | + recommended: 'strict', |
| 31 | + url: 'https://www.npmjs.com/package/@rushstack/eslint-plugin' |
| 32 | + } as TSESLint.RuleMetaDataDocs |
| 33 | + }, |
| 34 | + create: (context: TSESLint.RuleContext<MessageIds, Options>) => { |
| 35 | + const parserServices: Partial<ParserServices> | undefined = |
| 36 | + context.sourceCode?.parserServices ?? context.parserServices; |
| 37 | + if (!parserServices || !parserServices.program || !parserServices.esTreeNodeToTSNodeMap) { |
| 38 | + throw new Error( |
| 39 | + 'This rule requires your ESLint configuration to define the "parserOptions.project"' + |
| 40 | + ' property for "@typescript-eslint/parser".' |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + const typeChecker: ts.TypeChecker = parserServices.program.getTypeChecker(); |
| 45 | + |
| 46 | + /** |
| 47 | + * Checks whether the given type represents a pure string-keyed dictionary type: |
| 48 | + * it has a string index signature and no named properties. |
| 49 | + */ |
| 50 | + function isStringKeyedDictionaryType(type: ts.Type): boolean { |
| 51 | + // Check if the type has a string index signature |
| 52 | + if (!type.getStringIndexType()) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + |
| 56 | + // A pure dictionary type has no named properties - only an index signature. |
| 57 | + // Types with named properties (like interfaces with extra index signatures) |
| 58 | + // are not considered pure dictionaries. |
| 59 | + if (type.getProperties().length > 0) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + return { |
| 67 | + ObjectExpression(node: TSESTree.ObjectExpression): void { |
| 68 | + const tsNode: ts.Node = parserServices.esTreeNodeToTSNodeMap!.get(node); |
| 69 | + |
| 70 | + // Get the contextual type (the type expected by the surrounding context, |
| 71 | + // e.g. from a variable declaration's type annotation) |
| 72 | + const contextualType: ts.Type | undefined = typeChecker.getContextualType( |
| 73 | + tsNode as ts.Expression |
| 74 | + ); |
| 75 | + if (!contextualType) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + if (isStringKeyedDictionaryType(contextualType)) { |
| 80 | + context.report({ |
| 81 | + node, |
| 82 | + messageId: 'error-object-literal-dictionary' |
| 83 | + }); |
| 84 | + } |
| 85 | + } |
| 86 | + }; |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +export { nullPrototypeDictionariesRule }; |
0 commit comments