Skip to content

Commit

Permalink
Use affirm, see #1465
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Dec 2, 2024
1 parent ecf4a17 commit b5a53a7
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 16 deletions.
20 changes: 9 additions & 11 deletions js/browser-and-node/phetioCompareAPIs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

// Import only the types from LoDashStatic, the actual lodash instance is dependency-injected at runtime
import type { LoDashStatic } from 'lodash';
import affirm from '../../../perennial-alias/js/browser-and-node/affirm.js';

import { FlattenedAPIPhetioElements, PhetioAPI, PhetioElement, PhetioElementMetadata, PhetioElementMetadataValue, PhetioElements, PhetioElementState } from '../../../tandem/js/phet-io-types.js';
import isInitialStateCompatible from './isInitialStateCompatible.js';
Expand All @@ -32,8 +33,6 @@ type PhetioCompareAPIsResult = {
designedProblems: string[];
};

type Assert = undefined | ( ( bool: any, message?: string ) => void );

const METADATA_KEY_NAME = '_metadata';
const DATA_KEY_NAME = '_data';

Expand Down Expand Up @@ -79,11 +78,11 @@ const toStructuredTree = ( api: { phetioElements: FlattenedAPIPhetioElements },
return sparseAPI;
};

const getMetadataValues = ( phetioElement: PhetioElement, api: PhetioAPI, _: LoDashStatic, assert: Assert ): PhetioElementMetadata => {
const getMetadataValues = ( phetioElement: PhetioElement, api: PhetioAPI, _: LoDashStatic ): PhetioElementMetadata => {
const ioTypeName = phetioElement[ METADATA_KEY_NAME ] ? ( phetioElement[ METADATA_KEY_NAME ].phetioTypeName || 'ObjectIO' ) : 'ObjectIO';

if ( api.version ) {
const defaults = getMetadataDefaults( ioTypeName, api, _, assert );
const defaults = getMetadataDefaults( ioTypeName, api, _ );
return _.merge( defaults, phetioElement[ METADATA_KEY_NAME ] );
}
else {
Expand All @@ -96,11 +95,11 @@ const getMetadataValues = ( phetioElement: PhetioElement, api: PhetioAPI, _: LoD
/**
* @returns - defensive copy, non-mutating
*/
const getMetadataDefaults = ( typeName: string, api: PhetioAPI, _: LoDashStatic, assert: Assert ): PhetioElementMetadata => {
const getMetadataDefaults = ( typeName: string, api: PhetioAPI, _: LoDashStatic ): PhetioElementMetadata => {
const entry = api.phetioTypes[ typeName ];
assert && assert( entry, `entry missing: ${typeName}` );
affirm( entry, `entry missing: ${typeName}` );
if ( entry.supertype ) {
return _.merge( getMetadataDefaults( entry.supertype, api, _, assert ), entry.metadataDefaults );
return _.merge( getMetadataDefaults( entry.supertype, api, _ ), entry.metadataDefaults );
}
else {
return _.merge( {}, entry.metadataDefaults ) as PhetioElementMetadata;
Expand All @@ -120,10 +119,9 @@ const isOldAPIVersion = ( api: PhetioAPI ): boolean => {
* @param referenceAPI - the "ground truth" or reference API
* @param proposedAPI - the proposed API for comparison with referenceAPI
* @param _ - lodash, so this can be used from different contexts.
* @param assert - so this can be used from different contexts
* @param providedOptions
*/
const phetioCompareAPIs = ( referenceAPI: PhetioAPI, proposedAPI: PhetioAPI, _: LoDashStatic, assert: Assert, providedOptions?: Partial<PhetioCompareAPIsOptions> ): PhetioCompareAPIsResult => {
const phetioCompareAPIs = ( referenceAPI: PhetioAPI, proposedAPI: PhetioAPI, _: LoDashStatic, providedOptions?: Partial<PhetioCompareAPIsOptions> ): PhetioCompareAPIsResult => {

// If the proposed version predates 1.0, then bring it forward to the structured tree with metadata under `_metadata`.
if ( isOldAPIVersion( proposedAPI ) ) {
Expand Down Expand Up @@ -172,8 +170,8 @@ const phetioCompareAPIs = ( referenceAPI: PhetioAPI, proposedAPI: PhetioAPI, _:
// Override isDesigned, if specified. Once on, you cannot turn off a subtree.
isDesignedElement = isDesignedElement || reference[ METADATA_KEY_NAME ].phetioDesigned;

const referenceCompleteMetadata = getMetadataValues( reference, referenceAPI, _, assert );
const proposedCompleteMetadata = getMetadataValues( proposed, proposedAPI, _, assert );
const referenceCompleteMetadata = getMetadataValues( reference, referenceAPI, _ );
const proposedCompleteMetadata = getMetadataValues( proposed, proposedAPI, _ );

/**
* Push any problems that may exist for the provided metadataKey.
Expand Down
3 changes: 1 addition & 2 deletions js/phet-io/phetioCompareAPISets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* @author Sam Reid (PhET Interactive Simulations)
*/

import assert from 'assert';
import fs from 'fs';
import _ from 'lodash';
import phetioCompareAPIs, { PhetioCompareAPIsOptions } from '../browser-and-node/phetioCompareAPIs.js';
Expand Down Expand Up @@ -39,7 +38,7 @@ export default async ( repos: string[], proposedAPIs: PhetioAPIs, options?: Part
throw new Error( `No proposedAPI for repo: ${repo}` );
}

const comparisonData = phetioCompareAPIs( referenceAPI, proposedAPI, _, assert, {
const comparisonData = phetioCompareAPIs( referenceAPI, proposedAPI, _, {
compareBreakingAPIChanges: options.compareBreakingAPIChanges,
compareDesignedAPIChanges: !!phetioSection.compareDesignedAPIChanges // determined from the package.json flag
} );
Expand Down
4 changes: 1 addition & 3 deletions js/phet-io/phetioCompareAPIsTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

import assert from 'assert';
import _ from 'lodash';
import qunit from '../../../perennial-alias/js/npm-dependencies/qunit.js';
import { PhetioAPI } from '../../../tandem/js/phet-io-types.js';
import phetioCompareAPIs, { PhetioCompareAPIsOptions } from '../browser-and-node/phetioCompareAPIs.js';

qunit.module( 'phetioCompareAPIs' );


const _phetioCompareAPIs = ( referenceAPI: PhetioAPI, proposedAPI: PhetioAPI, options?: Partial<PhetioCompareAPIsOptions> ) => {
return phetioCompareAPIs( referenceAPI, proposedAPI, _, assert, options );
return phetioCompareAPIs( referenceAPI, proposedAPI, _, options );
};

// To cover the boilerplate that will be largely copied around otherwise.
Expand Down

0 comments on commit b5a53a7

Please sign in to comment.