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 Spacing;
|
20 | (function (Spacing) {
|
21 | Spacing[Spacing["always"] = 0] = "always";
|
22 | Spacing[Spacing["never"] = 1] = "never";
|
23 | })(Spacing || (Spacing = {}));
|
24 | var Rule = (function (_super) {
|
25 | __extends(Rule, _super);
|
26 | function Rule() {
|
27 | return _super !== null && _super.apply(this, arguments) || this;
|
28 | }
|
29 | Rule.prototype.apply = function (sourceFile) {
|
30 | return this.applyWithFunction(sourceFile, walk, this.parseOptions(this.getOptions()));
|
31 | };
|
32 | Rule.prototype.parseOptions = function (options) {
|
33 | var parsed = {
|
34 | spacing: Spacing.always,
|
35 | allowMultiline: false
|
36 | };
|
37 | if (options.ruleArguments[0] === 'never') {
|
38 | parsed.spacing = Spacing.never;
|
39 | }
|
40 | if (options.ruleArguments[1] !== undefined) {
|
41 | parsed.allowMultiline = options.ruleArguments[1].allowMultiline !== false;
|
42 | }
|
43 | return parsed;
|
44 | };
|
45 | Rule.metadata = {
|
46 | ruleName: 'react-tsx-curly-spacing',
|
47 | type: 'style',
|
48 | description: 'Consistently use spaces around the brace characters of JSX attributes.',
|
49 | options: null,
|
50 | optionsDescription: '',
|
51 | typescriptOnly: true,
|
52 | issueClass: 'Non-SDL',
|
53 | issueType: 'Warning',
|
54 | severity: 'Low',
|
55 | level: 'Opportunity for Excellence',
|
56 | recommendation: 'false',
|
57 | group: 'Deprecated'
|
58 | };
|
59 | return Rule;
|
60 | }(Lint.Rules.AbstractRule));
|
61 | exports.Rule = Rule;
|
62 | function walk(ctx) {
|
63 | function validateBraceSpacing(node, first, second, violationRoot) {
|
64 | if (first === undefined || second === undefined || violationRoot === undefined) {
|
65 | return;
|
66 | }
|
67 | if (isMultiline(first, second)) {
|
68 | if (!ctx.options.allowMultiline) {
|
69 | reportFailure(node, violationRoot, getFailureForNewLine(first, violationRoot));
|
70 | }
|
71 | }
|
72 | else if (ctx.options.spacing === Spacing.always) {
|
73 | if (!isSpaceBetweenTokens(first, second)) {
|
74 | reportFailure(node, violationRoot, getFailureForSpace(first, violationRoot));
|
75 | }
|
76 | }
|
77 | else {
|
78 | if (isSpaceBetweenTokens(first, second)) {
|
79 | reportFailure(node, violationRoot, getFailureForSpace(first, violationRoot));
|
80 | }
|
81 | }
|
82 | }
|
83 | function getFailureForSpace(first, violationRoot) {
|
84 | if (ctx.options.spacing === Spacing.always) {
|
85 | if (first === violationRoot) {
|
86 | return "A space is required after '" + violationRoot.getText() + "'";
|
87 | }
|
88 | else {
|
89 | return "A space is required before '" + violationRoot.getText() + "'";
|
90 | }
|
91 | }
|
92 | else {
|
93 | if (first === violationRoot) {
|
94 | return "There should be no space after '" + violationRoot.getText() + "'";
|
95 | }
|
96 | else {
|
97 | return "There should be no space before '" + violationRoot.getText() + "'";
|
98 | }
|
99 | }
|
100 | }
|
101 | function getFailureForNewLine(first, violationRoot) {
|
102 | if (first === violationRoot) {
|
103 | return "There should be no newline after '" + violationRoot.getText() + "'";
|
104 | }
|
105 | else {
|
106 | return "There should be no newline before '" + violationRoot.getText() + "'";
|
107 | }
|
108 | }
|
109 | function reportFailure(start, endNode, failure) {
|
110 | ctx.addFailureAt(start.getStart(), endNode.getStart() - start.getStart(), failure);
|
111 | }
|
112 | function isSpaceBetweenTokens(left, right) {
|
113 | var text = ctx.sourceFile.getText().slice(left.getEnd(), right.getStart());
|
114 | return /\s/.test(text.replace(/\/\*.*?\*\//g, ''));
|
115 | }
|
116 | function isMultiline(left, right) {
|
117 | var sourceFile = ctx.sourceFile;
|
118 | var leftLine = sourceFile.getLineAndCharacterOfPosition(left.getStart()).line;
|
119 | var rightLine = sourceFile.getLineAndCharacterOfPosition(right.getStart()).line;
|
120 | return leftLine !== rightLine;
|
121 | }
|
122 | function cb(node) {
|
123 | if (tsutils.isJsxExpression(node)) {
|
124 | var childrenCount = node.getChildCount();
|
125 | if (childrenCount > 2) {
|
126 | var first = node.getFirstToken();
|
127 | var last = node.getLastToken();
|
128 | var second = node.getChildAt(1);
|
129 | var penultimate = node.getChildAt(childrenCount - 2);
|
130 | validateBraceSpacing(node, first, second, first);
|
131 | validateBraceSpacing(node, penultimate, last, last);
|
132 | }
|
133 | }
|
134 | return ts.forEachChild(node, cb);
|
135 | }
|
136 | return ts.forEachChild(ctx.sourceFile, cb);
|
137 | }
|
138 |
|
\ | No newline at end of file |