Skip to content

Commit

Permalink
feat: copy utils from eslint-plugin-unicorn
Browse files Browse the repository at this point in the history
  • Loading branch information
lzear committed Nov 4, 2023
1 parent c16cc8c commit 6aa0497
Show file tree
Hide file tree
Showing 94 changed files with 4,085 additions and 0 deletions.
128 changes: 128 additions & 0 deletions rules/ast/call-or-new-expression.ts
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,
};
9 changes: 9 additions & 0 deletions rules/ast/function-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict'

const functionTypes = [
'FunctionDeclaration',
'FunctionExpression',
'ArrowFunctionExpression',
]

export default functionTypes
42 changes: 42 additions & 0 deletions rules/ast/index.ts
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'
9 changes: 9 additions & 0 deletions rules/ast/is-arrow-function-body.ts
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
22 changes: 22 additions & 0 deletions rules/ast/is-empty-node.ts
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
11 changes: 11 additions & 0 deletions rules/ast/is-expression-statement.ts
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
8 changes: 8 additions & 0 deletions rules/ast/is-function.ts
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
101 changes: 101 additions & 0 deletions rules/ast/is-member-expression.ts
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;
Loading

0 comments on commit 6aa0497

Please sign in to comment.