1 | /**
|
---|
2 | * @fileoverview Validates newlines before and after dots
|
---|
3 | * @author Greg Cochard
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | const astUtils = require("./utils/ast-utils");
|
---|
10 |
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 | // Rule Definition
|
---|
13 | //------------------------------------------------------------------------------
|
---|
14 |
|
---|
15 | /** @type {import('../shared/types').Rule} */
|
---|
16 | module.exports = {
|
---|
17 | meta: {
|
---|
18 | deprecated: true,
|
---|
19 | replacedBy: [],
|
---|
20 | type: "layout",
|
---|
21 |
|
---|
22 | docs: {
|
---|
23 | description: "Enforce consistent newlines before and after dots",
|
---|
24 | recommended: false,
|
---|
25 | url: "https://eslint.org/docs/latest/rules/dot-location"
|
---|
26 | },
|
---|
27 |
|
---|
28 | schema: [
|
---|
29 | {
|
---|
30 | enum: ["object", "property"]
|
---|
31 | }
|
---|
32 | ],
|
---|
33 |
|
---|
34 | fixable: "code",
|
---|
35 |
|
---|
36 | messages: {
|
---|
37 | expectedDotAfterObject: "Expected dot to be on same line as object.",
|
---|
38 | expectedDotBeforeProperty: "Expected dot to be on same line as property."
|
---|
39 | }
|
---|
40 | },
|
---|
41 |
|
---|
42 | create(context) {
|
---|
43 |
|
---|
44 | const config = context.options[0];
|
---|
45 |
|
---|
46 | // default to onObject if no preference is passed
|
---|
47 | const onObject = config === "object" || !config;
|
---|
48 |
|
---|
49 | const sourceCode = context.sourceCode;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Reports if the dot between object and property is on the correct location.
|
---|
53 | * @param {ASTNode} node The `MemberExpression` node.
|
---|
54 | * @returns {void}
|
---|
55 | */
|
---|
56 | function checkDotLocation(node) {
|
---|
57 | const property = node.property;
|
---|
58 | const dotToken = sourceCode.getTokenBefore(property);
|
---|
59 |
|
---|
60 | if (onObject) {
|
---|
61 |
|
---|
62 | // `obj` expression can be parenthesized, but those paren tokens are not a part of the `obj` node.
|
---|
63 | const tokenBeforeDot = sourceCode.getTokenBefore(dotToken);
|
---|
64 |
|
---|
65 | if (!astUtils.isTokenOnSameLine(tokenBeforeDot, dotToken)) {
|
---|
66 | context.report({
|
---|
67 | node,
|
---|
68 | loc: dotToken.loc,
|
---|
69 | messageId: "expectedDotAfterObject",
|
---|
70 | *fix(fixer) {
|
---|
71 | if (dotToken.value.startsWith(".") && astUtils.isDecimalIntegerNumericToken(tokenBeforeDot)) {
|
---|
72 | yield fixer.insertTextAfter(tokenBeforeDot, ` ${dotToken.value}`);
|
---|
73 | } else {
|
---|
74 | yield fixer.insertTextAfter(tokenBeforeDot, dotToken.value);
|
---|
75 | }
|
---|
76 | yield fixer.remove(dotToken);
|
---|
77 | }
|
---|
78 | });
|
---|
79 | }
|
---|
80 | } else if (!astUtils.isTokenOnSameLine(dotToken, property)) {
|
---|
81 | context.report({
|
---|
82 | node,
|
---|
83 | loc: dotToken.loc,
|
---|
84 | messageId: "expectedDotBeforeProperty",
|
---|
85 | *fix(fixer) {
|
---|
86 | yield fixer.remove(dotToken);
|
---|
87 | yield fixer.insertTextBefore(property, dotToken.value);
|
---|
88 | }
|
---|
89 | });
|
---|
90 | }
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Checks the spacing of the dot within a member expression.
|
---|
95 | * @param {ASTNode} node The node to check.
|
---|
96 | * @returns {void}
|
---|
97 | */
|
---|
98 | function checkNode(node) {
|
---|
99 | if (!node.computed) {
|
---|
100 | checkDotLocation(node);
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 | return {
|
---|
105 | MemberExpression: checkNode
|
---|
106 | };
|
---|
107 | }
|
---|
108 | };
|
---|