elearning/Frontend-Learner/node_modules/eslint-plugin-unicorn/rules/no-magic-array-flat-depth.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2026-01-13 10:46:40 +07:00
import {isMethodCall, isNumericLiteral} from './ast/index.js';
import {getCallExpressionTokens} from './utils/index.js';
const MESSAGE_ID = 'no-magic-array-flat-depth';
const messages = {
[MESSAGE_ID]: 'Magic number as depth is not allowed.',
};
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => ({
CallExpression(callExpression) {
if (!isMethodCall(callExpression, {
method: 'flat',
argumentsLength: 1,
optionalCall: false,
})) {
return;
}
const [depth] = callExpression.arguments;
if (!isNumericLiteral(depth) || depth.value === 1) {
return;
}
const {sourceCode} = context;
const {
openingParenthesisToken,
closingParenthesisToken,
} = getCallExpressionTokens(callExpression, context);
if (sourceCode.commentsExistBetween(openingParenthesisToken, closingParenthesisToken)) {
return;
}
return {
node: depth,
messageId: MESSAGE_ID,
};
},
});
/** @type {import('eslint').Rule.RuleModule} */
const config = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow a magic number as the `depth` argument in `Array#flat(…).`',
recommended: 'unopinionated',
},
messages,
},
};
export default config;