source: imaps-frontend/node_modules/eslint/lib/rules/dot-location.js@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.4 KB
Line 
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
9const astUtils = require("./utils/ast-utils");
10
11//------------------------------------------------------------------------------
12// Rule Definition
13//------------------------------------------------------------------------------
14
15/** @type {import('../shared/types').Rule} */
16module.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};
Note: See TracBrowser for help on using the repository browser.