1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const docs_url_1 = require("../utils/docs-url");
|
23 | const rule = {
|
24 | meta: {
|
25 | type: 'problem',
|
26 | docs: {
|
27 | description: 'Non-existent operators "=+", "=-" and "=!" should not be used',
|
28 | category: 'Possible Errors',
|
29 | recommended: 'error',
|
30 | url: docs_url_1.default(__filename),
|
31 | },
|
32 | },
|
33 | create(context) {
|
34 | return {
|
35 | AssignmentExpression(node) {
|
36 | const assignmentExpression = node;
|
37 | if (assignmentExpression.operator === '=') {
|
38 | checkOperator(context, assignmentExpression.right);
|
39 | }
|
40 | },
|
41 | VariableDeclarator(node) {
|
42 | const variableDeclarator = node;
|
43 | checkOperator(context, variableDeclarator.init);
|
44 | },
|
45 | };
|
46 | },
|
47 | };
|
48 | function checkOperator(context, unaryNode) {
|
49 | if (unaryNode &&
|
50 | unaryNode.type === 'UnaryExpression' &&
|
51 | isUnaryOperatorOfInterest(unaryNode.operator)) {
|
52 | const sourceCode = context.getSourceCode();
|
53 | const assignmentOperatorToken = sourceCode.getTokenBefore(unaryNode, token => token.value === '=');
|
54 | const unaryOperatorToken = sourceCode.getFirstToken(unaryNode);
|
55 | const expressionFirstToken = sourceCode.getFirstToken(unaryNode.argument);
|
56 | if (assignmentOperatorToken != null &&
|
57 | unaryOperatorToken != null &&
|
58 | expressionFirstToken != null &&
|
59 | areAdjacent(assignmentOperatorToken, unaryOperatorToken) &&
|
60 | !areAdjacent(unaryOperatorToken, expressionFirstToken)) {
|
61 | context.report({
|
62 | message: `Was "${unaryNode.operator}=" meant instead?`,
|
63 | loc: { start: assignmentOperatorToken.loc.start, end: unaryOperatorToken.loc.end },
|
64 | });
|
65 | }
|
66 | }
|
67 | }
|
68 | function isUnaryOperatorOfInterest(operator) {
|
69 | return operator === '-' || operator === '+' || operator === '!';
|
70 | }
|
71 | function areAdjacent(first, second) {
|
72 | return (first.loc.end.column === second.loc.start.column && first.loc.end.line === second.loc.start.line);
|
73 | }
|
74 | module.exports = rule;
|
75 |
|
\ | No newline at end of file |