Skip to content

Commit

Permalink
use IntentionalPerennialAny, #369
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph authored and jessegreenberg committed Dec 10, 2024
1 parent a846704 commit 4367167
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 48 deletions.
8 changes: 0 additions & 8 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,13 @@

import nodeEslintConfig from './js/eslint/config/node.eslint.config.mjs';

export const perennialRules = {
rules: {

// There is a complication about importing phet-core from perennial, so use `any` in this repo
'@typescript-eslint/no-explicit-any': 'off'
}
};

/**
* @author Sam Reid (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
*/
export default [
...nodeEslintConfig,
perennialRules,
{
rules: {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

export type Repo = string;
export type Repo = string;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type IntentionalPerennialAny = any;
26 changes: 13 additions & 13 deletions js/browser-and-node/affirm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
// Copyright 2024, University of Colorado Boulder

import { IntentionalPerennialAny } from './PerennialTypes.js';

// Define an interface that includes the optional 'assert' property
type GlobalWithAssert = {
assert?: boolean;
};

// Respect the global assert flag, which can be set to false to disable all assertions
const isBrowser = globalThis.hasOwnProperty( 'window' );
const isNode = !isBrowser;

/**
* Like assert.js, with the following differences:
*
Expand All @@ -12,20 +23,9 @@
* the term `affirm` is not otherwise used in our codebase.
*
* @author Sam Reid (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
*/

type IntentionalAny = any;

// Define an interface that includes the optional 'assert' property
type GlobalWithAssert = {
assert?: boolean;
};

// Respect the global assert flag, which can be set to false to disable all assertions
const isBrowser = globalThis.hasOwnProperty( 'window' );
const isNode = !isBrowser;

export default function affirm( predicate: unknown, ...messages: IntentionalAny[] ): asserts predicate {
export default function affirm( predicate: unknown, ...messages: IntentionalPerennialAny[] ): asserts predicate {

// Cast 'globalThis' to 'GlobalWithAssert' to access 'assert' safely
const isAssertEnabled = isNode || ( globalThis as GlobalWithAssert ).assert;
Expand Down
4 changes: 1 addition & 3 deletions js/browser-and-node/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
// Copyright 2024, University of Colorado Boulder


import { perennialRules } from '../../eslint.config.mjs';
import browserAndNodeEslintConfig from '../eslint/config/browser-and-node.eslint.config.mjs';

/**
* @author Sam Reid (PhET Interactive Simulations)
* @author Michael Kauzmann (PhET Interactive Simulations)
*/
export default [
...browserAndNodeEslintConfig,
perennialRules
...browserAndNodeEslintConfig
];
2 changes: 1 addition & 1 deletion js/eslint/divideIntoBatches.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024, University of Colorado Boulder

import os from 'os';
import { Repo } from '../common/PerennialTypes.js';
import { Repo } from '../browser-and-node/PerennialTypes.js';

const MAX_BATCH_SIZE = 50;

Expand Down
2 changes: 1 addition & 1 deletion js/eslint/getLintCLIOptions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2024, University of Colorado Boulder

import getRepoList from '../common/getRepoList.js';
import { Repo } from '../common/PerennialTypes.js';
import { Repo } from '../browser-and-node/PerennialTypes.js';
/**
* Parse command line options for linting.
*
Expand Down
2 changes: 1 addition & 1 deletion js/eslint/lint-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import fs from 'fs';
import _ from 'lodash';
import path from 'path';
import process from 'process';
import { Repo } from '../common/PerennialTypes.js';
import { Repo } from '../browser-and-node/PerennialTypes.js';
import { tscCleanRepo } from '../grunt/check.js';
import { DEBUG_PHET_LINT } from './lint.js';

Expand Down
2 changes: 1 addition & 1 deletion js/eslint/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { spawn } from 'child_process';
import _ from 'lodash';
import path from 'path';
import dirname from '../common/dirname.js';
import { Repo } from '../common/PerennialTypes.js';
import { Repo } from '../browser-and-node/PerennialTypes.js';
import tsxCommand from '../common/tsxCommand.js';
import divideIntoBatches from './divideIntoBatches.js';
import { DEFAULT_MAX_PROCESSES, LintOptions } from './getLintCLIOptions.js';
Expand Down
2 changes: 1 addition & 1 deletion js/grunt/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import os from 'os';
import path from 'path';
import fixEOL from '../common/fixEOL.js';
import { PERENNIAL_ROOT } from '../common/perennialRepoUtils.js';
import { Repo } from '../common/PerennialTypes.js';
import { Repo } from '../browser-and-node/PerennialTypes.js';

const ALL_CONFIG_PATH = `${PERENNIAL_ROOT}/../chipper/dist/tsconfig/all/`;
const tscCommand = `${PERENNIAL_ROOT}/node_modules/typescript/bin/tsc`;
Expand Down
4 changes: 3 additions & 1 deletion js/grunt/tasks/generate-data.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2024, University of Colorado Boulder

import { IntentionalPerennialAny } from '../../browser-and-node/PerennialTypes.js';

/**
* Generates the lists under perennial/data/, and if there were changes, will commit and push.
*
Expand Down Expand Up @@ -39,7 +41,7 @@ async function generateData(): Promise<void> {

const activeRepos: string[] = getActiveRepos();

function writeList( name: string, packageFilter: ( repo: any ) => void ): void {
function writeList( name: string, packageFilter: ( repo: IntentionalPerennialAny ) => void ): void {
const repos = activeRepos.filter( repo => {
// Make sure that if someone doesn't have all repositories checked out that this will FAIL. Otherwise bad things.
assert( grunt.file.exists( `../${repo}` ) );
Expand Down
3 changes: 2 additions & 1 deletion js/grunt/tasks/util/getOption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
// Use nopt to guarantee compatibility with grunt. See usage site: https://github.com/phetsims/chipper/issues/1459
// See usage in chipper/node_modules/grunt-cli/bin/grunt
import nopt from 'nopt';
import { IntentionalPerennialAny } from '../../../browser-and-node/PerennialTypes.js';

const options = nopt( {}, {}, process.argv, 2 );

export default function getOption( keyName: string ): any {
export default function getOption( keyName: string ): IntentionalPerennialAny {
return options[ keyName ];
}

Expand Down
2 changes: 1 addition & 1 deletion js/grunt/tasks/util/getRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { readFileSync } from 'fs';
import path from 'path';
import process from 'process';
import dirname from '../../../common/dirname.js';
import { Repo } from '../../../common/PerennialTypes.js';
import { Repo } from '../../../browser-and-node/PerennialTypes.js';
import getOption from './getOption.js';

// @ts-expect-error - until we have "type": "module" in our package.json
Expand Down
32 changes: 17 additions & 15 deletions js/phet-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
*/

// NOTE: we cannot use IntentionalAny in this file because it is a global file, and IntentionalAny is a module file
// Therefore, we must use `any` directly in this file. See https://github.com/phetsims/perennial/issues/406
// Therefore, we must a custom declaration. See https://github.com/phetsims/perennial/issues/406
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Any = any;

declare var assert: undefined | ( ( x: any, ...messages: any[] ) => void );
declare var assertSlow: undefined | ( ( x: any, ...messages: any[] ) => void );
declare var sceneryLog: null | false | ( Record<string, ( ob: any, style?: string ) => void> & {
declare var assert: undefined | ( ( x: Any, ...messages: Any[] ) => void );
declare var assertSlow: undefined | ( ( x: Any, ...messages: Any[] ) => void );
declare var sceneryLog: null | false | ( Record<string, ( ob: Any, style?: string ) => void> & {
push(): void;
pop(): void;
getDepth(): number;
} );
declare var phet: Record<string, any>;
declare var phet: Record<string, Any>;

// TODO: This can be moved to QueryStringMachine when it is moved to TypeScript, see https://github.com/phetsims/query-string-machine/issues/49
declare type Warning = {
Expand Down Expand Up @@ -58,20 +60,20 @@ declare type QueryStringMachineSchema = {
type: 'array';
elementSchema: QueryStringMachineSchema;
separator?: string;
defaultValue?: null | readonly any[];
validValues?: readonly any[][];
isValidValue?: ( n: any[] ) => boolean;
defaultValue?: null | readonly Any[];
validValues?: readonly Any[][];
isValidValue?: ( n: Any[] ) => boolean;
} |
{
type: 'custom';
parse: ( str: string ) => any;
defaultValue?: any;
validValues?: readonly any[];
isValidValue?: ( n: any ) => boolean;
parse: ( str: string ) => Any;
defaultValue?: Any;
validValues?: readonly Any[];
isValidValue?: ( n: Any ) => boolean;
} );

// Converts a Schema's type to the actual Typescript type it represents
declare type QueryMachineTypeToType<T> = T extends ( 'flag' | 'boolean' ) ? boolean : ( T extends 'number' ? number : ( T extends 'string' ? ( string | null ) : ( T extends 'array' ? any[] : any ) ) );
declare type QueryMachineTypeToType<T> = T extends ( 'flag' | 'boolean' ) ? boolean : ( T extends 'number' ? number : ( T extends 'string' ? ( string | null ) : ( T extends 'array' ? Any[] : Any ) ) );

type QSMSchemaObject = Record<string, QueryStringMachineSchema>;

Expand All @@ -88,7 +90,7 @@ declare var QueryStringMachine: {
get: <Schema extends QueryStringMachineSchema>( a: string, schema: Schema ) => QueryMachineTypeToType<Schema[ 'type' ]>;
containsKey: ( key: string ) => boolean;
warnings: Warning[];
addWarning: ( key: string, value: any, message: string ) => void;
addWarning: ( key: string, value: Any, message: string ) => void;
removeKeyValuePair: ( queryString: string, key: string ) => string;
removeKeyValuePairs: ( queryString: string, keys: string[] ) => string;
appendQueryString: ( url: string, tail: string ) => string;
Expand All @@ -103,7 +105,7 @@ declare var QueryStringMachine: {
declare var phetSplashScreenDownloadComplete: () => void;
declare var TWEEN: { update: ( dt: number ) => void };
declare var phetSplashScreen: { dispose: () => void };
declare var phetio: Record<string, any>;
declare var phetio: Record<string, Any>;

// Typing for linebreaker-1.1.0.js preload
declare type LineBreakerBreak = {
Expand Down

0 comments on commit 4367167

Please sign in to comment.