-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expose Result type for task action success/failure signaling
#8012
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
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7850766
feat: Add TaskResult type for signaling task success/failure from act…
schaable 7699501
feat: support optional value in TaskResult for both success and failure
schaable 48da5a5
Create nice-parrots-do.md
schaable cbbef86
feat: replace TaskResult with general-purpose Result type
schaable 1dc23c1
update changeset
schaable 54dfec9
refactor: use conditional rest params for successResult and errorResult
schaable f155727
refactor: use Result helpers in fixture and move to cli/result
schaable 5a5b747
fix lint
schaable 0c8d1f9
fix: tighten isResult to require value/error discriminator fields
schaable 1c799af
refactor: move types/result to types/util
schaable a873933
test: add more tests to result handling
schaable e35ba8d
refactor: rename successResult to successfulResult
schaable db636ad
docs: update setAction and setInlineAction to reference Result
schaable 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 hidden or 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,5 @@ | ||
| --- | ||
| "hardhat": patch | ||
| --- | ||
|
|
||
| Expose `Result` type for task action success/failure signaling. |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
2 changes: 1 addition & 1 deletion
2
v-next/hardhat/src/internal/builtin-plugins/solidity/build-system/resolver/utils.ts
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| import type { Result } from "../types/utils.js"; | ||
|
|
||
| import { isObject } from "@nomicfoundation/hardhat-utils/lang"; | ||
|
|
||
| /** | ||
| * Creates a successful Result with the given value. | ||
| * | ||
| * @param value The value to include in the result. | ||
| * @returns A Result with success: true and the given value. | ||
| */ | ||
| export function successfulResult<ValueT = undefined>( | ||
| ...args: ValueT extends undefined | void ? [] : [value: ValueT] | ||
| ): { success: true; value: ValueT } { | ||
| /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
| -- Cast needed because TS loses the conditional rest parameter context */ | ||
| return { success: true, value: args[0] as ValueT }; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a failed Result with the given error. | ||
| * | ||
| * @param error The error to include in the result. | ||
| * @returns A Result with success: false and the given error. | ||
| */ | ||
| export function errorResult<ErrorT = undefined>( | ||
| ...args: ErrorT extends undefined | void ? [] : [error: ErrorT] | ||
| ): { success: false; error: ErrorT } { | ||
| /* eslint-disable-next-line @typescript-eslint/consistent-type-assertions | ||
| -- Cast needed because TS loses the conditional rest parameter context */ | ||
| return { success: false, error: args[0] as ErrorT }; | ||
| } | ||
|
|
||
| /** | ||
| * A type guard that checks if a value is a Result. | ||
| * | ||
| * Optionally validates the value and error fields using type guard functions. | ||
| * | ||
| * @param value The value to check. | ||
| * @param isValue Optional type guard for the value field. | ||
| * @param isError Optional type guard for the error field. | ||
| * @returns true if the value is a Result. | ||
| */ | ||
| export function isResult<ValueT = unknown, ErrorT = unknown>( | ||
| value: unknown, | ||
| isValue?: (v: unknown) => v is ValueT, | ||
| isError?: (e: unknown) => e is ErrorT, | ||
| ): value is Result<ValueT, ErrorT> { | ||
| if (!isObject(value) || typeof value.success !== "boolean") { | ||
| return false; | ||
| } | ||
|
|
||
| if (value.success === true) { | ||
| return "value" in value && (isValue === undefined || isValue(value.value)); | ||
| } | ||
|
|
||
|
schaable marked this conversation as resolved.
|
||
| return "error" in value && (isError === undefined || isError(value.error)); | ||
| } | ||
51 changes: 51 additions & 0 deletions
51
v-next/hardhat/test/fixture-projects/cli/result/hardhat.config.ts
This file contains hidden or 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,51 @@ | ||
| import type { HardhatUserConfig } from "../../../../src/config.js"; | ||
|
|
||
| import { task } from "../../../../src/config.js"; | ||
| import { errorResult, successfulResult } from "../../../../src/utils/result.js"; | ||
|
|
||
| const failingTask = task("failing-task") | ||
| .setInlineAction(() => { | ||
| return errorResult(); | ||
| }) | ||
| .build(); | ||
|
|
||
| const succeedingTask = task("succeeding-task") | ||
| .setInlineAction(() => { | ||
| return successfulResult(42); | ||
| }) | ||
| .build(); | ||
|
|
||
| const undefinedTask = task("undefined-task") | ||
| .setInlineAction(() => {}) | ||
| .build(); | ||
|
|
||
| const plainObjectTask = task("plain-object-task") | ||
| .setInlineAction(() => { | ||
| return { failed: 2, passed: 5 }; | ||
| }) | ||
| .build(); | ||
|
|
||
| const failingTaskWithValue = task("failing-task-with-value") | ||
| .setInlineAction(() => { | ||
| return errorResult({ failed: 2, passed: 5 }); | ||
| }) | ||
| .build(); | ||
|
|
||
| const succeedingTaskNoValue = task("succeeding-task-no-value") | ||
| .setInlineAction(() => { | ||
| return successfulResult(); | ||
| }) | ||
| .build(); | ||
|
|
||
| const config: HardhatUserConfig = { | ||
| tasks: [ | ||
| failingTask, | ||
| succeedingTask, | ||
| undefinedTask, | ||
| plainObjectTask, | ||
| failingTaskWithValue, | ||
| succeedingTaskNoValue, | ||
| ], | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.