onlyArrowFunctionsRule.js 4.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright 2013 Palantir Technologies, Inc.
  5. *
  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
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  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.
  17. */
  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 OPTION_ALLOW_DECLARATIONS = "allow-declarations";
  24. var OPTION_ALLOW_NAMED_FUNCTIONS = "allow-named-functions";
  25. var Rule = /** @class */ (function (_super) {
  26. tslib_1.__extends(Rule, _super);
  27. function Rule() {
  28. return _super !== null && _super.apply(this, arguments) || this;
  29. }
  30. Rule.prototype.apply = function (sourceFile) {
  31. return this.applyWithFunction(sourceFile, walk, parseOptions(this.ruleArguments));
  32. };
  33. /* tslint:disable:object-literal-sort-keys */
  34. Rule.metadata = {
  35. ruleName: "only-arrow-functions",
  36. description: "Disallows traditional (non-arrow) function expressions.",
  37. rationale: "Traditional functions don't bind lexical scope, which can lead to unexpected behavior when accessing 'this'.",
  38. optionsDescription: Lint.Utils.dedent(templateObject_1 || (templateObject_1 = 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),
  39. options: {
  40. type: "array",
  41. items: {
  42. type: "string",
  43. enum: [OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS],
  44. },
  45. minLength: 0,
  46. maxLength: 1,
  47. },
  48. optionExamples: [true, [true, OPTION_ALLOW_DECLARATIONS, OPTION_ALLOW_NAMED_FUNCTIONS]],
  49. type: "typescript",
  50. typescriptOnly: false,
  51. };
  52. /* tslint:enable:object-literal-sort-keys */
  53. Rule.FAILURE_STRING = "non-arrow functions are forbidden";
  54. return Rule;
  55. }(Lint.Rules.AbstractRule));
  56. exports.Rule = Rule;
  57. function parseOptions(ruleArguments) {
  58. return {
  59. allowDeclarations: hasOption(OPTION_ALLOW_DECLARATIONS),
  60. allowNamedFunctions: hasOption(OPTION_ALLOW_NAMED_FUNCTIONS),
  61. };
  62. function hasOption(name) {
  63. return ruleArguments.indexOf(name) !== -1;
  64. }
  65. }
  66. function walk(ctx) {
  67. var sourceFile = ctx.sourceFile, _a = ctx.options, allowDeclarations = _a.allowDeclarations, allowNamedFunctions = _a.allowNamedFunctions;
  68. return ts.forEachChild(sourceFile, function cb(node) {
  69. switch (node.kind) {
  70. case ts.SyntaxKind.FunctionDeclaration:
  71. if (allowDeclarations) {
  72. break;
  73. }
  74. // falls through
  75. case ts.SyntaxKind.FunctionExpression: {
  76. var f = node;
  77. if (!(allowNamedFunctions && f.name !== undefined) && !functionIsExempt(f)) {
  78. ctx.addFailureAtNode(utils.getChildOfKind(node, ts.SyntaxKind.FunctionKeyword, ctx.sourceFile), Rule.FAILURE_STRING);
  79. }
  80. }
  81. }
  82. return ts.forEachChild(node, cb);
  83. });
  84. }
  85. /** Generator functions and functions using `this` are allowed. */
  86. function functionIsExempt(node) {
  87. return node.asteriskToken !== undefined ||
  88. node.parameters.length !== 0 && utils.isThisParameter(node.parameters[0]) ||
  89. node.body !== undefined && ts.forEachChild(node, usesThis) === true;
  90. }
  91. function usesThis(node) {
  92. return node.kind === ts.SyntaxKind.ThisKeyword || !utils.hasOwnThisReference(node) && ts.forEachChild(node, usesThis);
  93. }
  94. var templateObject_1;