aboutsummaryrefslogtreecommitdiff
path: root/node_modules/tslint/lib/rules/objectLiteralSortKeysRule.js
blob: 70d7c5e496efd42dc6d8714250132fdaecf4f2c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
"use strict";
/**
 * @license
 * Copyright 2013 Palantir Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var tsutils_1 = require("tsutils");
var ts = require("typescript");
var Lint = require("../index");
var OPTION_IGNORE_CASE = "ignore-case";
var OPTION_MATCH_DECLARATION_ORDER = "match-declaration-order";
var Rule = /** @class */ (function (_super) {
    tslib_1.__extends(Rule, _super);
    function Rule() {
        return _super !== null && _super.apply(this, arguments) || this;
    }
    /* tslint:enable:object-literal-sort-keys */
    Rule.FAILURE_STRING_ALPHABETICAL = function (name) {
        return "The key '" + name + "' is not sorted alphabetically";
    };
    Rule.FAILURE_STRING_USE_DECLARATION_ORDER = function (propName, typeName) {
        var type = typeName === undefined ? "its type declaration" : "'" + typeName + "'";
        return "The key '" + propName + "' is not in the same order as it is in " + type + ".";
    };
    Rule.prototype.apply = function (sourceFile) {
        var options = parseOptions(this.ruleArguments);
        if (options.matchDeclarationOrder) {
            throw new Error(this.ruleName + " needs type info to use \"" + OPTION_MATCH_DECLARATION_ORDER + "\".");
        }
        return this.applyWithFunction(sourceFile, walk, options);
    };
    Rule.prototype.applyWithProgram = function (sourceFile, program) {
        return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments), program.getTypeChecker());
    };
    /* tslint:disable:object-literal-sort-keys */
    Rule.metadata = {
        ruleName: "object-literal-sort-keys",
        description: (_a = ["\n            Checks ordering of keys in object literals.\n\n            When using the default alphabetical ordering, additional blank lines may be used to group\n            object properties together while keeping the elements within each group in alphabetical order.\n        "], _a.raw = ["\n            Checks ordering of keys in object literals.\n\n            When using the default alphabetical ordering, additional blank lines may be used to group\n            object properties together while keeping the elements within each group in alphabetical order.\n        "], Lint.Utils.dedent(_a)),
        rationale: "Useful in preventing merge conflicts",
        optionsDescription: (_b = ["\n            By default, this rule checks that keys are in alphabetical order.\n            The following may optionally be passed:\n\n            * \"", "\" will to compare keys in a case insensitive way.\n            * \"", "\" will prefer to use the key ordering of the contextual type of the object literal, as in:\n\n                interface I { foo: number; bar: number; }\n                const obj: I = { foo: 1, bar: 2 };\n\n            If a contextual type is not found, alphabetical ordering will be used instead.\n            "], _b.raw = ["\n            By default, this rule checks that keys are in alphabetical order.\n            The following may optionally be passed:\n\n            * \"", "\" will to compare keys in a case insensitive way.\n            * \"", "\" will prefer to use the key ordering of the contextual type of the object literal, as in:\n\n                interface I { foo: number; bar: number; }\n                const obj: I = { foo: 1, bar: 2 };\n\n            If a contextual type is not found, alphabetical ordering will be used instead.\n            "], Lint.Utils.dedent(_b, OPTION_IGNORE_CASE, OPTION_MATCH_DECLARATION_ORDER)),
        options: {
            type: "string",
            enum: [OPTION_IGNORE_CASE, OPTION_MATCH_DECLARATION_ORDER],
        },
        optionExamples: [
            true,
            [true, OPTION_IGNORE_CASE, OPTION_MATCH_DECLARATION_ORDER],
        ],
        type: "maintainability",
        typescriptOnly: false,
    };
    return Rule;
}(Lint.Rules.OptionallyTypedRule));
exports.Rule = Rule;
function parseOptions(ruleArguments) {
    return {
        ignoreCase: has(OPTION_IGNORE_CASE),
        matchDeclarationOrder: has(OPTION_MATCH_DECLARATION_ORDER),
    };
    function has(name) {
        return ruleArguments.indexOf(name) !== -1;
    }
}
function walk(ctx, checker) {
    var sourceFile = ctx.sourceFile, _a = ctx.options, ignoreCase = _a.ignoreCase, matchDeclarationOrder = _a.matchDeclarationOrder;
    ts.forEachChild(sourceFile, function cb(node) {
        if (tsutils_1.isObjectLiteralExpression(node) && node.properties.length > 1) {
            check(node);
        }
        ts.forEachChild(node, cb);
    });
    function check(node) {
        if (matchDeclarationOrder) {
            var type = getContextualType(node, checker);
            // If type has an index signature, we can't check ordering.
            // If type has call/construct signatures, it can't be satisfied by an object literal anyway.
            if (type !== undefined
                && type.members.every(function (m) { return m.kind === ts.SyntaxKind.PropertySignature || m.kind === ts.SyntaxKind.MethodSignature; })) {
                checkMatchesDeclarationOrder(node, type, type.members);
                return;
            }
        }
        checkAlphabetical(node);
    }
    function checkAlphabetical(node) {
        if (tsutils_1.isSameLine(ctx.sourceFile, node.properties.pos, node.end)) {
            return;
        }
        var lastKey;
        for (var _i = 0, _a = node.properties; _i < _a.length; _i++) {
            var property = _a[_i];
            switch (property.kind) {
                case ts.SyntaxKind.SpreadAssignment:
                    lastKey = undefined; // reset at spread
                    break;
                case ts.SyntaxKind.ShorthandPropertyAssignment:
                case ts.SyntaxKind.PropertyAssignment:
                    if (property.name.kind === ts.SyntaxKind.Identifier ||
                        property.name.kind === ts.SyntaxKind.StringLiteral) {
                        var key = ignoreCase ? property.name.text.toLowerCase() : property.name.text;
                        // comparison with undefined is expected
                        if (lastKey > key && !hasBlankLineBefore(ctx.sourceFile, property)) {
                            ctx.addFailureAtNode(property.name, Rule.FAILURE_STRING_ALPHABETICAL(property.name.text));
                            return; // only show warning on first out-of-order property
                        }
                        lastKey = key;
                    }
            }
        }
    }
    function checkMatchesDeclarationOrder(_a, type, members) {
        var properties = _a.properties;
        var memberIndex = 0;
        outer: for (var _i = 0, properties_1 = properties; _i < properties_1.length; _i++) {
            var prop = properties_1[_i];
            if (prop.kind === ts.SyntaxKind.SpreadAssignment) {
                memberIndex = 0;
                continue;
            }
            if (prop.name.kind === ts.SyntaxKind.ComputedPropertyName) {
                continue;
            }
            var propName = prop.name.text;
            for (; memberIndex !== members.length; memberIndex++) {
                var memberName = members[memberIndex].name;
                if (memberName.kind !== ts.SyntaxKind.ComputedPropertyName && propName === memberName.text) {
                    continue outer;
                }
            }
            // This We didn't find the member we were looking for past the previous member,
            // so it must have come before it and is therefore out of order.
            ctx.addFailureAtNode(prop.name, Rule.FAILURE_STRING_USE_DECLARATION_ORDER(propName, getTypeName(type)));
            // Don't bother with multiple errors.
            break;
        }
    }
}
function hasBlankLineBefore(sourceFile, element) {
    var comments = ts.getLeadingCommentRanges(sourceFile.text, element.pos);
    if (comments === undefined) {
        comments = []; // it will be easier to work with an empty array down below...
    }
    var elementStart = comments.length > 0 ? comments[comments.length - 1].end : element.getFullStart();
    // either the element itself, or one of its leading comments must have an extra new line before them
    return hasDoubleNewLine(sourceFile, elementStart) || comments.some(function (comment) {
        var commentLine = ts.getLineAndCharacterOfPosition(sourceFile, comment.pos).line;
        var commentLineStartPosition = ts.getPositionOfLineAndCharacter(sourceFile, commentLine, 0);
        return hasDoubleNewLine(sourceFile, commentLineStartPosition - 4);
    });
}
function hasDoubleNewLine(sourceFile, position) {
    return /(\r\n|\r|\n){2}/.test(sourceFile.text.slice(position, position + 4));
}
function getTypeName(t) {
    var parent = t.parent;
    return t.kind === ts.SyntaxKind.InterfaceDeclaration
        ? t.name.text
        : tsutils_1.isTypeAliasDeclaration(parent)
            ? parent.name.text
            : undefined;
}
function getContextualType(node, checker) {
    var c = checker.getContextualType(node);
    if (c === undefined || c.symbol === undefined) {
        return undefined;
    }
    var declarations = c.symbol.declarations;
    if (declarations === undefined || declarations.length !== 1) {
        return undefined;
    }
    var decl = declarations[0];
    return tsutils_1.isInterfaceDeclaration(decl) || tsutils_1.isTypeLiteralNode(decl) ? decl : undefined;
}
var _a, _b;