Merge pull request babel/eslint-plugin-babel#138 from babel/composer
Refactor rules to use eslint-rule-composer
This commit is contained in:
parent
15c5245d55
commit
b41b3af879
@ -1,727 +0,0 @@
|
||||
/**
|
||||
* @fileoverview Common utils for AST.
|
||||
* @author Gyandeep Singh
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
const anyFunctionPattern = /^(?:Function(?:Declaration|Expression)|ArrowFunctionExpression)$/;
|
||||
const anyLoopPattern = /^(?:DoWhile|For|ForIn|ForOf|While)Statement$/;
|
||||
const arrayOrTypedArrayPattern = /Array$/;
|
||||
const arrayMethodPattern = /^(?:every|filter|find|findIndex|forEach|map|some)$/;
|
||||
const bindOrCallOrApplyPattern = /^(?:bind|call|apply)$/;
|
||||
const breakableTypePattern = /^(?:(?:Do)?While|For(?:In|Of)?|Switch)Statement$/;
|
||||
const thisTagPattern = /^[\s\*]*@this/m;
|
||||
|
||||
/**
|
||||
* Checks reference if is non initializer and writable.
|
||||
* @param {Reference} reference - A reference to check.
|
||||
* @param {int} index - The index of the reference in the references.
|
||||
* @param {Reference[]} references - The array that the reference belongs to.
|
||||
* @returns {boolean} Success/Failure
|
||||
* @private
|
||||
*/
|
||||
function isModifyingReference(reference, index, references) {
|
||||
const identifier = reference.identifier;
|
||||
|
||||
/*
|
||||
* Destructuring assignments can have multiple default value, so
|
||||
* possibly there are multiple writeable references for the same
|
||||
* identifier.
|
||||
*/
|
||||
const modifyingDifferentIdentifier = index === 0 ||
|
||||
references[index - 1].identifier !== identifier;
|
||||
|
||||
return (identifier &&
|
||||
reference.init === false &&
|
||||
reference.isWrite() &&
|
||||
modifyingDifferentIdentifier
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the given string starts with uppercase or not.
|
||||
*
|
||||
* @param {string} s - The string to check.
|
||||
* @returns {boolean} `true` if the string starts with uppercase.
|
||||
*/
|
||||
function startsWithUpperCase(s) {
|
||||
return s[0] !== s[0].toLocaleLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is a constructor.
|
||||
* @param {ASTNode} node - A function node to check.
|
||||
* @returns {boolean} Wehether or not a node is a constructor.
|
||||
*/
|
||||
function isES5Constructor(node) {
|
||||
return (node.id && startsWithUpperCase(node.id.name));
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a function node from ancestors of a node.
|
||||
* @param {ASTNode} node - A start node to find.
|
||||
* @returns {Node|null} A found function node.
|
||||
*/
|
||||
function getUpperFunction(node) {
|
||||
while (node) {
|
||||
if (anyFunctionPattern.test(node.type)) {
|
||||
return node;
|
||||
}
|
||||
node = node.parent;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is `null` or `undefined`.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} Whether or not the node is a `null` or `undefined`.
|
||||
* @public
|
||||
*/
|
||||
function isNullOrUndefined(node) {
|
||||
return (
|
||||
(node.type === "Literal" && node.value === null) ||
|
||||
(node.type === "Identifier" && node.name === "undefined") ||
|
||||
(node.type === "UnaryExpression" && node.operator === "void")
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is callee.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} Whether or not the node is callee.
|
||||
*/
|
||||
function isCallee(node) {
|
||||
return node.parent.type === "CallExpression" && node.parent.callee === node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is `Reclect.apply`.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} Whether or not the node is a `Reclect.apply`.
|
||||
*/
|
||||
function isReflectApply(node) {
|
||||
return (
|
||||
node.type === "MemberExpression" &&
|
||||
node.object.type === "Identifier" &&
|
||||
node.object.name === "Reflect" &&
|
||||
node.property.type === "Identifier" &&
|
||||
node.property.name === "apply" &&
|
||||
node.computed === false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is `Array.from`.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} Whether or not the node is a `Array.from`.
|
||||
*/
|
||||
function isArrayFromMethod(node) {
|
||||
return (
|
||||
node.type === "MemberExpression" &&
|
||||
node.object.type === "Identifier" &&
|
||||
arrayOrTypedArrayPattern.test(node.object.name) &&
|
||||
node.property.type === "Identifier" &&
|
||||
node.property.name === "from" &&
|
||||
node.computed === false
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node is a method which has `thisArg`.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} Whether or not the node is a method which has `thisArg`.
|
||||
*/
|
||||
function isMethodWhichHasThisArg(node) {
|
||||
while (node) {
|
||||
if (node.type === "Identifier") {
|
||||
return arrayMethodPattern.test(node.name);
|
||||
}
|
||||
if (node.type === "MemberExpression" && !node.computed) {
|
||||
node = node.property;
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether or not a node has a `@this` tag in its comments.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @param {SourceCode} sourceCode - A SourceCode instance to get comments.
|
||||
* @returns {boolean} Whether or not the node has a `@this` tag in its comments.
|
||||
*/
|
||||
function hasJSDocThisTag(node, sourceCode) {
|
||||
const jsdocComment = sourceCode.getJSDocComment(node);
|
||||
|
||||
if (jsdocComment && thisTagPattern.test(jsdocComment.value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Checks `@this` in its leading comments for callbacks,
|
||||
// because callbacks don't have its JSDoc comment.
|
||||
// e.g.
|
||||
// sinon.test(/* @this sinon.Sandbox */function() { this.spy(); });
|
||||
return sourceCode.getComments(node).leading.some(function(comment) {
|
||||
return thisTagPattern.test(comment.value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if a node is surrounded by parentheses.
|
||||
* @param {SourceCode} sourceCode The ESLint source code object
|
||||
* @param {ASTNode} node The node to be checked.
|
||||
* @returns {boolean} True if the node is parenthesised.
|
||||
* @private
|
||||
*/
|
||||
function isParenthesised(sourceCode, node) {
|
||||
const previousToken = sourceCode.getTokenBefore(node),
|
||||
nextToken = sourceCode.getTokenAfter(node);
|
||||
|
||||
return Boolean(previousToken && nextToken) &&
|
||||
previousToken.value === "(" && previousToken.range[1] <= node.range[0] &&
|
||||
nextToken.value === ")" && nextToken.range[0] >= node.range[1];
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Public Interface
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* Determines whether two adjacent tokens are on the same line.
|
||||
* @param {Object} left - The left token object.
|
||||
* @param {Object} right - The right token object.
|
||||
* @returns {boolean} Whether or not the tokens are on the same line.
|
||||
* @public
|
||||
*/
|
||||
isTokenOnSameLine(left, right) {
|
||||
return left.loc.end.line === right.loc.start.line;
|
||||
},
|
||||
|
||||
isNullOrUndefined,
|
||||
isCallee,
|
||||
isES5Constructor,
|
||||
getUpperFunction,
|
||||
isArrayFromMethod,
|
||||
isParenthesised,
|
||||
|
||||
/**
|
||||
* Checks whether or not a given node is a string literal.
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} `true` if the node is a string literal.
|
||||
*/
|
||||
isStringLiteral(node) {
|
||||
return (
|
||||
(node.type === "Literal" && typeof node.value === "string") ||
|
||||
node.type === "TemplateLiteral"
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether a given node is a breakable statement or not.
|
||||
* The node is breakable if the node is one of the following type:
|
||||
*
|
||||
* - DoWhileStatement
|
||||
* - ForInStatement
|
||||
* - ForOfStatement
|
||||
* - ForStatement
|
||||
* - SwitchStatement
|
||||
* - WhileStatement
|
||||
*
|
||||
* @param {ASTNode} node - A node to check.
|
||||
* @returns {boolean} `true` if the node is breakable.
|
||||
*/
|
||||
isBreakableStatement(node) {
|
||||
return breakableTypePattern.test(node.type);
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the label if the parent node of a given node is a LabeledStatement.
|
||||
*
|
||||
* @param {ASTNode} node - A node to get.
|
||||
* @returns {string|null} The label or `null`.
|
||||
*/
|
||||
getLabel(node) {
|
||||
if (node.parent.type === "LabeledStatement") {
|
||||
return node.parent.label.name;
|
||||
}
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets references which are non initializer and writable.
|
||||
* @param {Reference[]} references - An array of references.
|
||||
* @returns {Reference[]} An array of only references which are non initializer and writable.
|
||||
* @public
|
||||
*/
|
||||
getModifyingReferences(references) {
|
||||
return references.filter(isModifyingReference);
|
||||
},
|
||||
|
||||
/**
|
||||
* Validate that a string passed in is surrounded by the specified character
|
||||
* @param {string} val The text to check.
|
||||
* @param {string} character The character to see if it's surrounded by.
|
||||
* @returns {boolean} True if the text is surrounded by the character, false if not.
|
||||
* @private
|
||||
*/
|
||||
isSurroundedBy(val, character) {
|
||||
return val[0] === character && val[val.length - 1] === character;
|
||||
},
|
||||
|
||||
/**
|
||||
* Returns whether the provided node is an ESLint directive comment or not
|
||||
* @param {LineComment|BlockComment} node The node to be checked
|
||||
* @returns {boolean} `true` if the node is an ESLint directive comment
|
||||
*/
|
||||
isDirectiveComment(node) {
|
||||
const comment = node.value.trim();
|
||||
|
||||
return (
|
||||
node.type === "Line" && comment.indexOf("eslint-") === 0 ||
|
||||
node.type === "Block" && (
|
||||
comment.indexOf("global ") === 0 ||
|
||||
comment.indexOf("eslint ") === 0 ||
|
||||
comment.indexOf("eslint-") === 0
|
||||
)
|
||||
);
|
||||
},
|
||||
|
||||
/**
|
||||
* Finds the variable by a given name in a given scope and its upper scopes.
|
||||
*
|
||||
* @param {escope.Scope} initScope - A scope to start find.
|
||||
* @param {string} name - A variable name to find.
|
||||
* @returns {escope.Variable|null} A found variable or `null`.
|
||||
*/
|
||||
getVariableByName(initScope, name) {
|
||||
let scope = initScope;
|
||||
|
||||
while (scope) {
|
||||
const variable = scope.set.get(name);
|
||||
|
||||
if (variable) {
|
||||
return variable;
|
||||
}
|
||||
|
||||
scope = scope.upper;
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether or not a given function node is the default `this` binding.
|
||||
*
|
||||
* First, this checks the node:
|
||||
*
|
||||
* - The function name does not start with uppercase (it's a constructor).
|
||||
* - The function does not have a JSDoc comment that has a @this tag.
|
||||
*
|
||||
* Next, this checks the location of the node.
|
||||
* If the location is below, this judges `this` is valid.
|
||||
*
|
||||
* - The location is not on an object literal.
|
||||
* - The location is not assigned to a variable which starts with an uppercase letter.
|
||||
* - The location is not on an ES2015 class.
|
||||
* - Its `bind`/`call`/`apply` method is not called directly.
|
||||
* - The function is not a callback of array methods (such as `.forEach()`) if `thisArg` is given.
|
||||
*
|
||||
* @param {ASTNode} node - A function node to check.
|
||||
* @param {SourceCode} sourceCode - A SourceCode instance to get comments.
|
||||
* @returns {boolean} The function node is the default `this` binding.
|
||||
*/
|
||||
isDefaultThisBinding(node, sourceCode) {
|
||||
if (isES5Constructor(node) || hasJSDocThisTag(node, sourceCode)) {
|
||||
return false;
|
||||
}
|
||||
const isAnonymous = node.id === null;
|
||||
|
||||
while (node) {
|
||||
const parent = node.parent;
|
||||
|
||||
switch (parent.type) {
|
||||
|
||||
/*
|
||||
* Looks up the destination.
|
||||
* e.g., obj.foo = nativeFoo || function foo() { ... };
|
||||
*/
|
||||
case "LogicalExpression":
|
||||
case "ConditionalExpression":
|
||||
node = parent;
|
||||
break;
|
||||
|
||||
// If the upper function is IIFE, checks the destination of the return value.
|
||||
// e.g.
|
||||
// obj.foo = (function() {
|
||||
// // setup...
|
||||
// return function foo() { ... };
|
||||
// })();
|
||||
case "ReturnStatement": {
|
||||
const func = getUpperFunction(parent);
|
||||
|
||||
if (func === null || !isCallee(func)) {
|
||||
return true;
|
||||
}
|
||||
node = func.parent;
|
||||
break;
|
||||
}
|
||||
|
||||
// e.g.
|
||||
// var obj = { foo() { ... } };
|
||||
// var obj = { foo: function() { ... } };
|
||||
// class A { constructor() { ... } }
|
||||
// class A { foo() { ... } }
|
||||
// class A { get foo() { ... } }
|
||||
// class A { set foo() { ... } }
|
||||
// class A { static foo() { ... } }
|
||||
case "Property":
|
||||
case "MethodDefinition":
|
||||
return parent.value !== node;
|
||||
|
||||
// e.g.
|
||||
// obj.foo = function foo() { ... };
|
||||
// Foo = function() { ... };
|
||||
// [obj.foo = function foo() { ... }] = a;
|
||||
// [Foo = function() { ... }] = a;
|
||||
case "AssignmentExpression":
|
||||
case "AssignmentPattern":
|
||||
if (parent.right === node) {
|
||||
if (parent.left.type === "MemberExpression") {
|
||||
return false;
|
||||
}
|
||||
if (isAnonymous &&
|
||||
parent.left.type === "Identifier" &&
|
||||
startsWithUpperCase(parent.left.name)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
// e.g.
|
||||
// var Foo = function() { ... };
|
||||
case "VariableDeclarator":
|
||||
return !(
|
||||
isAnonymous &&
|
||||
parent.init === node &&
|
||||
parent.id.type === "Identifier" &&
|
||||
startsWithUpperCase(parent.id.name)
|
||||
);
|
||||
|
||||
// e.g.
|
||||
// var foo = function foo() { ... }.bind(obj);
|
||||
// (function foo() { ... }).call(obj);
|
||||
// (function foo() { ... }).apply(obj, []);
|
||||
case "MemberExpression":
|
||||
return (
|
||||
parent.object !== node ||
|
||||
parent.property.type !== "Identifier" ||
|
||||
!bindOrCallOrApplyPattern.test(parent.property.name) ||
|
||||
!isCallee(parent) ||
|
||||
parent.parent.arguments.length === 0 ||
|
||||
isNullOrUndefined(parent.parent.arguments[0])
|
||||
);
|
||||
|
||||
// e.g.
|
||||
// Reflect.apply(function() {}, obj, []);
|
||||
// Array.from([], function() {}, obj);
|
||||
// list.forEach(function() {}, obj);
|
||||
case "CallExpression":
|
||||
if (isReflectApply(parent.callee)) {
|
||||
return (
|
||||
parent.arguments.length !== 3 ||
|
||||
parent.arguments[0] !== node ||
|
||||
isNullOrUndefined(parent.arguments[1])
|
||||
);
|
||||
}
|
||||
if (isArrayFromMethod(parent.callee)) {
|
||||
return (
|
||||
parent.arguments.length !== 3 ||
|
||||
parent.arguments[1] !== node ||
|
||||
isNullOrUndefined(parent.arguments[2])
|
||||
);
|
||||
}
|
||||
if (isMethodWhichHasThisArg(parent.callee)) {
|
||||
return (
|
||||
parent.arguments.length !== 2 ||
|
||||
parent.arguments[0] !== node ||
|
||||
isNullOrUndefined(parent.arguments[1])
|
||||
);
|
||||
}
|
||||
return true;
|
||||
|
||||
// Otherwise `this` is default.
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get the precedence level based on the node type
|
||||
* @param {ASTNode} node node to evaluate
|
||||
* @returns {int} precedence level
|
||||
* @private
|
||||
*/
|
||||
getPrecedence(node) {
|
||||
switch (node.type) {
|
||||
case "SequenceExpression":
|
||||
return 0;
|
||||
|
||||
case "AssignmentExpression":
|
||||
case "ArrowFunctionExpression":
|
||||
case "YieldExpression":
|
||||
return 1;
|
||||
|
||||
case "ConditionalExpression":
|
||||
return 3;
|
||||
|
||||
case "LogicalExpression":
|
||||
switch (node.operator) {
|
||||
case "||":
|
||||
return 4;
|
||||
case "&&":
|
||||
return 5;
|
||||
|
||||
// no default
|
||||
}
|
||||
|
||||
/* falls through */
|
||||
|
||||
case "BinaryExpression":
|
||||
|
||||
switch (node.operator) {
|
||||
case "|":
|
||||
return 6;
|
||||
case "^":
|
||||
return 7;
|
||||
case "&":
|
||||
return 8;
|
||||
case "==":
|
||||
case "!=":
|
||||
case "===":
|
||||
case "!==":
|
||||
return 9;
|
||||
case "<":
|
||||
case "<=":
|
||||
case ">":
|
||||
case ">=":
|
||||
case "in":
|
||||
case "instanceof":
|
||||
return 10;
|
||||
case "<<":
|
||||
case ">>":
|
||||
case ">>>":
|
||||
return 11;
|
||||
case "+":
|
||||
case "-":
|
||||
return 12;
|
||||
case "*":
|
||||
case "/":
|
||||
case "%":
|
||||
return 13;
|
||||
|
||||
// no default
|
||||
}
|
||||
|
||||
/* falls through */
|
||||
|
||||
case "UnaryExpression":
|
||||
case "AwaitExpression":
|
||||
return 14;
|
||||
|
||||
case "UpdateExpression":
|
||||
return 15;
|
||||
|
||||
case "CallExpression":
|
||||
|
||||
// IIFE is allowed to have parens in any position (#655)
|
||||
if (node.callee.type === "FunctionExpression") {
|
||||
return -1;
|
||||
}
|
||||
return 16;
|
||||
|
||||
case "NewExpression":
|
||||
return 17;
|
||||
|
||||
// no default
|
||||
}
|
||||
return 18;
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether a given node is a loop node or not.
|
||||
* The following types are loop nodes:
|
||||
*
|
||||
* - DoWhileStatement
|
||||
* - ForInStatement
|
||||
* - ForOfStatement
|
||||
* - ForStatement
|
||||
* - WhileStatement
|
||||
*
|
||||
* @param {ASTNode|null} node - A node to check.
|
||||
* @returns {boolean} `true` if the node is a loop node.
|
||||
*/
|
||||
isLoop(node) {
|
||||
return Boolean(node && anyLoopPattern.test(node.type));
|
||||
},
|
||||
|
||||
/**
|
||||
* Checks whether a given node is a function node or not.
|
||||
* The following types are function nodes:
|
||||
*
|
||||
* - ArrowFunctionExpression
|
||||
* - FunctionDeclaration
|
||||
* - FunctionExpression
|
||||
*
|
||||
* @param {ASTNode|null} node - A node to check.
|
||||
* @returns {boolean} `true` if the node is a function node.
|
||||
*/
|
||||
isFunction(node) {
|
||||
return Boolean(node && anyFunctionPattern.test(node.type));
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the property name of a given node.
|
||||
* The node can be a MemberExpression, a Property, or a MethodDefinition.
|
||||
*
|
||||
* If the name is dynamic, this returns `null`.
|
||||
*
|
||||
* For examples:
|
||||
*
|
||||
* a.b // => "b"
|
||||
* a["b"] // => "b"
|
||||
* a['b'] // => "b"
|
||||
* a[`b`] // => "b"
|
||||
* a[100] // => "100"
|
||||
* a[b] // => null
|
||||
* a["a" + "b"] // => null
|
||||
* a[tag`b`] // => null
|
||||
* a[`${b}`] // => null
|
||||
*
|
||||
* let a = {b: 1} // => "b"
|
||||
* let a = {["b"]: 1} // => "b"
|
||||
* let a = {['b']: 1} // => "b"
|
||||
* let a = {[`b`]: 1} // => "b"
|
||||
* let a = {[100]: 1} // => "100"
|
||||
* let a = {[b]: 1} // => null
|
||||
* let a = {["a" + "b"]: 1} // => null
|
||||
* let a = {[tag`b`]: 1} // => null
|
||||
* let a = {[`${b}`]: 1} // => null
|
||||
*
|
||||
* @param {ASTNode} node - The node to get.
|
||||
* @returns {string|null} The property name if static. Otherwise, null.
|
||||
*/
|
||||
getStaticPropertyName(node) {
|
||||
let prop;
|
||||
|
||||
switch (node && node.type) {
|
||||
case "Property":
|
||||
case "MethodDefinition":
|
||||
prop = node.key;
|
||||
break;
|
||||
|
||||
case "MemberExpression":
|
||||
prop = node.property;
|
||||
break;
|
||||
|
||||
// no default
|
||||
}
|
||||
|
||||
switch (prop && prop.type) {
|
||||
case "Literal":
|
||||
return String(prop.value);
|
||||
|
||||
case "TemplateLiteral":
|
||||
if (prop.expressions.length === 0 && prop.quasis.length === 1) {
|
||||
return prop.quasis[0].value.cooked;
|
||||
}
|
||||
break;
|
||||
|
||||
case "Identifier":
|
||||
if (!node.computed) {
|
||||
return prop.name;
|
||||
}
|
||||
break;
|
||||
|
||||
// no default
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get directives from directive prologue of a Program or Function node.
|
||||
* @param {ASTNode} node - The node to check.
|
||||
* @returns {ASTNode[]} The directives found in the directive prologue.
|
||||
*/
|
||||
getDirectivePrologue(node) {
|
||||
const directives = [];
|
||||
|
||||
// Directive prologues only occur at the top of files or functions.
|
||||
if (
|
||||
node.type === "Program" ||
|
||||
node.type === "FunctionDeclaration" ||
|
||||
node.type === "FunctionExpression" ||
|
||||
|
||||
// Do not check arrow functions with implicit return.
|
||||
// `() => "use strict";` returns the string `"use strict"`.
|
||||
(node.type === "ArrowFunctionExpression" && node.body.type === "BlockStatement")
|
||||
) {
|
||||
const statements = node.type === "Program" ? node.body : node.body.body;
|
||||
|
||||
for (const statement of statements) {
|
||||
if (
|
||||
statement.type === "ExpressionStatement" &&
|
||||
statement.expression.type === "Literal"
|
||||
) {
|
||||
directives.push(statement);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return directives;
|
||||
},
|
||||
|
||||
|
||||
/**
|
||||
* Determines whether this node is a decimal integer literal. If a node is a decimal integer literal, a dot added
|
||||
after the node will be parsed as a decimal point, rather than a property-access dot.
|
||||
* @param {ASTNode} node - The node to check.
|
||||
* @returns {boolean} `true` if this node is a decimal integer.
|
||||
* @example
|
||||
*
|
||||
* 5 // true
|
||||
* 5. // false
|
||||
* 5.0 // false
|
||||
* 05 // false
|
||||
* 0x5 // false
|
||||
* 0b101 // false
|
||||
* 0o5 // false
|
||||
* 5e0 // false
|
||||
* '5' // false
|
||||
*/
|
||||
isDecimalInteger(node) {
|
||||
return node.type === "Literal" && typeof node.value === "number" && /^(0|[1-9]\d*)$/.test(node.raw);
|
||||
}
|
||||
};
|
||||
@ -27,11 +27,14 @@
|
||||
},
|
||||
"homepage": "https://github.com/babel/eslint-plugin-babel#readme",
|
||||
"peerDependencies": {
|
||||
"eslint": ">=3.0.0"
|
||||
"eslint": ">=4.0.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"eslint-rule-composer": "^0.1.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-eslint": "^7.1.0",
|
||||
"eslint": "^3.0.0",
|
||||
"eslint": "^4.19.1",
|
||||
"lodash.clonedeep": "^4.5.0",
|
||||
"mocha": "^3.0.0"
|
||||
}
|
||||
|
||||
@ -1,235 +1,19 @@
|
||||
/**
|
||||
* @fileoverview Rule to flag use of constructors without capital letters
|
||||
* @author Nicholas C. Zakas
|
||||
* @copyright 2014 Jordan Harband. All rights reserved.
|
||||
* @copyright 2013-2014 Nicholas C. Zakas. All rights reserved.
|
||||
* @copyright 2015 Mathieu M-Gosselin. All rights reserved.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
var CAPS_ALLOWED = [
|
||||
"Array",
|
||||
"Boolean",
|
||||
"Date",
|
||||
"Error",
|
||||
"Function",
|
||||
"Number",
|
||||
"Object",
|
||||
"RegExp",
|
||||
"String",
|
||||
"Symbol"
|
||||
];
|
||||
const ruleComposer = require('eslint-rule-composer');
|
||||
const eslint = require('eslint');
|
||||
const newCapRule = new eslint.Linter().getRules().get('new-cap');
|
||||
|
||||
/**
|
||||
* Ensure that if the key is provided, it must be an array.
|
||||
* @param {Object} obj Object to check with `key`.
|
||||
* @param {string} key Object key to check on `obj`.
|
||||
* @param {*} fallback If obj[key] is not present, this will be returned.
|
||||
* @returns {string[]} Returns obj[key] if it's an Array, otherwise `fallback`
|
||||
* Returns whether a node is under a decorator or not.
|
||||
* @param {ASTNode} node CallExpression node
|
||||
* @returns {Boolean} Returns true if the node is under a decorator.
|
||||
*/
|
||||
function checkArray(obj, key, fallback) {
|
||||
/* istanbul ignore if */
|
||||
if (Object.prototype.hasOwnProperty.call(obj, key) && !Array.isArray(obj[key])) {
|
||||
throw new TypeError(key + ", if provided, must be an Array");
|
||||
}
|
||||
return obj[key] || fallback;
|
||||
function isDecorator(node) {
|
||||
return node.parent.type === "Decorator";
|
||||
}
|
||||
|
||||
/**
|
||||
* A reducer function to invert an array to an Object mapping the string form of the key, to `true`.
|
||||
* @param {Object} map Accumulator object for the reduce.
|
||||
* @param {string} key Object key to set to `true`.
|
||||
* @returns {Object} Returns the updated Object for further reduction.
|
||||
*/
|
||||
function invert(map, key) {
|
||||
map[key] = true;
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an object with the cap is new exceptions as its keys and true as their values.
|
||||
* @param {Object} config Rule configuration
|
||||
* @returns {Object} Object with cap is new exceptions.
|
||||
*/
|
||||
function calculateCapIsNewExceptions(config) {
|
||||
var capIsNewExceptions = checkArray(config, "capIsNewExceptions", CAPS_ALLOWED);
|
||||
|
||||
if (capIsNewExceptions !== CAPS_ALLOWED) {
|
||||
capIsNewExceptions = capIsNewExceptions.concat(CAPS_ALLOWED);
|
||||
}
|
||||
|
||||
return capIsNewExceptions.reduce(invert, {});
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = function(context) {
|
||||
|
||||
var config = context.options[0] || {};
|
||||
var NEW_IS_CAP = config.newIsCap !== false;
|
||||
var CAP_IS_NEW = config.capIsNew !== false;
|
||||
|
||||
var newIsCapExceptions = checkArray(config, "newIsCapExceptions", []).reduce(invert, {});
|
||||
|
||||
var capIsNewExceptions = calculateCapIsNewExceptions(config);
|
||||
|
||||
var listeners = {};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get exact callee name from expression
|
||||
* @param {ASTNode} node CallExpression or NewExpression node
|
||||
* @returns {string} name
|
||||
*/
|
||||
function extractNameFromExpression(node) {
|
||||
|
||||
var name = "",
|
||||
property;
|
||||
|
||||
if (node.callee.type === "MemberExpression") {
|
||||
property = node.callee.property;
|
||||
|
||||
if (property.type === "Literal" && (typeof property.value === "string")) {
|
||||
name = property.value;
|
||||
} else if (property.type === "Identifier" && !node.callee.computed) {
|
||||
name = property.name;
|
||||
}
|
||||
} else {
|
||||
name = node.callee.name;
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the capitalization state of the string -
|
||||
* Whether the first character is uppercase, lowercase, or non-alphabetic
|
||||
* @param {string} str String
|
||||
* @returns {string} capitalization state: "non-alpha", "lower", or "upper"
|
||||
*/
|
||||
function getCap(str) {
|
||||
var firstChar = str.charAt(0);
|
||||
|
||||
var firstCharLower = firstChar.toLowerCase();
|
||||
var firstCharUpper = firstChar.toUpperCase();
|
||||
|
||||
if (firstCharLower === firstCharUpper) {
|
||||
// char has no uppercase variant, so it's non-alphabetic
|
||||
return "non-alpha";
|
||||
} else if (firstChar === firstCharLower) {
|
||||
return "lower";
|
||||
} else {
|
||||
return "upper";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a node is under a decorator or not.
|
||||
* @param {ASTNode} node CallExpression node
|
||||
* @returns {Boolean} Returns true if the node is under a decorator.
|
||||
*/
|
||||
function isDecorator(node) {
|
||||
return node.parent.type === "Decorator";
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if capitalization is allowed for a CallExpression
|
||||
* @param {Object} allowedMap Object mapping calleeName to a Boolean
|
||||
* @param {ASTNode} node CallExpression node
|
||||
* @param {string} calleeName Capitalized callee name from a CallExpression
|
||||
* @returns {Boolean} Returns true if the callee may be capitalized
|
||||
*/
|
||||
function isCapAllowed(allowedMap, node, calleeName) {
|
||||
if (allowedMap[calleeName] || allowedMap[context.getSource(node.callee)]) {
|
||||
return true;
|
||||
}
|
||||
if (calleeName === "UTC" && node.callee.type === "MemberExpression") {
|
||||
// allow if callee is Date.UTC
|
||||
return node.callee.object.type === "Identifier" &&
|
||||
node.callee.object.name === "Date";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports the given message for the given node. The location will be the start of the property or the callee.
|
||||
* @param {ASTNode} node CallExpression or NewExpression node.
|
||||
* @param {string} message The message to report.
|
||||
* @returns {void}
|
||||
*/
|
||||
function report(node, message) {
|
||||
var callee = node.callee;
|
||||
|
||||
if (callee.type === "MemberExpression") {
|
||||
callee = callee.property;
|
||||
}
|
||||
|
||||
context.report(node, callee.loc.start, message);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Public
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
if (NEW_IS_CAP) {
|
||||
listeners.NewExpression = function(node) {
|
||||
|
||||
var constructorName = extractNameFromExpression(node);
|
||||
if (constructorName) {
|
||||
var capitalization = getCap(constructorName);
|
||||
var isAllowed = capitalization !== "lower" || isCapAllowed(newIsCapExceptions, node, constructorName);
|
||||
if (!isAllowed) {
|
||||
report(node, "A constructor name should not start with a lowercase letter.");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
if (CAP_IS_NEW) {
|
||||
listeners.CallExpression = function(node) {
|
||||
|
||||
var calleeName = extractNameFromExpression(node);
|
||||
if (calleeName) {
|
||||
var capitalization = getCap(calleeName);
|
||||
var isAllowed = capitalization !== "upper" || isDecorator(node) || isCapAllowed(capIsNewExceptions, node, calleeName);
|
||||
if (!isAllowed) {
|
||||
report(node, "A function with a name starting with an uppercase letter should only be used as a constructor.");
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
return listeners;
|
||||
};
|
||||
|
||||
module.exports.schema = [
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"newIsCap": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"capIsNew": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"newIsCapExceptions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"capIsNewExceptions": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
];
|
||||
module.exports = ruleComposer.filterReports(
|
||||
newCapRule,
|
||||
(problem, metadata) => !isDecorator(problem.node)
|
||||
);
|
||||
|
||||
@ -1,143 +1,24 @@
|
||||
/**
|
||||
* @fileoverview A rule to disallow `this` keywords outside of classes or class-like objects.
|
||||
* @author Toru Nagashima
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
//------------------------------------------------------------------------------
|
||||
const ruleComposer = require('eslint-rule-composer');
|
||||
const eslint = require('eslint');
|
||||
const noInvalidThisRule = new eslint.Linter().getRules().get('no-invalid-this');
|
||||
|
||||
const astUtils = require("../ast-utils");
|
||||
module.exports = ruleComposer.filterReports(
|
||||
noInvalidThisRule,
|
||||
(problem, metadata) => {
|
||||
let inClassProperty = false;
|
||||
let node = problem.node;
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: "disallow `this` keywords outside of classes or class-like objects",
|
||||
category: "Best Practices",
|
||||
recommended: false
|
||||
},
|
||||
|
||||
schema: []
|
||||
},
|
||||
|
||||
create(context) {
|
||||
const stack = [],
|
||||
sourceCode = context.getSourceCode();
|
||||
|
||||
let insideClassProperty = false;
|
||||
|
||||
/**
|
||||
* Gets the current checking context.
|
||||
*
|
||||
* The return value has a flag that whether or not `this` keyword is valid.
|
||||
* The flag is initialized when got at the first time.
|
||||
*
|
||||
* @returns {{valid: boolean}}
|
||||
* an object which has a flag that whether or not `this` keyword is valid.
|
||||
*/
|
||||
stack.getCurrent = function() {
|
||||
const current = this[this.length - 1];
|
||||
|
||||
if (!current.init) {
|
||||
current.init = true;
|
||||
current.valid = !astUtils.isDefaultThisBinding(
|
||||
current.node,
|
||||
sourceCode);
|
||||
while (node) {
|
||||
if (node.type === "ClassProperty") {
|
||||
inClassProperty = true;
|
||||
return;
|
||||
}
|
||||
return current;
|
||||
};
|
||||
|
||||
/**
|
||||
* `this` should be fair game anywhere inside a class property.
|
||||
*
|
||||
* @returns {void}
|
||||
*/
|
||||
function enterClassProperty() {
|
||||
insideClassProperty = true;
|
||||
|
||||
node = node.parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Back to the normal check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function exitClassProperty() {
|
||||
insideClassProperty = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushs new checking context into the stack.
|
||||
*
|
||||
* The checking context is not initialized yet.
|
||||
* Because most functions don't have `this` keyword.
|
||||
* When `this` keyword was found, the checking context is initialized.
|
||||
*
|
||||
* @param {ASTNode} node - A function node that was entered.
|
||||
* @returns {void}
|
||||
*/
|
||||
function enterFunction(node) {
|
||||
|
||||
// `this` can be invalid only under strict mode.
|
||||
stack.push({
|
||||
init: !context.getScope().isStrict,
|
||||
node,
|
||||
valid: true
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Pops the current checking context from the stack.
|
||||
* @returns {void}
|
||||
*/
|
||||
function exitFunction() {
|
||||
stack.pop();
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
/*
|
||||
* `this` is invalid only under strict mode.
|
||||
* Modules is always strict mode.
|
||||
*/
|
||||
Program(node) {
|
||||
const scope = context.getScope(),
|
||||
features = context.parserOptions.ecmaFeatures || {};
|
||||
|
||||
stack.push({
|
||||
init: true,
|
||||
node,
|
||||
valid: !(
|
||||
scope.isStrict ||
|
||||
node.sourceType === "module" ||
|
||||
(features.globalReturn && scope.childScopes[0].isStrict)
|
||||
)
|
||||
});
|
||||
},
|
||||
|
||||
"Program:exit"() {
|
||||
stack.pop();
|
||||
},
|
||||
|
||||
ClassProperty: enterClassProperty,
|
||||
"ClassProperty:exit": exitClassProperty,
|
||||
FunctionDeclaration: enterFunction,
|
||||
"FunctionDeclaration:exit": exitFunction,
|
||||
FunctionExpression: enterFunction,
|
||||
"FunctionExpression:exit": exitFunction,
|
||||
|
||||
// Reports if `this` of the current context is invalid.
|
||||
ThisExpression(node) {
|
||||
const current = stack.getCurrent();
|
||||
|
||||
if (!insideClassProperty && current && !current.valid) {
|
||||
context.report(node, "Unexpected 'this'.");
|
||||
}
|
||||
}
|
||||
};
|
||||
return !inClassProperty;
|
||||
}
|
||||
};
|
||||
);
|
||||
|
||||
@ -1,296 +1,25 @@
|
||||
/**
|
||||
* @fileoverview Disallows or enforces spaces inside of object literals.
|
||||
* @author Jamund Ferguson
|
||||
* @copyright 2014 Brandyn Bennett. All rights reserved.
|
||||
* @copyright 2014 Michael Ficarra. No rights reserved.
|
||||
* @copyright 2014 Vignesh Anand. All rights reserved.
|
||||
* @copyright 2015 Jamund Ferguson. All rights reserved.
|
||||
* @copyright 2015 Mathieu M-Gosselin. All rights reserved.
|
||||
* @copyright 2015 Toru Nagashima. All rights reserved.
|
||||
* See LICENSE file in root directory for full license.
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
const ruleComposer = require('eslint-rule-composer');
|
||||
const eslint = require('eslint');
|
||||
const objectCurlySpacingRule = new eslint.Linter().getRules().get('object-curly-spacing');
|
||||
|
||||
module.exports = function(context) {
|
||||
var spaced = context.options[0] === "always",
|
||||
sourceCode = context.getSourceCode();
|
||||
|
||||
/**
|
||||
* Determines whether an option is set, relative to the spacing option.
|
||||
* If spaced is "always", then check whether option is set to false.
|
||||
* If spaced is "never", then check whether option is set to true.
|
||||
* @param {Object} option - The option to exclude.
|
||||
* @returns {boolean} Whether or not the property is excluded.
|
||||
*/
|
||||
function isOptionSet(option) {
|
||||
return context.options[1] != null ? context.options[1][option] === !spaced : false;
|
||||
}
|
||||
|
||||
var options = {
|
||||
spaced: spaced,
|
||||
arraysInObjectsException: isOptionSet("arraysInObjects"),
|
||||
objectsInObjectsException: isOptionSet("objectsInObjects")
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Determines whether two adjacent tokens are have whitespace between them.
|
||||
* @param {Object} left - The left token object.
|
||||
* @param {Object} right - The right token object.
|
||||
* @returns {boolean} Whether or not there is space between the tokens.
|
||||
*/
|
||||
function isSpaced(left, right) {
|
||||
return sourceCode.isSpaceBetweenTokens(left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines whether two adjacent tokens are on the same line.
|
||||
* @param {Object} left - The left token object.
|
||||
* @param {Object} right - The right token object.
|
||||
* @returns {boolean} Whether or not the tokens are on the same line.
|
||||
*/
|
||||
function isSameLine(left, right) {
|
||||
return left.loc.start.line === right.loc.start.line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports that there shouldn't be a space after the first token
|
||||
* @param {ASTNode} node - The node to report in the event of an error.
|
||||
* @param {Token} token - The token to use for the report.
|
||||
* @returns {void}
|
||||
*/
|
||||
function reportNoBeginningSpace(node, token) {
|
||||
context.report({
|
||||
node: node,
|
||||
loc: token.loc.end,
|
||||
message: "There should be no space after '" + token.value + "'",
|
||||
fix: function(fixer) {
|
||||
var nextToken = sourceCode.getTokenAfter(token);
|
||||
return fixer.removeRange([token.range[1], nextToken.range[0]]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports that there shouldn't be a space before the last token
|
||||
* @param {ASTNode} node - The node to report in the event of an error.
|
||||
* @param {Token} token - The token to use for the report.
|
||||
* @returns {void}
|
||||
*/
|
||||
function reportNoEndingSpace(node, token) {
|
||||
context.report({
|
||||
node: node,
|
||||
loc: token.loc.start,
|
||||
message: "There should be no space before '" + token.value + "'",
|
||||
fix: function(fixer) {
|
||||
var previousToken = sourceCode.getTokenBefore(token);
|
||||
return fixer.removeRange([previousToken.range[1], token.range[0]]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports that there should be a space after the first token
|
||||
* @param {ASTNode} node - The node to report in the event of an error.
|
||||
* @param {Token} token - The token to use for the report.
|
||||
* @returns {void}
|
||||
*/
|
||||
function reportRequiredBeginningSpace(node, token) {
|
||||
context.report({
|
||||
node: node,
|
||||
loc: token.loc.end,
|
||||
message: "A space is required after '" + token.value + "'",
|
||||
fix: function(fixer) {
|
||||
return fixer.insertTextAfter(token, " ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports that there should be a space before the last token
|
||||
* @param {ASTNode} node - The node to report in the event of an error.
|
||||
* @param {Token} token - The token to use for the report.
|
||||
* @returns {void}
|
||||
*/
|
||||
function reportRequiredEndingSpace(node, token) {
|
||||
context.report({
|
||||
node: node,
|
||||
loc: token.loc.start,
|
||||
message: "A space is required before '" + token.value + "'",
|
||||
fix: function(fixer) {
|
||||
return fixer.insertTextBefore(token, " ");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if spacing in curly braces is valid.
|
||||
* @param {ASTNode} node The AST node to check.
|
||||
* @param {Token} first The first token to check (should be the opening brace)
|
||||
* @param {Token} second The second token to check (should be first after the opening brace)
|
||||
* @param {Token} penultimate The penultimate token to check (should be last before closing brace)
|
||||
* @param {Token} last The last token to check (should be closing brace)
|
||||
* @returns {void}
|
||||
*/
|
||||
function validateBraceSpacing(node, first, second, penultimate, last) {
|
||||
var closingCurlyBraceMustBeSpaced =
|
||||
options.arraysInObjectsException && penultimate.value === "]" ||
|
||||
options.objectsInObjectsException && penultimate.value === "}"
|
||||
? !options.spaced : options.spaced;
|
||||
|
||||
if (isSameLine(first, second)) {
|
||||
if (options.spaced && !isSpaced(first, second)) {
|
||||
reportRequiredBeginningSpace(node, first);
|
||||
}
|
||||
if (!options.spaced && isSpaced(first, second)) {
|
||||
reportNoBeginningSpace(node, first);
|
||||
}
|
||||
}
|
||||
|
||||
if (isSameLine(penultimate, last)) {
|
||||
if (closingCurlyBraceMustBeSpaced && !isSpaced(penultimate, last)) {
|
||||
reportRequiredEndingSpace(node, last);
|
||||
}
|
||||
if (!closingCurlyBraceMustBeSpaced && isSpaced(penultimate, last)) {
|
||||
reportNoEndingSpace(node, last);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a given object node if spacing in curly braces is invalid.
|
||||
* @param {ASTNode} node - An ObjectExpression or ObjectPattern node to check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function checkForObject(node) {
|
||||
if (node.properties.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var firstSpecifier = node.properties[0],
|
||||
lastSpecifier = node.properties[node.properties.length - 1];
|
||||
|
||||
var first = sourceCode.getTokenBefore(firstSpecifier),
|
||||
last = sourceCode.getTokenAfter(lastSpecifier);
|
||||
|
||||
// support trailing commas
|
||||
if (last.value === ",") {
|
||||
last = sourceCode.getTokenAfter(last);
|
||||
}
|
||||
|
||||
var second = sourceCode.getTokenAfter(first),
|
||||
penultimate = sourceCode.getTokenBefore(last);
|
||||
|
||||
validateBraceSpacing(node, first, second, penultimate, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a given import node if spacing in curly braces is invalid.
|
||||
* @param {ASTNode} node - An ImportDeclaration node to check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function checkForImport(node) {
|
||||
if (node.specifiers.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var firstSpecifier = node.specifiers[0],
|
||||
lastSpecifier = node.specifiers[node.specifiers.length - 1];
|
||||
|
||||
if (lastSpecifier.type !== "ImportSpecifier") {
|
||||
return;
|
||||
}
|
||||
if (firstSpecifier.type !== "ImportSpecifier") {
|
||||
firstSpecifier = node.specifiers[1];
|
||||
}
|
||||
|
||||
var first = sourceCode.getTokenBefore(firstSpecifier),
|
||||
last = sourceCode.getTokenAfter(lastSpecifier);
|
||||
|
||||
// to support a trailing comma.
|
||||
if (last.value === ",") {
|
||||
last = sourceCode.getTokenAfter(last);
|
||||
}
|
||||
|
||||
var second = sourceCode.getTokenAfter(first),
|
||||
penultimate = sourceCode.getTokenBefore(last);
|
||||
|
||||
validateBraceSpacing(node, first, second, penultimate, last);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reports a given export node if spacing in curly braces is invalid.
|
||||
* @param {ASTNode} node - An ExportNamedDeclaration node to check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function checkForExport(node) {
|
||||
if (node.specifiers.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
var firstSpecifier = node.specifiers[0],
|
||||
lastSpecifier = node.specifiers[node.specifiers.length - 1],
|
||||
first = sourceCode.getTokenBefore(firstSpecifier),
|
||||
last = sourceCode.getTokenAfter(lastSpecifier);
|
||||
module.exports = ruleComposer.filterReports(
|
||||
objectCurlySpacingRule,
|
||||
(problem, metadata) => {
|
||||
const node = problem.node;
|
||||
|
||||
// Allow `exportNamespaceFrom` and `exportDefaultFrom` syntax:
|
||||
// export * as x from '...';
|
||||
// export x from '...';
|
||||
if (first.value === "export") {
|
||||
return;
|
||||
if (
|
||||
node.type === 'ExportNamedDeclaration' &&
|
||||
node.specifiers.length > 0 &&
|
||||
metadata.sourceCode.getTokenBefore(node.specifiers[0]).value === "export"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// to support a trailing comma.
|
||||
if (last.value === ",") {
|
||||
last = sourceCode.getTokenAfter(last);
|
||||
}
|
||||
|
||||
var second = sourceCode.getTokenAfter(first),
|
||||
penultimate = sourceCode.getTokenBefore(last);
|
||||
|
||||
validateBraceSpacing(node, first, second, penultimate, last);
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Public
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
// var {x} = y;
|
||||
ObjectPattern: checkForObject,
|
||||
|
||||
// var y = {x: 'y'}
|
||||
ObjectExpression: checkForObject,
|
||||
|
||||
// import {y} from 'x';
|
||||
ImportDeclaration: checkForImport,
|
||||
|
||||
// export {name} from 'yo';
|
||||
ExportNamedDeclaration: checkForExport
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
module.exports.schema = [
|
||||
{
|
||||
"enum": ["always", "never"]
|
||||
},
|
||||
{
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"arraysInObjects": {
|
||||
"type": "boolean"
|
||||
},
|
||||
"objectsInObjects": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
}
|
||||
];
|
||||
);
|
||||
|
||||
@ -1,225 +1,115 @@
|
||||
/**
|
||||
* @fileoverview Rule to flag missing semicolons.
|
||||
* @author Nicholas C. Zakas
|
||||
*/
|
||||
"use strict";
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
// Rule Definition
|
||||
//------------------------------------------------------------------------------
|
||||
const ruleComposer = require('eslint-rule-composer');
|
||||
const eslint = require('eslint');
|
||||
const semiRule = new eslint.Linter().getRules().get('semi');
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
description: "require or disallow semicolons instead of ASI",
|
||||
category: "Stylistic Issues",
|
||||
recommended: false
|
||||
},
|
||||
const OPT_OUT_PATTERN = /^[-[(/+`]/; // One of [(/+-`
|
||||
|
||||
fixable: "code",
|
||||
const isSemicolon = token => token.type === "Punctuator" && token.value === ";";
|
||||
|
||||
schema: {
|
||||
anyOf: [
|
||||
{
|
||||
type: "array",
|
||||
items: [
|
||||
{
|
||||
enum: ["never"]
|
||||
}
|
||||
],
|
||||
minItems: 0,
|
||||
maxItems: 1
|
||||
},
|
||||
{
|
||||
type: "array",
|
||||
items: [
|
||||
{
|
||||
enum: ["always"]
|
||||
},
|
||||
{
|
||||
type: "object",
|
||||
properties: {
|
||||
omitLastInOneLineBlock: { type: "boolean" }
|
||||
},
|
||||
additionalProperties: false
|
||||
}
|
||||
],
|
||||
minItems: 0,
|
||||
maxItems: 2
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
const isUnnecessarySemicolon = (context, lastToken) => {
|
||||
if (!isSemicolon(lastToken)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
create(context) {
|
||||
const nextToken = context.getSourceCode().getTokenAfter(lastToken);
|
||||
|
||||
const OPT_OUT_PATTERN = /^[-[(/+`]/; // One of [(/+-`
|
||||
const options = context.options[1];
|
||||
const never = context.options[0] === "never",
|
||||
exceptOneLine = options && options.omitLastInOneLineBlock === true,
|
||||
sourceCode = context.getSourceCode();
|
||||
if (!nextToken) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Helpers
|
||||
//--------------------------------------------------------------------------
|
||||
const lastTokenLine = lastToken.loc.end.line;
|
||||
const nextTokenLine = nextToken.loc.start.line;
|
||||
const isOptOutToken = OPT_OUT_PATTERN.test(nextToken.value) && nextToken.value !== "++" && nextToken.value !== "--";
|
||||
const isDivider = (nextToken.value === "}" || nextToken.value === ";");
|
||||
|
||||
/**
|
||||
* Reports a semicolon error with appropriate location and message.
|
||||
* @param {ASTNode} node The node with an extra or missing semicolon.
|
||||
* @param {boolean} missing True if the semicolon is missing.
|
||||
* @returns {void}
|
||||
*/
|
||||
function report(node, missing) {
|
||||
const lastToken = sourceCode.getLastToken(node);
|
||||
let message,
|
||||
fix,
|
||||
loc = lastToken.loc;
|
||||
return (lastTokenLine !== nextTokenLine && !isOptOutToken) || isDivider;
|
||||
}
|
||||
|
||||
if (!missing) {
|
||||
message = "Missing semicolon.";
|
||||
loc = loc.end;
|
||||
fix = function(fixer) {
|
||||
return fixer.insertTextAfter(lastToken, ";");
|
||||
};
|
||||
} else {
|
||||
message = "Extra semicolon.";
|
||||
loc = loc.start;
|
||||
fix = function(fixer) {
|
||||
return fixer.remove(lastToken);
|
||||
};
|
||||
}
|
||||
const isOneLinerBlock = (context, node) => {
|
||||
const nextToken = context.getSourceCode().getTokenAfter(node);
|
||||
|
||||
context.report({
|
||||
node,
|
||||
loc,
|
||||
message,
|
||||
fix
|
||||
});
|
||||
if (!nextToken || nextToken.value !== "}") {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
const parent = node.parent;
|
||||
|
||||
/**
|
||||
* Checks whether a token is a semicolon punctuator.
|
||||
* @param {Token} token The token.
|
||||
* @returns {boolean} True if token is a semicolon punctuator.
|
||||
*/
|
||||
function isSemicolon(token) {
|
||||
return (token.type === "Punctuator" && token.value === ";");
|
||||
}
|
||||
return parent && parent.type === "BlockStatement" &&
|
||||
parent.loc.start.line === parent.loc.end.line;
|
||||
};
|
||||
|
||||
/**
|
||||
* Check if a semicolon is unnecessary, only true if:
|
||||
* - next token is on a new line and is not one of the opt-out tokens
|
||||
* - next token is a valid statement divider
|
||||
* @param {Token} lastToken last token of current node.
|
||||
* @returns {boolean} whether the semicolon is unnecessary.
|
||||
*/
|
||||
function isUnnecessarySemicolon(lastToken) {
|
||||
if (!isSemicolon(lastToken)) {
|
||||
return false;
|
||||
}
|
||||
const report = (context, node, missing) => {
|
||||
const lastToken = context.getSourceCode().getLastToken(node);
|
||||
|
||||
const nextToken = sourceCode.getTokenAfter(lastToken);
|
||||
let message, fix, loc = lastToken.loc;
|
||||
|
||||
if (!nextToken) {
|
||||
return true;
|
||||
}
|
||||
if (!missing) {
|
||||
message = "Missing semicolon.";
|
||||
loc = loc.end;
|
||||
fix = function(fixer) {
|
||||
return fixer.insertTextAfter(lastToken, ";");
|
||||
};
|
||||
} else {
|
||||
message = "Extra semicolon.";
|
||||
loc = loc.start;
|
||||
fix = function(fixer) {
|
||||
return fixer.remove(lastToken);
|
||||
};
|
||||
}
|
||||
|
||||
const lastTokenLine = lastToken.loc.end.line;
|
||||
const nextTokenLine = nextToken.loc.start.line;
|
||||
const isOptOutToken = OPT_OUT_PATTERN.test(nextToken.value) && nextToken.value !== "++" && nextToken.value !== "--";
|
||||
const isDivider = (nextToken.value === "}" || nextToken.value === ";");
|
||||
context.report({
|
||||
node,
|
||||
loc,
|
||||
message,
|
||||
fix
|
||||
});
|
||||
};
|
||||
|
||||
return (lastTokenLine !== nextTokenLine && !isOptOutToken) || isDivider;
|
||||
}
|
||||
const semiRuleWithClassProperty = ruleComposer.joinReports([
|
||||
semiRule,
|
||||
context => ({
|
||||
ClassProperty(node) {
|
||||
const options = context.options[1];
|
||||
const exceptOneLine = options && options.omitLastInOneLineBlock === true;
|
||||
|
||||
/**
|
||||
* Checks a node to see if it's in a one-liner block statement.
|
||||
* @param {ASTNode} node The node to check.
|
||||
* @returns {boolean} whether the node is in a one-liner block statement.
|
||||
*/
|
||||
function isOneLinerBlock(node) {
|
||||
const nextToken = sourceCode.getTokenAfter(node);
|
||||
|
||||
if (!nextToken || nextToken.value !== "}") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const parent = node.parent;
|
||||
|
||||
return parent && parent.type === "BlockStatement" &&
|
||||
parent.loc.start.line === parent.loc.end.line;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks a node to see if it's followed by a semicolon.
|
||||
* @param {ASTNode} node The node to check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function checkForSemicolon(node) {
|
||||
const sourceCode = context.getSourceCode();
|
||||
const lastToken = sourceCode.getLastToken(node);
|
||||
|
||||
if (never) {
|
||||
if (isUnnecessarySemicolon(lastToken)) {
|
||||
report(node, true);
|
||||
if (context.options[0] === "never") {
|
||||
if (isUnnecessarySemicolon(context, lastToken)) {
|
||||
report(context, node, true);
|
||||
}
|
||||
} else {
|
||||
if (!isSemicolon(lastToken)) {
|
||||
if (!exceptOneLine || !isOneLinerBlock(node)) {
|
||||
report(node);
|
||||
report(context, node);
|
||||
}
|
||||
} else {
|
||||
if (exceptOneLine && isOneLinerBlock(node)) {
|
||||
report(node, true);
|
||||
report(context, node, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
module.exports = ruleComposer.filterReports(
|
||||
semiRuleWithClassProperty,
|
||||
(problem, metadata) => {
|
||||
const node = problem.node;
|
||||
|
||||
// Handle async iterator:
|
||||
// for await (let something of {})
|
||||
if (
|
||||
node.type === "VariableDeclaration" &&
|
||||
node.parent.type === "ForAwaitStatement"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks to see if there's a semicolon after a variable declaration.
|
||||
* @param {ASTNode} node The node to check.
|
||||
* @returns {void}
|
||||
*/
|
||||
function checkForSemicolonForVariableDeclaration(node) {
|
||||
const ancestors = context.getAncestors(),
|
||||
parentIndex = ancestors.length - 1,
|
||||
parent = ancestors[parentIndex];
|
||||
|
||||
if ((parent.type !== "ForStatement" || parent.init !== node) &&
|
||||
(!/^For(?:In|Of|Await)Statement/.test(parent.type) || parent.left !== node)
|
||||
) {
|
||||
checkForSemicolon(node);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Public API
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
return {
|
||||
VariableDeclaration: checkForSemicolonForVariableDeclaration,
|
||||
ExpressionStatement: checkForSemicolon,
|
||||
ReturnStatement: checkForSemicolon,
|
||||
ThrowStatement: checkForSemicolon,
|
||||
DoWhileStatement: checkForSemicolon,
|
||||
DebuggerStatement: checkForSemicolon,
|
||||
BreakStatement: checkForSemicolon,
|
||||
ContinueStatement: checkForSemicolon,
|
||||
ImportDeclaration: checkForSemicolon,
|
||||
ExportAllDeclaration: checkForSemicolon,
|
||||
ClassProperty: checkForSemicolon,
|
||||
ExportNamedDeclaration(node) {
|
||||
if (!node.declaration) {
|
||||
checkForSemicolon(node);
|
||||
}
|
||||
},
|
||||
ExportDefaultDeclaration(node) {
|
||||
if (!/(?:Class|Function)Declaration/.test(node.declaration.type)) {
|
||||
checkForSemicolon(node);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
);
|
||||
|
||||
@ -78,6 +78,9 @@ function extractPatterns(patterns, type) {
|
||||
thisPattern.code += " /* should error */";
|
||||
}
|
||||
|
||||
delete thisPattern.invalid;
|
||||
delete thisPattern.valid;
|
||||
|
||||
return thisPattern;
|
||||
});
|
||||
});
|
||||
|
||||
@ -161,13 +161,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 9
|
||||
column: 8
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 12
|
||||
@ -183,7 +183,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 18
|
||||
@ -199,13 +199,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 9
|
||||
column: 8
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 17
|
||||
@ -221,7 +221,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 13
|
||||
@ -237,7 +237,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 16
|
||||
@ -254,7 +254,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 21
|
||||
@ -271,13 +271,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 15
|
||||
@ -294,13 +294,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 20
|
||||
@ -316,13 +316,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 9
|
||||
column: 8
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 13
|
||||
@ -339,13 +339,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 9
|
||||
column: 8
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ImportDeclaration",
|
||||
line: 1,
|
||||
column: 15
|
||||
@ -361,13 +361,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ExportNamedDeclaration",
|
||||
line: 1,
|
||||
column: 9
|
||||
column: 8
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ExportNamedDeclaration",
|
||||
line: 1,
|
||||
column: 12
|
||||
@ -382,7 +382,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always", {"arraysInObjects": false}],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression"
|
||||
}
|
||||
]
|
||||
@ -393,7 +393,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always", {"arraysInObjects": false}],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression"
|
||||
}
|
||||
]
|
||||
@ -406,7 +406,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always", {"objectsInObjects": false}],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 43
|
||||
@ -419,7 +419,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always", {"objectsInObjects": false}],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 61
|
||||
@ -435,7 +435,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
ecmaFeatures: { destructuring: true },
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 9
|
||||
@ -449,7 +449,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
ecmaFeatures: { destructuring: true },
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 9
|
||||
@ -463,7 +463,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
ecmaFeatures: { destructuring: true },
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 10
|
||||
@ -477,13 +477,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
ecmaFeatures: { destructuring: true },
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 6
|
||||
column: 5
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 11
|
||||
@ -498,7 +498,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never", {"objectsInObjects": true}],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 39
|
||||
@ -511,7 +511,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never", {"objectsInObjects": true}],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 55
|
||||
@ -526,13 +526,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 30
|
||||
@ -545,10 +545,10 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -558,7 +558,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 31
|
||||
@ -571,13 +571,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 32
|
||||
@ -590,7 +590,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 31
|
||||
@ -603,10 +603,10 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -616,16 +616,16 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 12
|
||||
column: 11
|
||||
},
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 19
|
||||
column: 18
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -635,13 +635,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 28
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 40
|
||||
@ -658,10 +658,10 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectExpression",
|
||||
line: 1,
|
||||
column: 23
|
||||
column: 22
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -674,13 +674,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 6
|
||||
column: 5
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 10
|
||||
@ -694,7 +694,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 11
|
||||
@ -708,13 +708,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 6
|
||||
column: 5
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 12
|
||||
@ -728,7 +728,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never"],
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 11
|
||||
@ -742,7 +742,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 11
|
||||
@ -756,10 +756,10 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["always"],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 6
|
||||
column: 5
|
||||
}
|
||||
]
|
||||
},
|
||||
@ -771,7 +771,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never", {"arraysInObjects": true}],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression"
|
||||
}
|
||||
]
|
||||
@ -782,7 +782,7 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
options: ["never", {"arraysInObjects": true}],
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectExpression"
|
||||
}
|
||||
]
|
||||
@ -801,13 +801,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "A space is required after '{'",
|
||||
message: "A space is required after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 14
|
||||
column: 13
|
||||
},
|
||||
{
|
||||
message: "A space is required before '}'",
|
||||
message: "A space is required before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 17
|
||||
@ -826,13 +826,13 @@ ruleTester.run('babel/object-curly-spacing', rule, {
|
||||
},
|
||||
errors: [
|
||||
{
|
||||
message: "There should be no space after '{'",
|
||||
message: "There should be no space after '{'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 14
|
||||
column: 13
|
||||
},
|
||||
{
|
||||
message: "There should be no space before '}'",
|
||||
message: "There should be no space before '}'.",
|
||||
type: "ObjectPattern",
|
||||
line: 1,
|
||||
column: 19
|
||||
|
||||
1023
eslint/babel-eslint-plugin/yarn.lock
Normal file
1023
eslint/babel-eslint-plugin/yarn.lock
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user