1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = function (d, b) {
|
4 | extendStatics = Object.setPrototypeOf ||
|
5 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
6 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
7 | return extendStatics(d, b);
|
8 | }
|
9 | return function (d, b) {
|
10 | extendStatics(d, b);
|
11 | function __() { this.constructor = d; }
|
12 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
13 | };
|
14 | })();
|
15 | Object.defineProperty(exports, "__esModule", { value: true });
|
16 | var ts = require("typescript");
|
17 | var Lint = require("tslint");
|
18 | var tsutils = require("tsutils");
|
19 | var AstUtils_1 = require("./utils/AstUtils");
|
20 | var Rule = (function (_super) {
|
21 | __extends(Rule, _super);
|
22 | function Rule() {
|
23 | return _super !== null && _super.apply(this, arguments) || this;
|
24 | }
|
25 | Rule.prototype.apply = function (sourceFile) {
|
26 | return this.applyWithFunction(sourceFile, walk);
|
27 | };
|
28 | Rule.metadata = {
|
29 | ruleName: 'no-function-expression',
|
30 | type: 'maintainability',
|
31 | description: 'Do not use function expressions; use arrow functions (lambdas) instead.',
|
32 | options: null,
|
33 | optionsDescription: '',
|
34 | typescriptOnly: true,
|
35 | issueClass: 'Non-SDL',
|
36 | issueType: 'Warning',
|
37 | severity: 'Important',
|
38 | level: 'Opportunity for Excellence',
|
39 | group: 'Clarity',
|
40 | commonWeaknessEnumeration: '398, 710'
|
41 | };
|
42 | Rule.FAILURE_STRING = 'Use arrow function instead of function expression';
|
43 | return Rule;
|
44 | }(Lint.Rules.AbstractRule));
|
45 | exports.Rule = Rule;
|
46 | function walk(ctx) {
|
47 | var allowGenericFunctionExpression = AstUtils_1.AstUtils.getLanguageVariant(ctx.sourceFile) === ts.LanguageVariant.JSX;
|
48 | function cb(node) {
|
49 | if (tsutils.isFunctionExpression(node)) {
|
50 | var walker_1 = new SingleFunctionWalker();
|
51 | node.getChildren().forEach(function (child) {
|
52 | walker_1.walk(child);
|
53 | });
|
54 | var isGenericFunctionInTSX = allowGenericFunctionExpression && walker_1.isGenericFunction;
|
55 | if (!walker_1.isAccessingThis &&
|
56 | !node.asteriskToken &&
|
57 | !isGenericFunctionInTSX) {
|
58 | ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
|
59 | }
|
60 | }
|
61 | return ts.forEachChild(node, cb);
|
62 | }
|
63 | return ts.forEachChild(ctx.sourceFile, cb);
|
64 | }
|
65 | var SingleFunctionWalker = (function () {
|
66 | function SingleFunctionWalker() {
|
67 | this.isAccessingThis = false;
|
68 | this.isGenericFunction = false;
|
69 | }
|
70 | SingleFunctionWalker.prototype.walk = function (root) {
|
71 | var _this = this;
|
72 | var cb = function (node) {
|
73 | if (node.getText() === 'this') {
|
74 | _this.isAccessingThis = true;
|
75 | }
|
76 | if (tsutils.isFunctionExpression(node) || tsutils.isArrowFunction(node)) {
|
77 | return;
|
78 | }
|
79 | if (tsutils.isTypeReferenceNode(node)) {
|
80 | _this.isGenericFunction = true;
|
81 | }
|
82 | ts.forEachChild(node, cb);
|
83 | };
|
84 | cb(root);
|
85 | };
|
86 | return SingleFunctionWalker;
|
87 | }());
|
88 |
|
\ | No newline at end of file |