-
-
Notifications
You must be signed in to change notification settings - Fork 240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(core): improve alias validation #2164
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
549c950
feat(core): improve alias validation
P0lip cebca90
chore(core): ignore overrides for now
P0lip e086d74
feat(core): validate aliases in overrides
P0lip e546d4f
chore(core): eslint warning
P0lip 38d9c12
chore(core): no cache
P0lip c9709f3
feat(core): handle AggregateErrors
P0lip f8c5487
test(core): validate error paths
P0lip e7b5afe
fix(core): default fn options to null
P0lip a0cbe59
Merge branch 'develop' into feat/core/alias-validation
P0lip d1424bf
chore(core): ah, Node 12
P0lip 53ad865
chore(test-utils): cast error to object for proper assertion
P0lip 3476a13
chore(deps-dev): bump ESLint and related packages
P0lip 209fdd5
Merge branch 'develop' into feat/core/alias-validation
P0lip 462e258
Merge branch 'develop' into feat/core/alias-validation
P0lip 496f753
revert(rulesets): toThrowError
P0lip File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { isScopedAliasDefinition, isSimpleAliasDefinition } from './utils/guards'; | ||
import type { RulesetScopedAliasDefinition } from './types'; | ||
|
||
const ALIAS = /^#([A-Za-z0-9_-]+)/; | ||
|
||
export function resolveAliasForFormats( | ||
{ targets }: RulesetScopedAliasDefinition, | ||
formats: Set<unknown> | null, | ||
): string[] | null { | ||
if (formats === null || formats.size === 0) { | ||
return null; | ||
} | ||
|
||
// we start from the end to be consistent with overrides etc. - we generally tend to pick the "last" value. | ||
for (let i = targets.length - 1; i >= 0; i--) { | ||
const target = targets[i]; | ||
for (const format of target.formats) { | ||
if (formats.has(format)) { | ||
return target.given; | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
export function resolveAlias( | ||
aliases: Record<string, unknown> | null, | ||
expression: string, | ||
formats: Set<unknown> | null, | ||
): string[] { | ||
return _resolveAlias(aliases, expression, formats, new Set()); | ||
} | ||
|
||
function _resolveAlias( | ||
aliases: Record<string, unknown> | null, | ||
expression: string, | ||
formats: Set<unknown> | null, | ||
stack: Set<string>, | ||
): string[] { | ||
const resolvedExpressions: string[] = []; | ||
|
||
if (expression.startsWith('#')) { | ||
const alias = ALIAS.exec(expression)?.[1]; | ||
|
||
if (alias === void 0 || alias === null) { | ||
throw new ReferenceError(`Alias must match /^#([A-Za-z0-9_-]+)/`); | ||
} | ||
|
||
if (stack.has(alias)) { | ||
const _stack = [...stack, alias]; | ||
throw new ReferenceError(`Alias "${_stack[0]}" is circular. Resolution stack: ${_stack.join(' -> ')}`); | ||
} | ||
|
||
stack.add(alias); | ||
|
||
if (aliases === null || !(alias in aliases)) { | ||
throw new ReferenceError(`Alias "${alias}" does not exist`); | ||
} | ||
|
||
const aliasValue = aliases[alias]; | ||
let actualAliasValue: string[] | null; | ||
if (isSimpleAliasDefinition(aliasValue)) { | ||
actualAliasValue = aliasValue; | ||
} else if (isScopedAliasDefinition(aliasValue)) { | ||
actualAliasValue = resolveAliasForFormats(aliasValue, formats); | ||
} else { | ||
actualAliasValue = null; | ||
} | ||
|
||
if (actualAliasValue !== null) { | ||
resolvedExpressions.push( | ||
...actualAliasValue.flatMap(item => | ||
_resolveAlias(aliases, item + expression.slice(alias.length + 1), formats, new Set([...stack])), | ||
), | ||
); | ||
} | ||
} else { | ||
resolvedExpressions.push(expression); | ||
} | ||
|
||
return resolvedExpressions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be worth adding a note re override behavior. E.g. - if an alias called
Stoplight
were defined here, would it take precedence over theStoplight
alias defined in the root?