UNPKG

1.81 kBJavaScriptView Raw
1/**
2 * @fileoverview Validates newlines before and after dots
3 * @author Greg Cochard
4 * @copyright 2015 Greg Cochard
5 */
6
7"use strict";
8
9var astUtils = require("../ast-utils");
10
11//------------------------------------------------------------------------------
12// Rule Definition
13//------------------------------------------------------------------------------
14
15module.exports = function(context) {
16
17 var config = context.options[0],
18 // default to onObject if no preference is passed
19 onObject = config === "object" || !config;
20
21 /**
22 * Reports if the dot between object and property is on the correct loccation.
23 * @param {ASTNode} obj The object owning the property.
24 * @param {ASTNode} prop The property of the object.
25 * @param {ASTNode} node The corresponding node of the token.
26 * @returns {void}
27 */
28 function checkDotLocation(obj, prop, node) {
29 var dot = context.getTokenBefore(prop);
30
31 if (dot.type === "Punctuator" && dot.value === ".") {
32 if (onObject) {
33 if (!astUtils.isTokenOnSameLine(obj, dot)) {
34 context.report(node, dot.loc.start, "Expected dot to be on same line as object.");
35 }
36 } else if (!astUtils.isTokenOnSameLine(dot, prop)) {
37 context.report(node, dot.loc.start, "Expected dot to be on same line as property.");
38 }
39 }
40 }
41
42 /**
43 * Checks the spacing of the dot within a member expression.
44 * @param {ASTNode} node The node to check.
45 * @returns {void}
46 */
47 function checkNode(node) {
48 checkDotLocation(node.object, node.property, node);
49 }
50
51 return {
52 "MemberExpression": checkNode
53 };
54};
55
56module.exports.schema = [
57 {
58 "enum": ["object", "property"]
59 }
60];