repo_name
string
dataset
string
owner
string
lang
string
func_name
string
code
string
docstring
string
url
string
sha
string
eslint-stylistic
github_2023
eslint-stylistic
typescript
getCommentLineNums
function getCommentLineNums(comments: Token[]) { const lines: number[] = [] comments.forEach((token) => { const start = token.loc.start.line const end = token.loc.end.line lines.push(start, end) }) return lines }
/** * Return an array with any line numbers that contain comments. * @param comments An array of comment tokens. * @returns An array of line numbers. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L30-L40
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
codeAroundComment
function codeAroundComment(token: Token) { let currentToken: Token | null = token do currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true }) while (currentToken && isCommentToken(currentToken)) if (currentToken && isTokenOnSameLine(currentToken, token)) ...
/** * Returns whether or not comments are on lines starting with or ending with code * @param token The comment token to check. * @returns True if the comment is not alone. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L143-L162
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isParentNodeType
function isParentNodeType<T extends NodeTypes>( parent: ASTNode, nodeType: T, ): parent is Extract<ASTNode, { type: T }> { return parent.type === nodeType }
/** * Returns whether or not comments are inside a node type or not. * @param parent The Comment parent node. * @param nodeType The parent type to check against. * @returns True if the comment is inside nodeType. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L170-L175
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getParentNodeOfToken
function getParentNodeOfToken(token: Token): ASTNode | null { const node = sourceCode.getNodeByRangeIndex(token.range[0]) /** * For the purpose of this rule, the comment token is in a `StaticBlock` node only * if it's inside the braces of that `StaticBlock` node. * *...
/** * Returns the parent node that contains the given token. * @param token The token to check. * @returns The parent node that contains the given token. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L182-L213
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtParentStart
function isCommentAtParentStart(token: Token, nodeType: NodeTypes) { const parent = getParentNodeOfToken(token) if (parent && isParentNodeType(parent, nodeType)) { let parentStartNodeOrToken: Token | ASTNode | null = parent if (parent.type === 'StaticBlock') { parentStartNodeOrTo...
/** * Returns whether or not comments are at the parent start or not. * @param token The Comment token. * @param nodeType The parent type to check against. * @returns True if the comment is at parent start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L221-L240
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtParentEnd
function isCommentAtParentEnd(token: Token, nodeType: NodeTypes) { const parent = getParentNodeOfToken(token) return !!parent && isParentNodeType(parent, nodeType) && parent.loc.end.line - token.loc.end.line === 1 }
/** * Returns whether or not comments are at the parent end or not. * @param token The Comment token. * @param nodeType The parent type to check against. * @returns True if the comment is at parent end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L248-L253
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtBlockStart
function isCommentAtBlockStart(token: Token) { return ( isCommentAtParentStart(token, 'ClassBody') || isCommentAtParentStart(token, 'BlockStatement') || isCommentAtParentStart(token, 'StaticBlock') || isCommentAtParentStart(token, 'SwitchCase') || isCommentAtParentStart(tok...
/** * Returns whether or not comments are at the block start or not. * @param token The Comment token. * @returns True if the comment is at block start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L260-L268
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtBlockEnd
function isCommentAtBlockEnd(token: Token) { return ( isCommentAtParentEnd(token, 'ClassBody') || isCommentAtParentEnd(token, 'BlockStatement') || isCommentAtParentEnd(token, 'StaticBlock') || isCommentAtParentEnd(token, 'SwitchCase') || isCommentAtParentEnd(token, 'SwitchS...
/** * Returns whether or not comments are at the block end or not. * @param token The Comment token. * @returns True if the comment is at block end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L275-L283
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtClassStart
function isCommentAtClassStart(token: Token) { return isCommentAtParentStart(token, 'ClassBody') }
/** * Returns whether or not comments are at the class start or not. * @param token The Comment token. * @returns True if the comment is at class start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L290-L292
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtClassEnd
function isCommentAtClassEnd(token: Token) { return isCommentAtParentEnd(token, 'ClassBody') }
/** * Returns whether or not comments are at the class end or not. * @param token The Comment token. * @returns True if the comment is at class end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L299-L301
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtObjectStart
function isCommentAtObjectStart(token: Token) { return isCommentAtParentStart(token, 'ObjectExpression') || isCommentAtParentStart(token, 'ObjectPattern') }
/** * Returns whether or not comments are at the object start or not. * @param token The Comment token. * @returns True if the comment is at object start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L308-L311
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtObjectEnd
function isCommentAtObjectEnd(token: Token) { return isCommentAtParentEnd(token, 'ObjectExpression') || isCommentAtParentEnd(token, 'ObjectPattern') }
/** * Returns whether or not comments are at the object end or not. * @param token The Comment token. * @returns True if the comment is at object end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L318-L321
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtArrayStart
function isCommentAtArrayStart(token: Token) { return isCommentAtParentStart(token, 'ArrayExpression') || isCommentAtParentStart(token, 'ArrayPattern') }
/** * Returns whether or not comments are at the array start or not. * @param token The Comment token. * @returns True if the comment is at array start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L328-L331
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtArrayEnd
function isCommentAtArrayEnd(token: Token) { return isCommentAtParentEnd(token, 'ArrayExpression') || isCommentAtParentEnd(token, 'ArrayPattern') }
/** * Returns whether or not comments are at the array end or not. * @param token The Comment token. * @returns True if the comment is at array end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L338-L341
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkForEmptyLine
function checkForEmptyLine(token: Token, opts: BeforAndAfter) { if (applyDefaultIgnorePatterns && defaultIgnoreRegExp.test(token.value)) return if (customIgnoreRegExp && customIgnoreRegExp.test(token.value)) return let after = opts.after let before = opts.before const pr...
/** * Checks if a comment token has lines around it (ignores inline comments) * @param token The Comment token. * @param opts Options to determine the newline. * @param opts.after Should have a newline after this line. * @param opts.before Should have a newline before this line. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._js_.ts#L355-L423
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getEmptyLineNums
function getEmptyLineNums(lines: string[]): number[] { const emptyLines = lines .map((line, i) => ({ code: line.trim(), num: i + 1, })) .filter(line => !line.code) .map(line => line.num) return emptyLines }
/** * @returns an array with with any line numbers that are empty. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L16-L26
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getCommentLineNums
function getCommentLineNums(comments: Tree.Comment[]): number[] { const lines: number[] = [] comments.forEach((token) => { const start = token.loc.start.line const end = token.loc.end.line lines.push(start, end) }) return lines }
/** * @returns an array with with any line numbers that contain comments. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L31-L41
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
codeAroundComment
function codeAroundComment(token: Tree.Token): boolean { let currentToken: Tree.Token | null = token do { currentToken = sourceCode.getTokenBefore(currentToken, { includeComments: true, }) } while (currentToken && isCommentToken(currentToken)) if (currentToken && isTo...
/** * @returns whether comments are on lines starting with or ending with code. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L159-L182
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isParentNodeType
function isParentNodeType<T extends Tree.AST_NODE_TYPES>( parent: ASTNode, nodeType: T, ): parent is Extract<ASTNode, { type: T }> { return parent.type === nodeType }
/** * @returns whether comments are inside a node type. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L187-L192
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getParentNodeOfToken
function getParentNodeOfToken(token: Tree.Token): ASTNode | null { const node = sourceCode.getNodeByRangeIndex(token.range[0]) return node }
/** * @returns the parent node that contains the given token. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L197-L201
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtParentStart
function isCommentAtParentStart( token: Tree.Token, nodeType: Tree.AST_NODE_TYPES, ): boolean { const parent = getParentNodeOfToken(token) if (parent && isParentNodeType(parent, nodeType)) { const parentStartNodeOrToken = parent return ( token.loc.start.line - par...
/** * @returns whether comments are at the parent start. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L206-L221
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCommentAtParentEnd
function isCommentAtParentEnd( token: Tree.Token, nodeType: Tree.AST_NODE_TYPES, ): boolean { const parent = getParentNodeOfToken(token) return ( !!parent && isParentNodeType(parent, nodeType) && parent.loc.end.line - token.loc.end.line === 1 ) }
/** * @returns whether comments are at the parent end. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L226-L237
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
customReport
const customReport: typeof context.report = (descriptor) => { if ('node' in descriptor) { if ( descriptor.node.type === AST_TOKEN_TYPES.Line || descriptor.node.type === AST_TOKEN_TYPES.Block ) { if (isCommentNearTSConstruct(descriptor.node)) return ...
/** * A custom report function for the baseRule to ignore false positive errors * caused by TS-specific codes */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-around-comment/lines-around-comment._ts_.ts#L391-L402
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getBoundaryTokens
function getBoundaryTokens(curNode: ASTNode, nextNode: ASTNode) { const lastToken = sourceCode.getLastToken(curNode)! const prevToken = sourceCode.getTokenBefore(lastToken) const nextToken = sourceCode.getFirstToken(nextNode) // skip possible lone `;` between nodes const isSemicolonLessStyle = ...
/** * Gets a pair of tokens that should be used to check lines between two class member nodes. * * In most cases, this returns the very last token of the current node and * the very first token of the next node. * For example: * * class C { * x = 1; // curLast: `;` ...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L127-L141
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
findLastConsecutiveTokenAfter
function findLastConsecutiveTokenAfter(prevLastToken: Token, nextFirstToken: Token, maxLine: number): Token { const after = sourceCode.getTokenAfter(prevLastToken, { includeComments: true })! if (after !== nextFirstToken && after.loc.start.line - prevLastToken.loc.end.line <= maxLine) return findLa...
/** * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member. * @param prevLastToken The last token in the previous member node. * @param nextFirstToken The first token in the next member node. * @param maxLine...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L150-L157
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
findFirstConsecutiveTokenBefore
function findFirstConsecutiveTokenBefore(nextFirstToken: Token, prevLastToken: Token, maxLine: number): Token { const before = sourceCode.getTokenBefore(nextFirstToken, { includeComments: true })! if (before !== prevLastToken && nextFirstToken.loc.start.line - before.loc.end.line <= maxLine) return...
/** * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member. * @param nextFirstToken The first token in the next member node. * @param prevLastToken The last token in the previous member node. * @param maxL...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L166-L173
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
hasTokenOrCommentBetween
function hasTokenOrCommentBetween(before: Token, after: Token): boolean { return sourceCode.getTokensBetween(before, after, { includeComments: true }).length !== 0 }
/** * Checks if there is a token or comment between two tokens. * @param before The token before. * @param after The token after. * @returns True if there is a token or comment between two tokens. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L181-L183
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
match
function match(node: ASTNode, type: keyof typeof ClassMemberTypes): boolean { return ClassMemberTypes[type].test(node) }
/** * Checks whether the given node matches the given type. * @param node The class member node to check. * @param type The class member type to check. * @returns `true` if the class member node matched the type. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L192-L194
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getPaddingType
function getPaddingType(prevNode: ASTNode, nextNode: ASTNode) { for (let i = configureList.length - 1; i >= 0; --i) { const configure = configureList[i] const matched = match(prevNode, configure.prev) && match(nextNode, configure.next) if (matched...
/** * Finds the last matched configuration from the configureList. * @param prevNode The previous node to match. * @param nextNode The current node to match. * @returns Padding type or `null` if no matches were found. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/lines-between-class-members/lines-between-class-members._js_.ts#L203-L214
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
computeLineLength
function computeLineLength(line: string, tabWidth: number): number { let extraCharacterCount = 0 line.replace(/\t/gu, (_, offset) => { const totalOffset = offset + extraCharacterCount const previousTabStopOffset = tabWidth ? totalOffset % tabWidth : 0 const spaceCount = tabWidth - p...
/** * Computes the length of a line that may contain tabs. The width of each * tab will be the number of spaces to the next tab stop. * @param line The line. * @param tabWidth The width of each tab stop in spaces. * @returns The computed line length. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L102-L114
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isTrailingComment
function isTrailingComment(line: string, lineNumber: number, comment: ASTNode): boolean { return comment && (comment.loc.start.line === lineNumber && lineNumber <= comment.loc.end.line) && (comment.loc.end.line > lineNumber || comment.loc.end.column === line.length) }
/** * Tells if a given comment is trailing: it starts on the current line and * extends to or past the end of the current line. * @param line The source line we want to check for a trailing comment on * @param lineNumber The one-indexed line number for line * @param comment The comment to inspe...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L150-L154
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isFullLineComment
function isFullLineComment(line: string, lineNumber: number, comment: ASTNode | Tree.Comment): boolean { const start = comment.loc.start const end = comment.loc.end const isFirstTokenOnLine = !line.slice(0, comment.loc.start.column).trim() return comment && (start.line < lineNumber || (...
/** * Tells if a comment encompasses the entire line. * @param line The source line with a trailing comment * @param lineNumber The one-indexed line number this is on * @param comment The comment to remove * @returns If the comment covers the entire line */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L163-L171
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isJSXEmptyExpressionInSingleLineContainer
function isJSXEmptyExpressionInSingleLineContainer(node: ASTNode): boolean { if (!node || !node.parent || node.type !== 'JSXEmptyExpression' || node.parent.type !== 'JSXExpressionContainer') return false const parent = node.parent return parent.loc.start.line === parent.loc.end.line }
/** * Check if a node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. * @param node A node to check. * @returns True if the node is a JSXEmptyExpression contained in a single line JSXExpressionContainer. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L178-L185
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
stripTrailingComment
function stripTrailingComment(line: string, comment: ASTNode): string { // loc.column is zero-indexed return line.slice(0, comment.loc.start.column).replace(/\s+$/u, '') }
/** * Gets the line after the comment and any remaining trailing whitespace is * stripped. * @param line The source line with a trailing comment * @param comment The comment to remove * @returns Line without comment and trailing whitespace */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L194-L197
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
ensureArrayAndPush
function ensureArrayAndPush<T extends Record<string, any>>(object: T, key: keyof T, value: unknown): void { if (!Array.isArray(object[key])) object[key] = [] as any object[key].push(value) }
/** * Ensure that an array exists at [key] on `object`, and add `value` to it. * @param object the object to mutate * @param key the object's key * @param value the value to add * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L206-L211
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getAllStrings
function getAllStrings(): Tree.Token[] { return sourceCode.ast.tokens.filter(token => (token.type === 'String' || (token.type === 'JSXText' && sourceCode.getNodeByRangeIndex(token.range[0] - 1)!.type === 'JSXAttribute'))) }
/** * Retrieves an array containing all strings (" or ') in the source code. * @returns An array of string nodes. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L217-L220
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getAllTemplateLiterals
function getAllTemplateLiterals(): Tree.Token[] { return sourceCode.ast.tokens.filter(token => token.type === 'Template') }
/** * Retrieves an array containing all template literals in the source code. * @returns An array of template literal nodes. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L226-L228
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getAllRegExpLiterals
function getAllRegExpLiterals(): Tree.Token[] { return sourceCode.ast.tokens.filter(token => token.type === 'RegularExpression') }
/** * Retrieves an array containing all RegExp literals in the source code. * @returns An array of RegExp literal nodes. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L234-L236
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
groupArrayByLineNumber
function groupArrayByLineNumber(arr: Tree.Token[]): Record<number, Tree.Token[]> { const obj: Record<number, Tree.Token[]> = {} for (let i = 0; i < arr.length; i++) { const node = arr[i] for (let j = node.loc.start.line; j <= node.loc.end.line; ++j) ensureArrayAndPush(obj, j, nod...
/** * * reduce an array of AST nodes by line number, both start and end. * @param arr array of AST nodes * @returns accululated AST nodes */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L244-L254
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getAllComments
function getAllComments(): ASTNode[] { const comments: ASTNode[] = [] sourceCode.getAllComments() .forEach((commentNode) => { const containingNode = sourceCode.getNodeByRangeIndex(commentNode.range[0])! if (isJSXEmptyExpressionInSingleLineContainer(containingNode)) { ...
/** * Returns an array of all comments in the source code. * If the element in the array is a JSXEmptyExpression contained with a single line JSXExpressionContainer, * the element is changed with JSXExpressionContainer node. * @returns An array of comment nodes */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L262-L280
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkProgramForMaxLength
function checkProgramForMaxLength(node: ASTNode): void { // split (honors line-ending) const lines = sourceCode.lines // list of comments to ignore const comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? getAllComments() : [] // we iterate over comments in paralle...
/** * Check the program for max length * @param node Node to examine * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-len/max-len._js_.ts#L287-L404
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
reportFirstExtraStatementAndClear
function reportFirstExtraStatementAndClear() { if (firstExtraStatement) { context.report({ node: firstExtraStatement, messageId: 'exceed', data: { numberOfStatementsOnThisLine, maxStatementsPerLine, statements: numberOfStatementsOnThisLine ...
/** * Reports with the first extra statement, and clears it. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line._js_.ts#L87-L100
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getActualLastToken
function getActualLastToken(node: ASTNode) { return sourceCode.getLastToken(node, isNotSemicolonToken) }
/** * Gets the actual last token of a given node. * @param node A node to get. This is a node except EmptyStatement. * @returns The actual last token. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line._js_.ts#L107-L109
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
enterStatement
function enterStatement(node: ASTNode) { const line = node.loc.start.line /** * Skip to allow non-block statements if this is direct child of control statements. * `if (a) foo();` is counted as 1. * But `if (a) foo(); else foo();` should be counted as 2. */ if (node.parent...
/** * Addresses a given node. * It updates the state of this rule, then reports the node if the node violated this rule. * @param node A node to check. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line._js_.ts#L116-L144
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
leaveStatement
function leaveStatement(node: ASTNode) { const line = getActualLastToken(node)!.loc.end.line // Update state. if (line !== lastStatementLine) { reportFirstExtraStatementAndClear() numberOfStatementsOnThisLine = 1 lastStatementLine = line } }
/** * Updates the state of this rule with the end line of leaving node to check with the next statement. * @param node A node to check. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/max-statements-per-line/max-statements-per-line._js_.ts#L150-L159
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkLastToken
function checkLastToken( member: Tree.TypeElement, opts: TypeOptionsWithType, isLast: boolean, ): void { /** * Resolves the boolean value for the given setting enum value * @param type the option name */ function getOption(type: Delimiter): boolean { if (is...
/** * Check the last token in the given member. * @param member the member to be evaluated. * @param opts the options to be validated. * @param isLast a flag indicating `member` is the last in the interface or type literal. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/member-delimiter-style/member-delimiter-style._ts_.ts#L218-L309
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getOption
function getOption(type: Delimiter): boolean { if (isLast && !opts.requireLast) { // only turn the option on if its expecting no delimiter for the last member return type === 'none' } return opts.delimiter === type }
/** * Resolves the boolean value for the given setting enum value * @param type the option name */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/member-delimiter-style/member-delimiter-style._ts_.ts#L227-L233
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkMemberSeparatorStyle
function checkMemberSeparatorStyle( node: Tree.TSInterfaceBody | Tree.TSTypeLiteral, ): void { const members = node.type === AST_NODE_TYPES.TSInterfaceBody ? node.body : node.members let isSingleLine = node.loc.start.line === node.loc.end.line if ( options.multilineDetection === 'la...
/** * Check the member separator being used matches the delimiter. * @param node the node to be evaluated. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/member-delimiter-style/member-delimiter-style._ts_.ts#L315-L342
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isStarredCommentLine
function isStarredCommentLine(line: string): boolean { return /^\s*\*/u.test(line) }
// ----------------------------------------------------------------------
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L81-L83
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isStarredBlockComment
function isStarredBlockComment([firstComment]: Token[]): boolean { if (firstComment.type !== 'Block') return false const lines = firstComment.value.split(LINEBREAK_MATCHER) // The first and last lines can only contain whitespace. return lines.length > 0 && lines.every((line, i) => (i =...
/** * Checks if a comment group is in starred-block form. * @param firstComment A group of comments, containing either multiple line comments or a single block comment. * @returns Whether or not the comment group is in starred block form. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L90-L98
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isJSDocComment
function isJSDocComment([firstComment]: Token[]): boolean { if (firstComment.type !== 'Block') return false const lines = firstComment.value.split(LINEBREAK_MATCHER) return /^\*\s*$/u.test(lines[0]) && lines.slice(1, -1).every(line => /^\s* /u.test(line)) && /^\s*$/u.test(lin...
/** * Checks if a comment group is in JSDoc form. * @param firstComment A group of comments, containing either multiple line comments or a single block comment. * @returns Whether or not the comment group is in JSDoc form. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L105-L114
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
processSeparateLineComments
function processSeparateLineComments(commentGroup: Token[]): string[] { const allLinesHaveLeadingSpace = commentGroup .map(({ value }) => value) .filter(line => line.trim().length) .every(line => line.startsWith(' ')) return commentGroup.map(({ value }) => (allLinesHaveLeadingSpace ...
/** * Processes a comment group that is currently in separate-line form, calculating the offset for each line. * @param commentGroup A group of comments containing multiple line comments. * @returns An array of the processed lines. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L121-L128
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
processStarredBlockComment
function processStarredBlockComment(comment: Token): string[] { const lines = comment.value.split(LINEBREAK_MATCHER).filter((line, i, linesArr) => !(i === 0 || i === linesArr.length - 1)).map(line => line.replace(/^\s*$/u, '')) const allLinesHaveLeadingSpace = lines .map(line => line.replace(/\s*\*/...
/** * Processes a comment group that is currently in starred-block form, calculating the offset for each line. * @param comment A single block comment token in starred-block form. * @returns An array of the processed lines. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L135-L143
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
processBareBlockComment
function processBareBlockComment(comment: Token): string[] { const lines = comment.value.split(LINEBREAK_MATCHER).map(line => line.replace(/^\s*$/u, '')) const leadingWhitespace = `${sourceCode.text.slice(comment.range[0] - comment.loc.start.column, comment.range[0])} ` let offset = '' /* ...
/** * Processes a comment group that is currently in bare-block form, calculating the offset for each line. * @param comment A single block comment token in bare-block form. * @returns An array of the processed lines. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L150-L185
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getCommentLines
function getCommentLines(commentGroup: Token[]): string[] { const [firstComment] = commentGroup if (firstComment.type === 'Line') return processSeparateLineComments(commentGroup) if (isStarredBlockComment(commentGroup)) return processStarredBlockComment(firstComment) return pr...
/** * Gets a list of comment lines in a group, formatting leading whitespace as necessary. * @param commentGroup A group of comments containing either multiple line comments or a single block comment. * @returns A list of comment lines. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L192-L202
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getInitialOffset
function getInitialOffset(comment: Token): string { return sourceCode.text.slice(comment.range[0] - comment.loc.start.column, comment.range[0]) }
/** * Gets the initial offset (whitespace) from the beginning of a line to a given comment token. * @param comment The token to check. * @returns The offset from the beginning of a line to the token. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L209-L211
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
convertToStarredBlock
function convertToStarredBlock(firstComment: Token, commentLinesList: string[]): string { const initialOffset = getInitialOffset(firstComment) return `/*\n${commentLinesList.map(line => `${initialOffset} * ${line}`).join('\n')}\n${initialOffset} */` }
/** * Converts a comment into starred-block form * @param firstComment The first comment of the group being converted * @param commentLinesList A list of lines to appear in the new starred-block comment * @returns A representation of the comment value in starred-block form, excluding start and end m...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L219-L223
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
convertToSeparateLines
function convertToSeparateLines(firstComment: Token, commentLinesList: string[]): string { return commentLinesList.map(line => `// ${line}`).join(`\n${getInitialOffset(firstComment)}`) }
/** * Converts a comment into separate-line form * @param firstComment The first comment of the group being converted * @param commentLinesList A list of lines to appear in the new starred-block comment * @returns A representation of the comment value in separate-line form */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L231-L233
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
convertToBlock
function convertToBlock(firstComment: Token, commentLinesList: string[]): string { return `/* ${commentLinesList.join(`\n${getInitialOffset(firstComment)} `)} */` }
/** * Converts a comment into bare-block form * @param firstComment The first comment of the group being converted * @param commentLinesList A list of lines to appear in the new starred-block comment * @returns A representation of the comment value in bare-block form */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/multiline-comment-style/multiline-comment-style._js_.ts#L241-L243
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getPrefix
function getPrefix(node: Tree.MemberExpression) { if (node.computed) { if (node.optional) return '?.[' return '[' } if (node.optional) return '?.' return '.' }
/** * Get the prefix of a given MemberExpression node. * If the MemberExpression node is a computed value it returns a * left bracket. If not it returns a period. * @param node A MemberExpression node to get * @returns The prefix of the node. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/newline-per-chained-call/newline-per-chained-call._js_.ts#L54-L65
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
getPropertyText
function getPropertyText(node: Tree.MemberExpression) { const prefix = getPrefix(node) const lines = sourceCode.getText(node.property).split(LINEBREAK_MATCHER) const suffix = node.computed && lines.length === 1 ? ']' : '' return prefix + lines[0] + suffix }
/** * Gets the property text of a given MemberExpression node. * If the text is multiline, this returns only the first line. * @param node A MemberExpression node to get. * @returns The property text of the node. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/newline-per-chained-call/newline-per-chained-call._js_.ts#L73-L79
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isConditional
function isConditional(node: ASTNode) { return node.type === 'ConditionalExpression' }
/** * Checks whether or not a node is a conditional expression. * @param node node to test * @returns `true` if the node is a conditional expression. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-confusing-arrow/no-confusing-arrow._js_.ts#L17-L19
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkArrowFunc
function checkArrowFunc(node: Tree.ArrowFunctionExpression) { const body = node.body if (isConditional(body) && !(allowParens && isParenthesised(sourceCode, body)) && !(onlyOneSimpleParam && !(node.params.length === 1 && node.params[0].type === 'Identifier'))) { context.report({ ...
/** * Reports if an arrow function contains an ambiguous conditional. * @param node A node to check and report. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-confusing-arrow/no-confusing-arrow._js_.ts#L57-L72
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
invalid
function invalid(code: string, output: string | null, type?: string, line?: number | null, config?: any) { const result = { code, output, parserOptions: config && config.parserOptions || {}, errors: [ { messageId: 'unexpected', ...(type && { type }), ...(line && { line })...
/** * Create error message object for failure cases * @param code source code * @param output fixed source code * @param type node type * @param line line number * @param config rule configuration * @returns result object * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.test.ts#L20-L36
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isImmediateFunctionPrototypeMethodCall
function isImmediateFunctionPrototypeMethodCall(node: ASTNode) { const callNode = skipChainExpression(node) if (callNode.type !== 'CallExpression') return false const callee = skipChainExpression(callNode.callee) return ( callee.type === 'MemberExpression' && callee.ob...
/** * Determines whether the given node is a `call` or `apply` method call, invoked directly on a `FunctionExpression` node. * Example: function(){}.call() * @param node The node to be checked. * @returns True if the node is an immediate `call` or `apply` method call. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L127-L140
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
ruleApplies
function ruleApplies(node: ASTNode) { if (node.type === 'JSXElement' || node.type === 'JSXFragment') { const isSingleLine = node.loc.start.line === node.loc.end.line switch (IGNORE_JSX) { // Exclude this JSX element from linting case 'all': return false ...
/** * Determines if this rule should be enforced for a node given the current configuration. * @param node The node to be checked. * @returns True if the rule should be enforced for this node. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L148-L180
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isParenthesised
function isParenthesised(node: ASTNode) { return isParenthesizedRaw(node, sourceCode, 1) }
/** * Determines if a node is surrounded by parentheses. * @param node The node to be checked. * @returns True if the node is parenthesised. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L188-L190
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isParenthesisedTwice
function isParenthesisedTwice(node: ASTNode) { return isParenthesizedRaw(node, sourceCode, 2) }
/** * Determines if a node is surrounded by parentheses twice. * @param node The node to be checked. * @returns True if the node is doubly parenthesised. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L198-L200
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
hasExcessParens
function hasExcessParens(node: ASTNode) { return ruleApplies(node) && isParenthesised(node) }
/** * Determines if a node is surrounded by (potentially) invalid parentheses. * @param node The node to be checked. * @returns True if the node is incorrectly parenthesised. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L208-L210
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
hasDoubleExcessParens
function hasDoubleExcessParens(node: ASTNode) { return ruleApplies(node) && isParenthesisedTwice(node) }
/** * Determines if a node that is expected to be parenthesised is surrounded by * (potentially) invalid extra parentheses. * @param node The node to be checked. * @returns True if the node is has an unexpected extra pair of parentheses. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L219-L221
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
hasExcessParensWithPrecedence
function hasExcessParensWithPrecedence(node: ASTNode, precedenceLowerLimit: number) { if (ruleApplies(node) && isParenthesised(node)) { if ( precedence(node) >= precedenceLowerLimit || isParenthesisedTwice(node) ) { return true } } return false ...
/** * Determines if a node that is expected to be parenthesised is surrounded by * (potentially) invalid extra parentheses with considering precedence level of the node. * If the preference level of the node is not higher or equal to precedence lower limit, it also checks * whether the node is surro...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L233-L243
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isCondAssignException
function isCondAssignException(node: Tree.ConditionalExpression | Tree.DoWhileStatement | Tree.WhileStatement | Tree.IfStatement | Tree.ForStatement) { return EXCEPT_COND_ASSIGN && node.test && node.test.type === 'AssignmentExpression' }
/** * Determines if a node test expression is allowed to have a parenthesised assignment * @param node The node to be checked. * @returns True if the assignment can be parenthesised. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L251-L253
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isInReturnStatement
function isInReturnStatement(node: ASTNode) { for (let currentNode = node; currentNode; currentNode = currentNode.parent!) { if ( currentNode.type === 'ReturnStatement' || (currentNode.type === 'ArrowFunctionExpression' && currentNode.body.type !== 'BlockStatement') ) { ...
/** * Determines if a node is in a return statement * @param node The node to be checked. * @returns True if the node is in a return statement. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L261-L272
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isNewExpressionWithParens
function isNewExpressionWithParens(newExpression: Tree.NewExpression) { const lastToken = sourceCode.getLastToken(newExpression)! const penultimateToken = sourceCode.getTokenBefore(lastToken)! return newExpression.arguments.length > 0 || ( // The expression should end with its own pare...
/** * Determines if a constructor function is newed-up with parens * @param newExpression The NewExpression node to be checked. * @returns True if the constructor is called with parens. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L280-L292
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
containsAssignment
function containsAssignment(node: ASTNode) { if (node.type === 'AssignmentExpression') return true if (node.type === 'ConditionalExpression' && (node.consequent.type === 'AssignmentExpression' || node.alternate.type === 'AssignmentExpression')) { return true } if ('left...
/** * Determines if a node is or contains an assignment expression * @param node The node to be checked. * @returns True if the node is or contains an assignment expression. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L300-L315
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isReturnAssignException
function isReturnAssignException(node: ASTNode) { if (!EXCEPT_RETURN_ASSIGN || !isInReturnStatement(node)) return false if (node.type === 'ReturnStatement') return node.argument && containsAssignment(node.argument) if (node.type === 'ArrowFunctionExpression' && node.body.type !== 'Bl...
/** * Determines if a node is contained by or is itself a return statement and is allowed to have a parenthesised assignment * @param node The node to be checked. * @returns True if the assignment can be parenthesised. * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L323-L334
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
hasExcessParensNoLineTerminator
function hasExcessParensNoLineTerminator(token: Token, node: ASTNode) { if (token.loc.end.line === node.loc.start.line) return hasExcessParens(node) return hasDoubleExcessParens(node) }
/** * Determines if a node following a [no LineTerminator here] restriction is * surrounded by (potentially) invalid extra parentheses. * @param token The token preceding the [no LineTerminator here] restriction. * @param node The node to be checked. * @returns True if the node is incorrectly p...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L344-L349
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
requiresLeadingSpace
function requiresLeadingSpace(node: ASTNode) { const leftParenToken = sourceCode.getTokenBefore(node)! const tokenBeforeLeftParen = sourceCode.getTokenBefore(leftParenToken, { includeComments: true })! const tokenAfterLeftParen = sourceCode.getTokenAfter(leftParenToken, { includeComments: true })! ...
/** * Determines whether a node should be preceded by an additional space when removing parens * @param node node to evaluate; must be surrounded by parentheses * @returns `true` if a space should be inserted before the node * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L357-L366
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
requiresTrailingSpace
function requiresTrailingSpace(node: ASTNode) { const nextTwoTokens = sourceCode.getTokensAfter(node, { count: 2 }) const rightParenToken = nextTwoTokens[0] const tokenAfterRightParen = nextTwoTokens[1] const tokenBeforeRightParen = sourceCode.getLastToken(node)! return rightParenToken &&...
/** * Determines whether a node should be followed by an additional space when removing parens * @param node node to evaluate; must be surrounded by parentheses * @returns `true` if a space should be inserted after the node * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L374-L383
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isIIFE
function isIIFE(node: ASTNode) { const maybeCallNode = skipChainExpression(node) return maybeCallNode.type === 'CallExpression' && maybeCallNode.callee.type === 'FunctionExpression' }
/** * Determines if a given expression node is an IIFE * @param node The node to check * @returns `true` if the given node is an IIFE */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L390-L394
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
canBeAssignmentTarget
function canBeAssignmentTarget(node: ASTNode) { return !!(node && (node.type === 'Identifier' || node.type === 'MemberExpression')) }
/** * Determines if the given node can be the assignment target in destructuring or the LHS of an assignment. * This is to avoid an autofix that could change behavior because parsers mistakenly allow invalid syntax, * such as `(a = b) = c` and `[(a = b) = c] = []`. Ideally, this function shouldn't be nec...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L403-L405
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isFixable
function isFixable(node: ASTNode) { // if it's not a string literal it can be autofixed if (node.type !== 'Literal' || typeof node.value !== 'string') return true if (isParenthesisedTwice(node)) return true return !isTopLevelExpressionStatement(node.parent) }
/** * Checks if a node is fixable. * A node is fixable if removing a single pair of surrounding parentheses does not turn it * into a directive after fixing other nodes. * Almost all nodes are fixable, except if all of the following conditions are met: * The node is a string Literal * It h...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L419-L428
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
report
function report(node: ASTNode) { const leftParenToken = sourceCode.getTokenBefore(node)! const rightParenToken = sourceCode.getTokenAfter(node)! if (!isParenthesisedTwice(node)) { if (tokensToIgnore.has(sourceCode.getFirstToken(node)!)) return if (isIIFE(node) && !('callee'...
/** * Report the node * @param node node to evaluate * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L435-L488
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
finishReport
function finishReport() { context.report({ node, loc: leftParenToken.loc, messageId: 'unexpected', fix: isFixable(node) ? (fixer) => { const parenthesizedSource = sourceCode.text.slice(leftParenToken.range[1], rightParenToken.range[0]) ...
/** * Finishes reporting * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L464-L480
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkArgumentWithPrecedence
function checkArgumentWithPrecedence(node: ASTNode) { if ('argument' in node && node.argument && hasExcessParensWithPrecedence(node.argument, precedence(node))) report(node.argument) }
/** * Evaluate a argument of the node. * @param node node to evaluate * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L495-L498
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
doesMemberExpressionContainCallExpression
function doesMemberExpressionContainCallExpression(node: Tree.MemberExpression) { let currentNode = node.object let currentNodeType = node.object.type while (currentNodeType === 'MemberExpression') { if (!('object' in currentNode)) break currentNode = currentNode.object ...
/** * Check if a member expression contains a call expression * @param node MemberExpression node to evaluate * @returns true if found, false if not */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L505-L517
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkCallNew
function checkCallNew(node: Tree.CallExpression | Tree.NewExpression) { const callee = node.callee if (hasExcessParensWithPrecedence(callee, precedence(node))) { if ( hasDoubleExcessParens(callee) || !( isIIFE(node) // (new A)(); new (new A)(); ...
/** * Evaluate a new call * @param node node to evaluate * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L524-L562
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkBinaryLogical
function checkBinaryLogical(node: Tree.BinaryExpression | Tree.LogicalExpression) { const prec = precedence(node) const leftPrecedence = precedence(node.left) const rightPrecedence = precedence(node.right) const isExponentiation = node.operator === '**' const shouldSkipLeft = NESTED_BINARY...
/** * Evaluate binary logicals * @param node node to evaluate * @private */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L569-L597
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkClass
function checkClass(node: Tree.ClassExpression | Tree.ClassDeclaration) { if (!node.superClass) return /** * If `node.superClass` is a LeftHandSideExpression, parentheses are extra. * Otherwise, parentheses are needed. */ const hasExtraParens = precedence(node.superClass)...
/** * Check the parentheses around the super class of the given class definition. * @param node The node of class declarations to check. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L603-L617
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkSpreadOperator
function checkSpreadOperator(node: Tree.SpreadElement) { if (hasExcessParensWithPrecedence(node.argument, PRECEDENCE_OF_ASSIGNMENT_EXPR)) report(node.argument) }
/** * Check the parentheses around the argument of the given spread operator. * @param node The node of spread elements/properties to check. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L623-L626
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
checkExpressionOrExportStatement
function checkExpressionOrExportStatement(node: Tree.ExportDefaultDeclaration | Tree.ExpressionStatement | Tree.DefaultExportDeclarations) { const firstToken = isParenthesised(node) ? sourceCode.getTokenBefore(node)! : sourceCode.getFirstToken(node)! const secondToken = sourceCode.getTokenAfter(firstToken, ...
/** * Checks the parentheses for an ExpressionStatement or ExportDefaultDeclaration * @param node The ExpressionStatement.expression or ExportDefaultDeclaration.declaration node */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L632-L664
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
pathToAncestor
function pathToAncestor(node: ASTNode, ancestor: ASTNode) { const path = [node] let currentNode: ASTNode | null | undefined = node while (currentNode !== ancestor) { currentNode = currentNode.parent /* c8 ignore start */ if (currentNode === null || currentNode === undefined) ...
/** * Finds the path from the given node to the specified ancestor. * @param node First node in the path. * @param ancestor Last node in the path. * @returns Path, including both nodes. * @throws {Error} If the given node does not have the specified ancestor. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L673-L689
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
pathToDescendant
function pathToDescendant(node: ASTNode, descendant: ASTNode) { return pathToAncestor(descendant, node).reverse() }
/** * Finds the path from the given node to the specified descendant. * @param node First node in the path. * @param descendant Last node in the path. * @returns Path, including both nodes. * @throws {Error} If the given node does not have the specified descendant. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L698-L700
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isSafelyEnclosingInExpression
function isSafelyEnclosingInExpression(node: ASTNode, child: ASTNode) { switch (node.type) { case 'ArrayExpression': case 'ArrayPattern': case 'BlockStatement': case 'ObjectExpression': case 'ObjectPattern': case 'TemplateLiteral': return true case...
/** * Checks whether the syntax of the given ancestor of an 'in' expression inside a for-loop initializer * is preventing the 'in' keyword from being interpreted as a part of an ill-formed for-in loop. * @param node Ancestor of an 'in' expression. * @param child Child of the node, ancestor of the sa...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L709-L733
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
startNewReportsBuffering
function startNewReportsBuffering() { reportsBuffer = { upper: reportsBuffer, inExpressionNodes: [], reports: [], } }
/** * Starts a new reports buffering. Warnings will be stored in a buffer instead of being reported immediately. * An additional logic that requires multiple nodes (e.g. a whole subtree) may dismiss some of the stored warnings. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L739-L745
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
endCurrentReportsBuffering
function endCurrentReportsBuffering() { const { upper, inExpressionNodes, reports } = reportsBuffer ?? {} if (upper) { upper.inExpressionNodes.push(...inExpressionNodes ?? []) upper.reports.push(...reports ?? []) } else { // flush remaining reports reports?.forEa...
/** * Ends the current reports buffering. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L750-L763
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isInCurrentReportsBuffer
function isInCurrentReportsBuffer(node: ASTNode) { return reportsBuffer?.reports.some(r => r.node === node) }
/** * Checks whether the given node is in the current reports buffer. * @param node Node to check. * @returns True if the node is in the current buffer, false otherwise. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L770-L772
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
removeFromCurrentReportsBuffer
function removeFromCurrentReportsBuffer(node: ASTNode) { if (reportsBuffer) reportsBuffer.reports = reportsBuffer.reports.filter(r => r.node !== node) }
/** * Removes the given node from the current reports buffer. * @param node Node to remove. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L778-L781
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isMemberExpInNewCallee
function isMemberExpInNewCallee(node: ASTNode): boolean { if (node.type === 'MemberExpression') { return node.parent.type === 'NewExpression' && node.parent.callee === node ? true : 'object' in node.parent && node.parent.object === node && isMemberExpInNewCallee(node.parent) } ...
/** * Checks whether a node is a MemberExpression at NewExpression's callee. * @param node node to check. * @returns True if the node is a MemberExpression at NewExpression's callee. false otherwise. */
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L788-L795
481d54b6521b8705570132424c78d320dec57610
eslint-stylistic
github_2023
eslint-stylistic
typescript
isAnonymousFunctionAssignmentException
function isAnonymousFunctionAssignmentException({ left, operator, right }: Tree.AssignmentExpression) { if (left.type === 'Identifier' && ['=', '&&=', '||=', '??='].includes(operator)) { const rhsType = right.type if (rhsType === 'ArrowFunctionExpression') return true if ((rhsT...
/** * Checks if the left-hand side of an assignment is an identifier, the operator is one of * `=`, `&&=`, `||=` or `??=` and the right-hand side is an anonymous class or function. * * As per https://tc39.es/ecma262/#sec-assignment-operators-runtime-semantics-evaluation, an * assignment involvi...
https://github.com/eslint-stylistic/eslint-stylistic/blob/481d54b6521b8705570132424c78d320dec57610/packages/eslint-plugin/rules/no-extra-parens/no-extra-parens._js_.ts#L815-L826
481d54b6521b8705570132424c78d320dec57610