Clean up capture methods
MODIFY
packages/@romejs/cli-flags/Parser.ts
packages/@romejs/cli-flags/Parser.ts
@@ -339,7 +339,7 @@ export default class Parser<T> {
let definedCommand: undefined | DefinedCommand;
- const rootFlags = await consumer.captureDiagnostics(async (consumer) => {
+ const rootFlags = await consumer.bufferDiagnostics(async (consumer) => {
for (const shorthandName of this.shorthandFlags) {
consumer.get(shorthandName).unexpected(
`Shorthand flags are not supported`,
@@ -519,10 +519,9 @@ export default class Parser<T> {
const {description, usage, examples, programName} = this.opts;
- const consumer = this.getFlagsConsumer();
+ const {consumer} = this.getFlagsConsumer().capture();
// Supress diagnostics
- await consumer.capture(async (consumer) => {
await this.opts.defineFlags(consumer);
for (const key of this.commands.keys()) {
@@ -530,7 +529,6 @@ export default class Parser<T> {
}
this.showUsageHelp(description, usage);
- });
const {reporter} = this;
reporter.section('Global Flags', () => {

MODIFY
packages/@romejs/codec-js-manifest/index.ts
packages/@romejs/codec-js-manifest/index.ts
@@ -463,9 +463,8 @@ function normalizeExportsConditions(value: Consumer): ManifestExportConditions {
} else if (Array.isArray(unknown)) {
// Find the first item that passes validation
for (const elem of value.asArray()) {
- const {result, diagnostics} = elem.captureSync(
- (consumer) => normalizeExportsConditions(consumer),
- );
+ const {consumer, diagnostics} = elem.capture();
+ const result = normalizeExportsConditions(consumer);
if (diagnostics.length === 0) {
return result;
}
@@ -581,15 +580,15 @@ function checkDependencyKeyTypo(key: string, prop: Consumer) {
export async function normalizeManifest(
path: AbsoluteFilePath,
- consumer: Consumer,
+ rawConsumer: Consumer,
): Promise<{
manifest: Manifest;
diagnostics: Diagnostics;
}> {
const loose = path.getSegments().includes('node_modules');
- const {result: manifest, diagnostics} = await consumer.capture(
- (consumer) => {
+ const {consumer, diagnostics} = rawConsumer.capture();
+
// FIXME: There's this ridiculous node module that includes it's tests... which deliberately includes an invalid package.json
if (path.join().includes('resolve/test/resolver/invalid_main')) {
consumer.setValue({});
@@ -636,21 +635,13 @@ export async function normalizeManifest(
// Dependency fields
dependencies: normalizeDependencies(consumer, 'dependencies', loose),
- devDependencies: normalizeDependencies(
- consumer,
- 'devDependencies',
- loose,
- ),
+ devDependencies: normalizeDependencies(consumer, 'devDependencies', loose),
optionalDependencies: normalizeDependencies(
consumer,
'optionalDependencies',
loose,
),
- peerDependencies: normalizeDependencies(
- consumer,
- 'peerDependencies',
- loose,
- ),
+ peerDependencies: normalizeDependencies(consumer, 'peerDependencies', loose),
bundledDependencies: [
...normalizeStringArray(consumer.get('bundledDependencies'), loose),
@@ -668,10 +659,6 @@ export async function normalizeManifest(
raw: consumer.asJSONObject(),
};
- return manifest;
- },
- );
-
return {
manifest,
diagnostics,

MODIFY
packages/@romejs/consume/Consumer.ts
packages/@romejs/consume/Consumer.ts
@@ -133,27 +133,7 @@ export default class Consumer {
hasHandledUnexpected: boolean;
forceDiagnosticTarget: undefined | ConsumeSourceLocationRequestTarget;
- async capture<T>(callback: (consumer: Consumer) => Promise<T> | T): Promise<{
- result: T;
- definitions: Array<ConsumePropertyDefinition>;
- diagnostics: Diagnostics;
- }> {
- const {definitions, diagnostics, consumer} = this._capture();
- const result = await callback(consumer);
- return {result, definitions, diagnostics};
- }
-
- captureSync<T>(callback: (consumer: Consumer) => T): {
- result: T;
- definitions: Array<ConsumePropertyDefinition>;
- diagnostics: Diagnostics;
- } {
- const {definitions, diagnostics, consumer} = this._capture();
- const result = callback(consumer);
- return {result, definitions, diagnostics};
- }
-
- _capture<T>(): {
+ capture<T>(): {
consumer: Consumer;
definitions: Array<ConsumePropertyDefinition>;
diagnostics: Diagnostics;
@@ -177,10 +157,11 @@ export default class Consumer {
return {consumer, definitions, diagnostics};
}
- async captureDiagnostics<T>(
+ async bufferDiagnostics<T>(
callback: (consumer: Consumer) => Promise<T> | T,
): Promise<T> {
- const {result, diagnostics} = await this.capture(callback);
+ const {diagnostics, consumer} = await this.capture();
+ const result = await callback(consumer);
if (result === undefined || diagnostics.length > 0) {
throw new DiagnosticsError('Captured diagnostics', diagnostics);
}

Clean up capture methods