1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | "use strict";
|
18 | var __extends = (this && this.__extends) || function (d, b) {
|
19 | for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
20 | function __() { this.constructor = d; }
|
21 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
22 | };
|
23 | var Lint = require("tslint");
|
24 | var ts = require("typescript");
|
25 | var OPTION_ALWAYS = "always";
|
26 | var OPTION_NEVER = "never";
|
27 | var SPACING_VALUES = [OPTION_ALWAYS, OPTION_NEVER];
|
28 |
|
29 | var SPACING_OBJECT = {
|
30 | type: "string",
|
31 | enum: SPACING_VALUES,
|
32 | };
|
33 |
|
34 | var newLineRegexp = /\n/;
|
35 | function isExpressionMultiline(text) {
|
36 | return newLineRegexp.test(text);
|
37 | }
|
38 | function getTokensCombinedText(firstToken, nextToken) {
|
39 | var parentNodeText = nextToken.parent.getText();
|
40 | var firstTokenText = firstToken.getText();
|
41 | var secondTokenText = nextToken.getText();
|
42 | var secondTokenTextLocation = parentNodeText.indexOf(secondTokenText);
|
43 | var firstTokenTextLocation = parentNodeText.indexOf(firstTokenText);
|
44 | var combinedTokeText = parentNodeText.slice(firstTokenTextLocation, secondTokenTextLocation + secondTokenText.length);
|
45 | return combinedTokeText;
|
46 | }
|
47 | function getTotalCharCount(comments) {
|
48 | return comments
|
49 | .map(function (comment) { return comment.end - comment.pos; })
|
50 | .reduce(function (l, r) { return l + r; }, 0);
|
51 | }
|
52 | var Rule = (function (_super) {
|
53 | __extends(Rule, _super);
|
54 | function Rule() {
|
55 | return _super.apply(this, arguments) || this;
|
56 | }
|
57 | Rule.prototype.apply = function (sourceFile) {
|
58 | var walker = new JsxCurlySpacingWalker(sourceFile, this.getOptions());
|
59 | return this.applyWithWalker(walker);
|
60 | };
|
61 | return Rule;
|
62 | }(Lint.Rules.AbstractRule));
|
63 |
|
64 | Rule.metadata = {
|
65 | ruleName: "jsx-curly-spacing",
|
66 | description: "Checks JSX curly braces spacing",
|
67 | optionsDescription: (_a = ["\n One of the following two options must be provided:\n\n * `\"", "\"` requires JSX attributes to have spaces between curly braces\n * `\"", "\"` requires JSX attributes to NOT have spaces between curly braces"], _a.raw = ["\n One of the following two options must be provided:\n\n * \\`\"", "\"\\` requires JSX attributes to have spaces between curly braces\n * \\`\"", "\"\\` requires JSX attributes to NOT have spaces between curly braces"], Lint.Utils.dedent(_a, OPTION_ALWAYS, OPTION_NEVER)),
|
68 | options: {
|
69 | type: "array",
|
70 | items: [SPACING_OBJECT],
|
71 | minLength: 1,
|
72 | maxLength: 1,
|
73 | },
|
74 | optionExamples: [
|
75 | "[true, \"" + OPTION_ALWAYS + "\"]",
|
76 | "[true, \"" + OPTION_NEVER + "\"]",
|
77 | ],
|
78 | type: "style",
|
79 | typescriptOnly: true,
|
80 | };
|
81 |
|
82 | Rule.FAILURE_NO_ENDING_SPACE = function (tokenStr) {
|
83 | return "A space is required before " + tokenStr;
|
84 | };
|
85 | Rule.FAILURE_NO_BEGINNING_SPACE = function (tokenStr) {
|
86 | return "A space is required after " + tokenStr;
|
87 | };
|
88 | Rule.FAILURE_FORBIDDEN_SPACES_BEGINNING = function (tokenStr) {
|
89 | return "There should be no space after " + tokenStr;
|
90 | };
|
91 | Rule.FAILURE_FORBIDDEN_SPACES_END = function (tokenStr) {
|
92 | return "There should be no space before " + tokenStr;
|
93 | };
|
94 | exports.Rule = Rule;
|
95 | var JsxCurlySpacingWalker = (function (_super) {
|
96 | __extends(JsxCurlySpacingWalker, _super);
|
97 | function JsxCurlySpacingWalker() {
|
98 | return _super.apply(this, arguments) || this;
|
99 | }
|
100 | JsxCurlySpacingWalker.prototype.visitJsxExpression = function (node) {
|
101 | this.validateBraceSpacing(node);
|
102 | _super.prototype.visitJsxExpression.call(this, node);
|
103 | };
|
104 | JsxCurlySpacingWalker.prototype.visitNode = function (node) {
|
105 | if (node.kind === ts.SyntaxKind.JsxSpreadAttribute) {
|
106 | this.validateBraceSpacing(node);
|
107 | }
|
108 | _super.prototype.visitNode.call(this, node);
|
109 | };
|
110 | JsxCurlySpacingWalker.prototype.validateBraceSpacing = function (node) {
|
111 | var firstToken = node.getFirstToken();
|
112 | var secondToken = node.getChildAt(1);
|
113 | var lastToken = node.getLastToken();
|
114 | var secondToLastToken = node.getChildAt(node.getChildCount() - 2);
|
115 | var nodeStart = node.getStart();
|
116 | var nodeWidth = node.getWidth();
|
117 | if (this.hasOption(OPTION_ALWAYS)) {
|
118 | var deleteFix = this.getDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
|
119 | if (deleteFix === undefined) {
|
120 | var fix = new Lint.Fix(Rule.metadata.ruleName, [
|
121 | this.appendText(secondToken.getFullStart(), " "),
|
122 | ]);
|
123 | var failureString = Rule.FAILURE_NO_BEGINNING_SPACE(firstToken.getText());
|
124 | this.addFailure(this.createFailure(nodeStart, 1, failureString, fix));
|
125 | }
|
126 | deleteFix = this.getDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
|
127 | if (deleteFix === undefined) {
|
128 | var fix = new Lint.Fix(Rule.metadata.ruleName, [
|
129 | this.appendText(lastToken.getStart(), " "),
|
130 | ]);
|
131 | var failureString = Rule.FAILURE_NO_ENDING_SPACE(lastToken.getText());
|
132 | this.addFailure(this.createFailure(nodeStart + nodeWidth - 1, 1, failureString, fix));
|
133 | }
|
134 | }
|
135 | else if (this.hasOption(OPTION_NEVER)) {
|
136 | var firstAndSecondTokensCombinedText = getTokensCombinedText(firstToken, secondToken);
|
137 | var lastAndSecondToLastCombinedText = getTokensCombinedText(secondToLastToken, lastToken);
|
138 | if (!isExpressionMultiline(firstAndSecondTokensCombinedText)) {
|
139 | var fix = this.getDeleteFixForSpaceBetweenTokens(firstToken, secondToken);
|
140 | if (fix !== undefined) {
|
141 | var failureString = Rule.FAILURE_FORBIDDEN_SPACES_BEGINNING(firstToken.getText());
|
142 | this.addFailure(this.createFailure(nodeStart, 1, failureString, fix));
|
143 | }
|
144 | }
|
145 | if (!isExpressionMultiline(lastAndSecondToLastCombinedText)) {
|
146 | var fix = this.getDeleteFixForSpaceBetweenTokens(secondToLastToken, lastToken);
|
147 | if (fix !== undefined) {
|
148 | var failureString = Rule.FAILURE_FORBIDDEN_SPACES_END(lastToken.getText());
|
149 |
|
150 |
|
151 | var failure = firstToken === secondToLastToken ?
|
152 | this.createFailure(nodeStart + nodeWidth - 1, 1, failureString) :
|
153 | this.createFailure(nodeStart + nodeWidth - 1, 1, failureString, fix);
|
154 | this.addFailure(failure);
|
155 | }
|
156 | }
|
157 | }
|
158 | };
|
159 | JsxCurlySpacingWalker.prototype.getDeleteFixForSpaceBetweenTokens = function (firstNode, secondNode) {
|
160 | if (firstNode.parent !== secondNode.parent) {
|
161 | throw Error("Expected identical parents for both nodes");
|
162 | }
|
163 | var parent = firstNode.parent;
|
164 | var parentStart = parent.getStart();
|
165 | var secondNodeStart = secondNode.getFullStart();
|
166 | var firstNodeEnd = firstNode.getStart() + firstNode.getWidth();
|
167 | var secondNodeRelativeStart = secondNodeStart - parentStart;
|
168 | var firstNodeRelativeEnd = firstNodeEnd - parentStart;
|
169 | var parentText = parent.getText();
|
170 | var trailingComments = ts.getTrailingCommentRanges(parentText, firstNodeRelativeEnd) || [];
|
171 | var leadingComments = ts.getLeadingCommentRanges(parentText, secondNodeRelativeStart) || [];
|
172 | var comments = trailingComments.concat(leadingComments);
|
173 | if (secondNode.getStart() - firstNode.getStart() - firstNode.getWidth() > getTotalCharCount(comments)) {
|
174 | var replacements = comments.map(function (comment) { return parentText.slice(comment.pos, comment.end); }).join("");
|
175 | return new Lint.Fix(Rule.metadata.ruleName, [
|
176 | this.createReplacement(secondNodeStart, secondNode.getStart() - secondNodeStart, replacements),
|
177 | ]);
|
178 | }
|
179 | else {
|
180 | return undefined;
|
181 | }
|
182 | };
|
183 | return JsxCurlySpacingWalker;
|
184 | }(Lint.RuleWalker));
|
185 | var _a;
|