|
| 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 { |
| 5 | + TSESTree, |
| 6 | + TSESLint |
| 7 | +} from '@typescript-eslint/experimental-utils'; |
| 8 | + |
| 9 | +type MessageIds = 'error-untyped-underscore'; |
| 10 | +type Options = [ ]; |
| 11 | + |
| 12 | +const noUntypedUnderscoreRule: TSESLint.RuleModule<MessageIds,Options> = { |
| 13 | + meta: { |
| 14 | + type: 'problem', |
| 15 | + messages: { |
| 16 | + 'error-untyped-underscore': |
| 17 | + 'This expression appears to access a private member "{{memberName}}"; ' |
| 18 | + + 'either remove the underscore prefix or else declare a type for the containing object' |
| 19 | + }, |
| 20 | + schema: [ ], |
| 21 | + docs: { |
| 22 | + description: 'Prevents usage of JavaScript\'s "null" keyword.', |
| 23 | + category: 'Stylistic Issues', |
| 24 | + recommended: "error", |
| 25 | + url: 'https://www.npmjs.com/package/@rushstack/eslint-config' |
| 26 | + } |
| 27 | + }, |
| 28 | + create: (context: TSESLint.RuleContext<MessageIds, Options>) => { |
| 29 | + return { |
| 30 | + MemberExpression: function(node: TSESTree.MemberExpression) { |
| 31 | + // Is it an expression like "x.y"? |
| 32 | + let match: boolean = true; |
| 33 | + |
| 34 | + // Ignore expressions such as "super.y", "this.y", and "that.y" |
| 35 | + const memberObject: TSESTree.LeftHandSideExpression = node.object; |
| 36 | + if (memberObject) { |
| 37 | + if (memberObject.type === 'Super' || memberObject.type === 'ThisExpression') { |
| 38 | + match = false; |
| 39 | + } else { |
| 40 | + if (memberObject.type === 'Identifier') { |
| 41 | + if (memberObject.name === 'this' || memberObject.name == 'that') { |
| 42 | + match = false; |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Does the member name start with an underscore? (e.g. "x._y") |
| 49 | + if (match && node.property && node.property.type === 'Identifier') { |
| 50 | + const memberName: string = node.property.name; |
| 51 | + if (memberName && memberName[0] === '_') { |
| 52 | + context.report({ |
| 53 | + node, |
| 54 | + messageId: 'error-untyped-underscore', |
| 55 | + data: { |
| 56 | + memberName: memberName |
| 57 | + } |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + }; |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +export { noUntypedUnderscoreRule }; |
0 commit comments