forked from azat-io/eslint-plugin-perfectionist
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: copy utils from eslint-plugin-unicorn
- Loading branch information
Showing
94 changed files
with
4,085 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
|
||
/** | ||
@typedef { | ||
{ | ||
name?: string, | ||
names?: string[], | ||
argumentsLength?: number, | ||
minimumArguments?: number, | ||
maximumArguments?: number, | ||
allowSpreadElement?: boolean, | ||
optional?: boolean, | ||
} | string | string[] | ||
} CallOrNewExpressionCheckOptions | ||
*/ | ||
function create(node, options, types) { | ||
if (!types.includes(node?.type)) { | ||
return false; | ||
} | ||
|
||
if (typeof options === 'string') { | ||
options = {names: [options]}; | ||
} | ||
|
||
if (Array.isArray(options)) { | ||
options = {names: options}; | ||
} | ||
|
||
let { | ||
name, | ||
names, | ||
argumentsLength, | ||
minimumArguments, | ||
maximumArguments, | ||
allowSpreadElement, | ||
optional, | ||
} = { | ||
minimumArguments: 0, | ||
maximumArguments: Number.POSITIVE_INFINITY, | ||
allowSpreadElement: false, | ||
...options, | ||
}; | ||
|
||
if (name) { | ||
names = [name]; | ||
} | ||
|
||
if ( | ||
(optional === true && (node.optional !== optional)) | ||
|| ( | ||
optional === false | ||
// `node.optional` can be `undefined` in some parsers | ||
&& node.optional | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if (typeof argumentsLength === 'number' && node.arguments.length !== argumentsLength) { | ||
return false; | ||
} | ||
|
||
if (minimumArguments !== 0 && node.arguments.length < minimumArguments) { | ||
return false; | ||
} | ||
|
||
if (Number.isFinite(maximumArguments) && node.arguments.length > maximumArguments) { | ||
return false; | ||
} | ||
|
||
if (!allowSpreadElement) { | ||
const maximumArgumentsLength = Number.isFinite(maximumArguments) ? maximumArguments : argumentsLength; | ||
if ( | ||
typeof maximumArgumentsLength === 'number' | ||
&& node.arguments.some( | ||
(node, index) => | ||
node.type === 'SpreadElement' | ||
&& index < maximumArgumentsLength, | ||
) | ||
) { | ||
return false; | ||
} | ||
} | ||
|
||
if ( | ||
Array.isArray(names) | ||
&& names.length > 0 | ||
&& ( | ||
node.callee.type !== 'Identifier' | ||
|| !names.includes(node.callee.name) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
@param {CallOrNewExpressionCheckOptions} [options] | ||
@returns {boolean} | ||
*/ | ||
export const isCallExpression = (node, options) => create(node, options, ['CallExpression']); | ||
|
||
/** | ||
@param {CallOrNewExpressionCheckOptions} [options] | ||
@returns {boolean} | ||
*/ | ||
export const isNewExpression = (node, options) => { | ||
if (typeof options?.optional === 'boolean') { | ||
throw new TypeError('Cannot check node.optional in `isNewExpression`.'); | ||
} | ||
|
||
return create(node, options, ['NewExpression']); | ||
}; | ||
|
||
/** | ||
@param {CallOrNewExpressionCheckOptions} [options] | ||
@returns {boolean} | ||
*/ | ||
export const isCallOrNewExpression = (node, options) => create(node, options, ['CallExpression', 'NewExpression']); | ||
|
||
export default { | ||
isCallExpression, | ||
isNewExpression, | ||
isCallOrNewExpression, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
const functionTypes = [ | ||
'FunctionDeclaration', | ||
'FunctionExpression', | ||
'ArrowFunctionExpression', | ||
] | ||
|
||
export default functionTypes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
'use strict' | ||
|
||
|
||
|
||
|
||
import isEmptyNode from './is-empty-node.js'; | ||
import isExpressionStatement from './is-expression-statement.js'; | ||
import isFunction from './is-function.js'; | ||
import isMemberExpression from './is-member-expression.js'; | ||
import isMethodCall from './is-method-call.js'; | ||
import isReferenceIdentifier from './is-reference-identifier.js'; | ||
import isStaticRequire from './is-static-require.js'; | ||
import isUndefined from './is-undefined.js'; | ||
import functionTypes from './function-types.js'; | ||
|
||
export { | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
isEmptyNode, | ||
isExpressionStatement, | ||
isFunction, | ||
isMemberExpression, | ||
isMethodCall, | ||
|
||
isReferenceIdentifier, | ||
isStaticRequire, | ||
isUndefined, | ||
|
||
functionTypes, | ||
}; | ||
|
||
export {default as isArrowFunctionBody} from './is-arrow-function-body.js'; | ||
export {isBigIntLiteral, isLiteral, isNumberLiteral, isNullLiteral, isStringLiteral, isRegexLiteral} from './literal.js' | ||
export { isCallExpression, isCallOrNewExpression, isNewExpression } from './call-or-new-expression.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
'use strict' | ||
|
||
function isArrowFunctionBody(node) { | ||
return ( | ||
node.parent.type === 'ArrowFunctionExpression' && node.parent.body === node | ||
) | ||
} | ||
|
||
export default isArrowFunctionBody |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
'use strict' | ||
function isEmptyNode(node, additionalEmpty) { | ||
const { type } = node | ||
|
||
if (type === 'BlockStatement') { | ||
return node.body.every(currentNode => | ||
isEmptyNode(currentNode, additionalEmpty), | ||
) | ||
} | ||
|
||
if (type === 'EmptyStatement') { | ||
return true | ||
} | ||
|
||
if (additionalEmpty?.(node)) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
export default isEmptyNode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
'use strict' | ||
|
||
function isExpressionStatement(node) { | ||
return ( | ||
node.type === 'ExpressionStatement' || | ||
(node.type === 'ChainExpression' && | ||
node.parent.type === 'ExpressionStatement') | ||
) | ||
} | ||
|
||
export default isExpressionStatement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict' | ||
const functionTypes = require('./function-types.js') | ||
|
||
function isFunction(node) { | ||
return functionTypes.includes(node.type) | ||
} | ||
|
||
export default isFunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* eslint-disable */ | ||
// @ts-nocheck | ||
/** | ||
@param { | ||
{ | ||
property?: string, | ||
properties?: string[], | ||
object?: string, | ||
objects?: string[], | ||
optional?: boolean, | ||
computed?: boolean | ||
} | string | string[] | ||
} [options] | ||
@returns {string} | ||
*/ | ||
export const isMemberExpression: any = (node, options) => { | ||
if (node?.type !== 'MemberExpression') { | ||
return false; | ||
} | ||
|
||
if (typeof options === 'string') { | ||
options = {properties: [options]}; | ||
} | ||
|
||
if (Array.isArray(options)) { | ||
options = {properties: options}; | ||
} | ||
|
||
let { | ||
property, | ||
properties, | ||
object, | ||
objects, | ||
optional, | ||
computed, | ||
} = { | ||
property: '', | ||
properties: [], | ||
object: '', | ||
...options, | ||
}; | ||
|
||
if (property) { | ||
properties = [property]; | ||
} | ||
|
||
if (object) { | ||
objects = [object]; | ||
} | ||
|
||
if ( | ||
(optional === true && (node.optional !== optional)) | ||
|| ( | ||
optional === false | ||
// `node.optional` can be `undefined` in some parsers | ||
&& node.optional | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
Array.isArray(properties) | ||
&& properties.length > 0 | ||
) { | ||
if ( | ||
node.property.type !== 'Identifier' | ||
|| !properties.includes(node.property.name) | ||
) { | ||
return false; | ||
} | ||
|
||
computed ??= false; | ||
} | ||
|
||
if ( | ||
(computed === true && (node.computed !== computed)) | ||
|| ( | ||
computed === false | ||
// `node.computed` can be `undefined` in some parsers | ||
&& node.computed | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
if ( | ||
Array.isArray(objects) | ||
&& objects.length > 0 | ||
&& ( | ||
node.object.type !== 'Identifier' | ||
|| !objects.includes(node.object.name) | ||
) | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
export default isMemberExpression; |
Oops, something went wrong.