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 util = __importStar(require("../util"));
|
23 | exports.default = util.createRule({
|
24 | name: 'require-array-sort-compare',
|
25 | defaultOptions: [
|
26 | {
|
27 | ignoreStringArrays: false,
|
28 | },
|
29 | ],
|
30 | meta: {
|
31 | type: 'problem',
|
32 | docs: {
|
33 | description: 'Requires `Array#sort` calls to always provide a `compareFunction`',
|
34 | recommended: false,
|
35 | requiresTypeChecking: true,
|
36 | },
|
37 | messages: {
|
38 | requireCompare: "Require 'compare' argument.",
|
39 | },
|
40 | schema: [
|
41 | {
|
42 | type: 'object',
|
43 | properties: {
|
44 | ignoreStringArrays: {
|
45 | type: 'boolean',
|
46 | },
|
47 | },
|
48 | },
|
49 | ],
|
50 | },
|
51 | create(context, [options]) {
|
52 | const service = util.getParserServices(context);
|
53 | const checker = service.program.getTypeChecker();
|
54 | |
55 |
|
56 |
|
57 |
|
58 | function isStringArrayNode(node) {
|
59 | const type = checker.getTypeAtLocation(service.esTreeNodeToTSNodeMap.get(node));
|
60 | if (checker.isArrayType(type) || checker.isTupleType(type)) {
|
61 | const typeArgs = checker.getTypeArguments(type);
|
62 | return typeArgs.every(arg => util.getTypeName(checker, arg) === 'string');
|
63 | }
|
64 | return false;
|
65 | }
|
66 | return {
|
67 | "CallExpression[arguments.length=0] > MemberExpression[property.name='sort'][computed=false]"(callee) {
|
68 | const tsNode = service.esTreeNodeToTSNodeMap.get(callee.object);
|
69 | const calleeObjType = util.getConstrainedTypeAtLocation(checker, tsNode);
|
70 | if (options.ignoreStringArrays && isStringArrayNode(callee.object)) {
|
71 | return;
|
72 | }
|
73 | if (util.isTypeArrayTypeOrUnionOfArrayTypes(calleeObjType, checker)) {
|
74 | context.report({ node: callee.parent, messageId: 'requireCompare' });
|
75 | }
|
76 | },
|
77 | };
|
78 | },
|
79 | });
|
80 |
|
\ | No newline at end of file |