forked from bradzacher/eslint-plugin-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
space-infix-ops.js
110 lines (96 loc) · 3.53 KB
/
space-infix-ops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @fileoverview Require spaces around infix operators
* @author Michael Ficarra
* @author Bence Dányi
*/
"use strict";
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = {
meta: {
docs: {
description:
"Require spacing around infix operators in TypeScript-specific constructs (to be used in tandem with the code `space-infix-ops` rule)",
category: "Stylistic Issues",
recommended: false,
url:
"https://github.com/nzakas/eslint-plugin-typescript/blob/master/docs/rules/space-infix-ops.md"
},
fixable: "whitespace"
},
create(context) {
const sourceCode = context.getSourceCode();
/**
* Returns the first token which violates the rule
* @param {ASTNode} left - The left node of the main node
* @param {ASTNode} right - The right node of the main node
* @returns {Object} The violator token or null
* @private
*/
function getFirstNonSpacedToken(left, right) {
const tokens = sourceCode.getTokensBetween(left, right, 1);
for (let i = 1, l = tokens.length - 1; i < l; ++i) {
const op = tokens[i];
if (
op.type === "Punctuator" &&
op.value === "=" &&
(tokens[i - 1].range[1] >= op.range[0] ||
op.range[1] >= tokens[i + 1].range[0])
) {
return op;
}
}
return null;
}
/**
* Reports an AST node as a rule violation
* @param {ASTNode} mainNode - The node to report
* @param {Object} culpritToken - The token which has a problem
* @returns {void}
* @private
*/
function report(mainNode, culpritToken) {
context.report({
node: mainNode,
loc: culpritToken.loc.start,
message: "Infix operators must be spaced.",
fix(fixer) {
const previousToken = sourceCode.getTokenBefore(
culpritToken
);
const afterToken = sourceCode.getTokenAfter(culpritToken);
let fixString = "";
if (culpritToken.range[0] - previousToken.range[1] === 0) {
fixString = " ";
}
fixString += culpritToken.value;
if (afterToken.range[0] - culpritToken.range[1] === 0) {
fixString += " ";
}
return fixer.replaceText(culpritToken, fixString);
}
});
}
/**
* Check if the node is a type alias
* @param {ASTNode} node node to evaluate
* @returns {void}
* @private
*/
function checkAlias(node) {
if (node.parent.kind !== "type") return;
const leftNode = node.typeParameters
? node.typeParameters
: node.id;
const rightNode = node.init;
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
if (nonSpacedNode) {
report(node, nonSpacedNode);
}
}
return {
VariableDeclarator: checkAlias
};
}
};