4 * Copyright 2013 Palantir Technologies, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 Object.defineProperty(exports, "__esModule", { value: true });
19 var tslib_1 = require("tslib");
20 var utils = require("tsutils");
21 var ts = require("typescript");
22 var Lint = require("../index");
23 var onlyArrowFunctions_examples_1 = require("./code-examples/onlyArrowFunctions.examples");
24 var OPTION_ALLOW_DECLARATIONS = "allow-declarations";
25 var OPTION_ALLOW_NAMED_FUNCTIONS = "allow-named-functions";
26 var Rule = /** @class */ (function (_super) {
27 tslib_1.__extends(Rule, _super);
29 return _super !== null && _super.apply(this, arguments) || this;
31 Rule.prototype.apply = function (sourceFile) {
32 return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
34 /* tslint:disable:object-literal-sort-keys */
36 ruleName: "only-arrow-functions",
37 description: "Disallows traditional (non-arrow) function expressions.",
38 rationale: "Traditional functions don't bind lexical scope, which can lead to unexpected behavior when accessing 'this'.",
39 descriptionDetails: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = tslib_1.__makeTemplateObject(["\n Note that non-arrow functions are allowed if 'this' appears somewhere in its body\n (as such functions cannot be converted to arrow functions).\n "], ["\n Note that non-arrow functions are allowed if 'this' appears somewhere in its body\n (as such functions cannot be converted to arrow functions).\n "]))),
40 optionsDescription: Lint.Utils.dedent(templateObject_2 || (templateObject_2 = tslib_1.__makeTemplateObject(["\n Two arguments may be optionally provided:\n\n * `\"", "\"` allows standalone function declarations.\n * `\"", "\"` allows the expression `function foo() {}` but not `function() {}`.\n "], ["\n Two arguments may be optionally provided:\n\n * \\`\"", "\"\\` allows standalone function declarations.\n * \\`\"", "\"\\` allows the expression \\`function foo() {}\\` but not \\`function() {}\\`.\n "])), OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS),
45 enum: [OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS],
50 optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS]],
52 typescriptOnly: false,
53 codeExamples: onlyArrowFunctions_examples_1.codeExamples,
55 /* tslint:enable:object-literal-sort-keys */
56 Rule.FAILURE_STRING = "non-arrow functions are forbidden";
58 }(Lint.Rules.AbstractRule));
60 function parseOptions(ruleArguments) {
62 allowDeclarations: hasOption(OPTION_ALLOW_DECLARATIONS),
63 allowNamedFunctions: hasOption(OPTION_ALLOW_NAMED_FUNCTIONS),
65 function hasOption(name) {
66 return ruleArguments.indexOf(name) !== -1;
70 var sourceFile = ctx.sourceFile, _a = ctx.options, allowDeclarations = _a.allowDeclarations, allowNamedFunctions = _a.allowNamedFunctions;
71 return ts.forEachChild(sourceFile, function cb(node) {
73 case ts.SyntaxKind.FunctionDeclaration:
74 if (allowDeclarations) {
78 case ts.SyntaxKind.FunctionExpression: {
80 if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
81 ctx.addFailureAtNode(utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile), Rule.FAILURE_STRING);
85 return ts.forEachChild(node, cb);
88 /** Generator functions and functions using `this` are allowed. */
89 function functionIsExempt(node) {
90 return (node.asteriskToken !== undefined ||
91 (node.parameters.length !== 0 && utils.isThisParameter(node.parameters[0])) ||
92 (node.body !== undefined && ts.forEachChild(node, usesThis) === true));
94 function usesThis(node) {
95 return (node.kind === ts.SyntaxKind.ThisKeyword ||
96 (!utils.hasOwnThisReference(node) && ts.forEachChild(node, usesThis)));
98 var templateObject_1, templateObject_2;