β¬ οΈ Back to Table of Contents
π no-array-constructor¶
π Analysis Summary¶
| Metric | Count |
|---|---|
| π§ Functions | 3 |
| π¦ Imports | 5 |
π Table of Contents¶
π οΈ File Location:¶
π packages/eslint-plugin/src/rules/no-array-constructor.ts
π€ Default Export¶
| Property | Value |
|---|---|
name |
'no-array-constructor' |
meta.type |
'suggestion' |
meta.docs.description |
'Disallow generic Array constructors' |
meta.docs.extendsBaseRule |
true |
meta.docs.recommended |
'recommended' |
meta.fixable |
'code' |
meta.messages.useLiteral |
'The array literal notation [] is preferable.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create β documented under Functions.
π¦ Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
isOpeningParenToken |
@typescript-eslint/utils/ast-utils |
isClosingParenToken |
@typescript-eslint/utils/ast-utils |
createRule |
../util |
Functions¶
create(context: any): { CallExpression: (node: TSESTree.CallExpression | TSESTree⦶
Parameters:
contextany
Returns: { CallExpression: (node: TSESTree.CallExpression | TSESTree.NewExpression) => void; NewExpression: (node: TSESTree.CallExpression | TSESTree.NewExpression) => void; }
Calls:
sourceCode.getLastTokenisClosingParenToken (from @typescript-eslint/utils/ast-utils)sourceCode.getTokenAfterisOpeningParenToken (from @typescript-eslint/utils/ast-utils)sourceCode.text.slicecontext.reportgetArgumentsTextfixer.replaceText
Internal Comments:
/**
* Disallow construction of dense arrays using the Array constructor
* @param node node to evaluate
*/
Code
create(context) {
const sourceCode = context.sourceCode;
function getArgumentsText(
node: TSESTree.CallExpression | TSESTree.NewExpression,
) {
const lastToken = sourceCode.getLastToken(node);
if (lastToken == null || !isClosingParenToken(lastToken)) {
return '';
}
let firstToken: TSESTree.Expression | TSESTree.Token | null = node.callee;
do {
firstToken = sourceCode.getTokenAfter(firstToken);
if (!firstToken || firstToken === lastToken) {
return '';
}
} while (!isOpeningParenToken(firstToken));
return sourceCode.text.slice(firstToken.range[1], lastToken.range[0]);
}
/**
* Disallow construction of dense arrays using the Array constructor
* @param node node to evaluate
*/
function check(
node: TSESTree.CallExpression | TSESTree.NewExpression,
): void {
if (
node.arguments.length !== 1 &&
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'Array' &&
!node.typeArguments
) {
context.report({
node,
messageId: 'useLiteral',
fix(fixer) {
const argsText = getArgumentsText(node);
return fixer.replaceText(node, `[${argsText}]`);
},
});
}
}
return {
CallExpression: check,
NewExpression: check,
};
}
Internal helpers¶
Declared inside another function in this file.
getArgumentsText(node: TSESTree.CallExpression | TSESTree.NewEβ¦): any¶
Parameters:
nodeTSESTree.CallExpression | TSESTree.NewExpression
Returns: any
Calls:
sourceCode.getLastTokenisClosingParenToken (from @typescript-eslint/utils/ast-utils)sourceCode.getTokenAfterisOpeningParenToken (from @typescript-eslint/utils/ast-utils)sourceCode.text.slice
Code
function getArgumentsText(
node: TSESTree.CallExpression | TSESTree.NewExpression,
) {
const lastToken = sourceCode.getLastToken(node);
if (lastToken == null || !isClosingParenToken(lastToken)) {
return '';
}
let firstToken: TSESTree.Expression | TSESTree.Token | null = node.callee;
do {
firstToken = sourceCode.getTokenAfter(firstToken);
if (!firstToken || firstToken === lastToken) {
return '';
}
} while (!isOpeningParenToken(firstToken));
return sourceCode.text.slice(firstToken.range[1], lastToken.range[0]);
}
check(node: TSESTree.CallExpression | TSESTree.NewEβ¦): void¶
Disallow construction of dense arrays using the Array constructor
Parameters:
nodeany: node to evaluate
Raw JSDoc
Calls:
context.reportgetArgumentsTextfixer.replaceText
Code
function check(
node: TSESTree.CallExpression | TSESTree.NewExpression,
): void {
if (
node.arguments.length !== 1 &&
node.callee.type === AST_NODE_TYPES.Identifier &&
node.callee.name === 'Array' &&
!node.typeArguments
) {
context.report({
node,
messageId: 'useLiteral',
fix(fixer) {
const argsText = getArgumentsText(node);
return fixer.replaceText(node, `[${argsText}]`);
},
});
}
}
Generated by Syntax Scribe