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 | exports.default = util.createRule({
|
25 | name: 'prefer-as-const',
|
26 | meta: {
|
27 | type: 'suggestion',
|
28 | docs: {
|
29 | description: 'Prefer usage of `as const` over literal type',
|
30 | recommended: 'error',
|
31 | suggestion: true,
|
32 | },
|
33 | fixable: 'code',
|
34 | hasSuggestions: true,
|
35 | messages: {
|
36 | preferConstAssertion: 'Expected a `const` instead of a literal type assertion.',
|
37 | variableConstAssertion: 'Expected a `const` assertion instead of a literal type annotation.',
|
38 | variableSuggest: 'You should use `as const` instead of type annotation.',
|
39 | },
|
40 | schema: [],
|
41 | },
|
42 | defaultOptions: [],
|
43 | create(context) {
|
44 | function compareTypes(valueNode, typeNode, canFix) {
|
45 | if (valueNode.type === utils_1.AST_NODE_TYPES.Literal &&
|
46 | typeNode.type === utils_1.AST_NODE_TYPES.TSLiteralType &&
|
47 | 'raw' in typeNode.literal &&
|
48 | valueNode.raw === typeNode.literal.raw) {
|
49 | if (canFix) {
|
50 | context.report({
|
51 | node: typeNode,
|
52 | messageId: 'preferConstAssertion',
|
53 | fix: fixer => fixer.replaceText(typeNode, 'const'),
|
54 | });
|
55 | }
|
56 | else {
|
57 | context.report({
|
58 | node: typeNode,
|
59 | messageId: 'variableConstAssertion',
|
60 | suggest: [
|
61 | {
|
62 | messageId: 'variableSuggest',
|
63 | fix: (fixer) => [
|
64 | fixer.remove(typeNode.parent),
|
65 | fixer.insertTextAfter(valueNode, ' as const'),
|
66 | ],
|
67 | },
|
68 | ],
|
69 | });
|
70 | }
|
71 | }
|
72 | }
|
73 | return {
|
74 | TSAsExpression(node) {
|
75 | compareTypes(node.expression, node.typeAnnotation, true);
|
76 | },
|
77 | TSTypeAssertion(node) {
|
78 | compareTypes(node.expression, node.typeAnnotation, true);
|
79 | },
|
80 | VariableDeclarator(node) {
|
81 | if (node.init && node.id.typeAnnotation) {
|
82 | compareTypes(node.init, node.id.typeAnnotation.typeAnnotation, false);
|
83 | }
|
84 | },
|
85 | };
|
86 | },
|
87 | });
|
88 |
|
\ | No newline at end of file |