routie dev init since i didn't adhere to any proper guidance up until now
This commit is contained in:
Generated
Vendored
+30
@@ -0,0 +1,30 @@
|
||||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'
|
||||
import { TSESTree } from '@typescript-eslint/types'
|
||||
import { ScopedRegexOption } from '../../types/scoped-regex-option.js'
|
||||
import { NodeOfType } from '../../types/node-of-type.js'
|
||||
export type NodeValuesComputer<T extends AST_NODE_TYPES> = (
|
||||
node: NodeOfType<T>,
|
||||
) => string[]
|
||||
/**
|
||||
* Checks whether any of the parent nodes match the scoped regex patterns.
|
||||
*
|
||||
* @param params - The parameters object.
|
||||
* @param params.nodeValuesComputer - Function to compute the string values of a
|
||||
* node to match against.
|
||||
* @param params.scopedRegexOption - The scoped regex option to match against.
|
||||
* @param params.allowedNodeTypes - The set of allowed node types to consider.
|
||||
* @param params.parentNodes - The parent nodes to check.
|
||||
* @returns True if any parent node matches the scoped regex patterns, false
|
||||
* otherwise.
|
||||
*/
|
||||
export declare function matchesScopedExpressions<T extends AST_NODE_TYPES>({
|
||||
nodeValuesComputer,
|
||||
scopedRegexOption,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
}: {
|
||||
scopedRegexOption: ScopedRegexOption | undefined
|
||||
nodeValuesComputer: NodeValuesComputer<T>
|
||||
parentNodes: TSESTree.Node[]
|
||||
allowedNodeTypes: Set<T>
|
||||
}): boolean
|
||||
Generated
Vendored
+87
@@ -0,0 +1,87 @@
|
||||
import { matches } from '../matches.js'
|
||||
import { partitionPatternsByScope } from './partition-patterns-by-scope.js'
|
||||
/**
|
||||
* Checks whether any of the parent nodes match the scoped regex patterns.
|
||||
*
|
||||
* @param params - The parameters object.
|
||||
* @param params.nodeValuesComputer - Function to compute the string values of a
|
||||
* node to match against.
|
||||
* @param params.scopedRegexOption - The scoped regex option to match against.
|
||||
* @param params.allowedNodeTypes - The set of allowed node types to consider.
|
||||
* @param params.parentNodes - The parent nodes to check.
|
||||
* @returns True if any parent node matches the scoped regex patterns, false
|
||||
* otherwise.
|
||||
*/
|
||||
function matchesScopedExpressions({
|
||||
nodeValuesComputer,
|
||||
scopedRegexOption,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
}) {
|
||||
if (!scopedRegexOption) {
|
||||
return true
|
||||
}
|
||||
let { shallowScopePatterns, deepScopePatterns } =
|
||||
partitionPatternsByScope(scopedRegexOption)
|
||||
return (
|
||||
matchesShallowScopedExpressions({
|
||||
patterns: shallowScopePatterns,
|
||||
nodeValuesComputer,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
}) ||
|
||||
matchesDeepScopedExpressions({
|
||||
patterns: deepScopePatterns,
|
||||
nodeValuesComputer,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
})
|
||||
)
|
||||
}
|
||||
function matchesShallowScopedExpressions({
|
||||
nodeValuesComputer,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
patterns,
|
||||
}) {
|
||||
let [firstParent] = parentNodes
|
||||
// v8 ignore if -- @preserve Unsure how we can reach that case
|
||||
if (!firstParent) {
|
||||
return false
|
||||
}
|
||||
if (!isNodeTypeAmong(firstParent, allowedNodeTypes)) {
|
||||
return false
|
||||
}
|
||||
return matchesParentExpression({
|
||||
parentNode: firstParent,
|
||||
nodeValuesComputer,
|
||||
patterns,
|
||||
})
|
||||
}
|
||||
function matchesDeepScopedExpressions({
|
||||
nodeValuesComputer,
|
||||
allowedNodeTypes,
|
||||
parentNodes,
|
||||
patterns,
|
||||
}) {
|
||||
return parentNodes
|
||||
.filter(parent => isNodeTypeAmong(parent, allowedNodeTypes))
|
||||
.some(parentNode =>
|
||||
matchesParentExpression({
|
||||
nodeValuesComputer,
|
||||
parentNode,
|
||||
patterns,
|
||||
}),
|
||||
)
|
||||
}
|
||||
function matchesParentExpression({ nodeValuesComputer, parentNode, patterns }) {
|
||||
let nodeValues = nodeValuesComputer(parentNode)
|
||||
return patterns.some(nodeValueMatchesPattern)
|
||||
function nodeValueMatchesPattern(pattern) {
|
||||
return nodeValues.some(nodeValue => matches(nodeValue, pattern))
|
||||
}
|
||||
}
|
||||
function isNodeTypeAmong(node, types) {
|
||||
return types.has(node.type)
|
||||
}
|
||||
export { matchesScopedExpressions }
|
||||
Generated
Vendored
+20
@@ -0,0 +1,20 @@
|
||||
import { ScopedRegexOption, Scope } from '../../types/scoped-regex-option.js'
|
||||
export type SingleRegexOption =
|
||||
| {
|
||||
pattern: string
|
||||
flags?: string
|
||||
scope?: Scope
|
||||
}
|
||||
| string
|
||||
/**
|
||||
* Partitions patterns by their scope (shallow or deep).
|
||||
*
|
||||
* @param patternOrPatterns - A single pattern or an array of patterns.
|
||||
* @returns An object containing arrays of shallow and deep scope patterns.
|
||||
*/
|
||||
export declare function partitionPatternsByScope(
|
||||
patternOrPatterns: ScopedRegexOption,
|
||||
): {
|
||||
shallowScopePatterns: SingleRegexOption[]
|
||||
deepScopePatterns: SingleRegexOption[]
|
||||
}
|
||||
Generated
Vendored
+38
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* Partitions patterns by their scope (shallow or deep).
|
||||
*
|
||||
* @param patternOrPatterns - A single pattern or an array of patterns.
|
||||
* @returns An object containing arrays of shallow and deep scope patterns.
|
||||
*/
|
||||
function partitionPatternsByScope(patternOrPatterns) {
|
||||
if (!Array.isArray(patternOrPatterns)) {
|
||||
let isDeepScopePattern = isDeepScopedPattern(patternOrPatterns)
|
||||
return {
|
||||
shallowScopePatterns: isDeepScopePattern ? [] : [patternOrPatterns],
|
||||
deepScopePatterns: isDeepScopePattern ? [patternOrPatterns] : [],
|
||||
}
|
||||
}
|
||||
let deepScopedPatterns = []
|
||||
let shallowScopedPatterns = []
|
||||
for (let pattern of patternOrPatterns) {
|
||||
if (isDeepScopedPattern(pattern)) {
|
||||
deepScopedPatterns.push(pattern)
|
||||
} else {
|
||||
shallowScopedPatterns.push(pattern)
|
||||
}
|
||||
}
|
||||
return {
|
||||
shallowScopePatterns: shallowScopedPatterns,
|
||||
deepScopePatterns: deepScopedPatterns,
|
||||
}
|
||||
}
|
||||
function isDeepScopedPattern(pattern) {
|
||||
if (typeof pattern !== 'object') {
|
||||
return false
|
||||
}
|
||||
if (!('scope' in pattern)) {
|
||||
return false
|
||||
}
|
||||
return pattern.scope === 'deep'
|
||||
}
|
||||
export { partitionPatternsByScope }
|
||||
Reference in New Issue
Block a user