UNPKG

2.97 kBJavaScriptView Raw
1"use strict";
2var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3 if (k2 === undefined) k2 = k;
4 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5}) : (function(o, m, k, k2) {
6 if (k2 === undefined) k2 = k;
7 o[k2] = m[k];
8}));
9var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
11}) : function(o, v) {
12 o["default"] = v;
13});
14var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
16 var result = {};
17 if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18 __setModuleDefault(result, mod);
19 return result;
20};
21Object.defineProperty(exports, "__esModule", { value: true });
22const utils_1 = require("@typescript-eslint/utils");
23const util = __importStar(require("../util"));
24exports.default = util.createRule({
25 name: 'no-this-alias',
26 meta: {
27 type: 'suggestion',
28 docs: {
29 description: 'Disallow aliasing `this`',
30 recommended: 'error',
31 },
32 schema: [
33 {
34 type: 'object',
35 additionalProperties: false,
36 properties: {
37 allowDestructuring: {
38 type: 'boolean',
39 },
40 allowedNames: {
41 type: 'array',
42 items: {
43 type: 'string',
44 },
45 },
46 },
47 },
48 ],
49 messages: {
50 thisAssignment: "Unexpected aliasing of 'this' to local variable.",
51 thisDestructure: "Unexpected aliasing of members of 'this' to local variables.",
52 },
53 },
54 defaultOptions: [
55 {
56 allowDestructuring: true,
57 allowedNames: [],
58 },
59 ],
60 create(context, [{ allowDestructuring, allowedNames }]) {
61 return {
62 "VariableDeclarator[init.type='ThisExpression']"(node) {
63 const { id } = node;
64 if (allowDestructuring && id.type !== utils_1.AST_NODE_TYPES.Identifier) {
65 return;
66 }
67 const hasAllowedName = id.type === utils_1.AST_NODE_TYPES.Identifier
68 ? allowedNames.includes(id.name)
69 : false;
70 if (!hasAllowedName) {
71 context.report({
72 node: id,
73 messageId: id.type === utils_1.AST_NODE_TYPES.Identifier
74 ? 'thisAssignment'
75 : 'thisDestructure',
76 });
77 }
78 },
79 };
80 },
81});
82//# sourceMappingURL=no-this-alias.js.map
\No newline at end of file