1 | "use strict";
|
2 | var __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 | }));
|
9 | var __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 | });
|
14 | var __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 | };
|
21 | Object.defineProperty(exports, "__esModule", { value: true });
|
22 | const utils_1 = require("@typescript-eslint/utils");
|
23 | const util = __importStar(require("../util"));
|
24 | const printNodeModifiers = (node, final) => {
|
25 | var _a;
|
26 | return `${(_a = node.accessibility) !== null && _a !== void 0 ? _a : ''}${node.static ? ' static' : ''} ${final} `.trimLeft();
|
27 | };
|
28 | const isSupportedLiteral = (node) => {
|
29 | if (node.type === utils_1.AST_NODE_TYPES.Literal) {
|
30 | return true;
|
31 | }
|
32 | if (node.type === utils_1.AST_NODE_TYPES.TaggedTemplateExpression ||
|
33 | node.type === utils_1.AST_NODE_TYPES.TemplateLiteral) {
|
34 | return ('quasi' in node ? node.quasi.quasis : node.quasis).length === 1;
|
35 | }
|
36 | return false;
|
37 | };
|
38 | exports.default = util.createRule({
|
39 | name: 'class-literal-property-style',
|
40 | meta: {
|
41 | type: 'problem',
|
42 | docs: {
|
43 | description: 'Ensures that literals on classes are exposed in a consistent style',
|
44 | recommended: false,
|
45 | },
|
46 | fixable: 'code',
|
47 | messages: {
|
48 | preferFieldStyle: 'Literals should be exposed using readonly fields.',
|
49 | preferGetterStyle: 'Literals should be exposed using getters.',
|
50 | },
|
51 | schema: [{ enum: ['fields', 'getters'] }],
|
52 | },
|
53 | defaultOptions: ['fields'],
|
54 | create(context, [style]) {
|
55 | return Object.assign(Object.assign({}, (style === 'fields' && {
|
56 | MethodDefinition(node) {
|
57 | if (node.kind !== 'get' ||
|
58 | !node.value.body ||
|
59 | !node.value.body.body.length) {
|
60 | return;
|
61 | }
|
62 | const [statement] = node.value.body.body;
|
63 | if (statement.type !== utils_1.AST_NODE_TYPES.ReturnStatement) {
|
64 | return;
|
65 | }
|
66 | const { argument } = statement;
|
67 | if (!argument || !isSupportedLiteral(argument)) {
|
68 | return;
|
69 | }
|
70 | context.report({
|
71 | node: node.key,
|
72 | messageId: 'preferFieldStyle',
|
73 | fix(fixer) {
|
74 | const sourceCode = context.getSourceCode();
|
75 | const name = sourceCode.getText(node.key);
|
76 | let text = '';
|
77 | text += printNodeModifiers(node, 'readonly');
|
78 | text += node.computed ? `[${name}]` : name;
|
79 | text += ` = ${sourceCode.getText(argument)};`;
|
80 | return fixer.replaceText(node, text);
|
81 | },
|
82 | });
|
83 | },
|
84 | })), (style === 'getters' && {
|
85 | PropertyDefinition(node) {
|
86 | if (!node.readonly || node.declare) {
|
87 | return;
|
88 | }
|
89 | const { value } = node;
|
90 | if (!value || !isSupportedLiteral(value)) {
|
91 | return;
|
92 | }
|
93 | context.report({
|
94 | node: node.key,
|
95 | messageId: 'preferGetterStyle',
|
96 | fix(fixer) {
|
97 | const sourceCode = context.getSourceCode();
|
98 | const name = sourceCode.getText(node.key);
|
99 | let text = '';
|
100 | text += printNodeModifiers(node, 'get');
|
101 | text += node.computed ? `[${name}]` : name;
|
102 | text += `() { return ${sourceCode.getText(value)}; }`;
|
103 | return fixer.replaceText(node, text);
|
104 | },
|
105 | });
|
106 | },
|
107 | }));
|
108 | },
|
109 | });
|
110 |
|
\ | No newline at end of file |