1- // Catches: the two simctl scope bypasses `tsc` cannot reject. (1) A hand-built argv at an xcrun
2- // invocation: `runXcrun` accepts only a `ScopedSimctlCommand` or a named non-simctl tool, but
3- // the plain executors (`runCmd('xcrun', …)`, `runCmdBackground('xcrun', …)`, an
4- // `executable: 'xcrun'` spec) take any string argv, so an inline argv there must name its tool
5- // as a string literal other than `simctl`. (2) A cast to `ScopedSimctlArgs`,
6- // `ScopedSimctlCommand` or `SimulatorAddress` outside the modules that mint them. Either form
7- // runs against the default CoreSimulator set: `Invalid device` for a simulator in a scoped set,
8- // or a different simulator with the same udid.
1+ // Catches: the simctl scope bypasses `tsc` cannot reject. `runXcrun` accepts only a
2+ // `ScopedSimctlCommand` or a named non-simctl tool, but the plain executors (`runCmd`,
3+ // `runCmdBackground`, an `executable: 'xcrun'` spec) take any string argv, so every simctl argv
4+ // reaching them must be builder output. (1) An array whose first element names simctl (the
5+ // literal, a quasi-only template, or a same-file binding of either) outside the brand mints,
6+ // however it later travels: inline, held in a variable, aliased or spread. (2) At an xcrun
7+ // invocation, an inline argv whose tool is not a string literal other than `simctl`. (3) A cast
8+ // to `ScopedSimctlArgs`, `ScopedSimctlCommand` or `SimulatorAddress` outside the brand mints.
9+ // Every form runs against the default CoreSimulator set: `Invalid device` for a simulator in a
10+ // scoped set, or a different simulator with the same udid.
911// Evidence: #2784 (fixed by #2818): the AX snapshot bridge (`snapshot-source/host.ts`) and the
1012// fold HID helper (`foldable/simulator-hid.ts`) built `['simctl', 'spawn', udid, ...]` from a
1113// bare udid and lost the set; #2824 moved every call site onto `core/simctl.ts` and checked it
1214// with a manual `git grep "'--set'"`.
13- // Cost: 225 LOC (109 rule + 116 test).
15+ // Cost: 300 LOC (159 rule + 141 test).
1416// Kill criterion: none enforced today; retire only by maintainer decision that scoped simulator
1517// sets (`--ios-simulator-device-set`) are no longer supported, or when no production xcrun
16- // invocation takes a plain string argv.
18+ // executor takes a plain string argv.
1719
1820import { parseSync } from 'oxc-parser' ;
1921import { propertyName , visitAst } from './layering-ast.ts' ;
@@ -30,6 +32,9 @@ const BRAND_MINTS = new Set([
3032] ) ;
3133const BRANDS = new Set ( [ 'ScopedSimctlArgs' , 'ScopedSimctlCommand' , 'SimulatorAddress' ] ) ;
3234
35+ const SIMCTL_ARGV_MESSAGE =
36+ 'builds a simctl argv outside core/simctl.ts and core/tool-provider.ts; build it with ' +
37+ 'buildSimctlArgsForDevice or buildSimctlArgsForAddress' ;
3338const HAND_BUILT_MESSAGE =
3439 'hands xcrun an argv whose tool is not a literal non-simctl name; build a simctl argv with ' +
3540 'buildSimctlArgsForDevice or buildSimctlArgsForAddress' ;
@@ -53,14 +58,31 @@ export function appleSimulatorScopeViolations(
5358 const violations : LayeringViolation [ ] = [ ] ;
5459 for ( const [ file , source ] of sources ) {
5560 if ( ! isPolicedSimulatorScopeFile ( file ) ) continue ;
61+ const program = parseSync ( file , source ) . program ;
62+ const simctlBindings = simctlNameBindings ( program ) ;
63+ const isMint = BRAND_MINTS . has ( file ) ;
64+ const buildsSimctlArgv = ( array : AstNode ) => {
65+ const tool = firstElement ( array ) ;
66+ return (
67+ ! isMint &&
68+ ( namesSimctl ( tool ) || ( tool ?. type === 'Identifier' && simctlBindings . has ( tool . name ) ) )
69+ ) ;
70+ } ;
5671 const report = ( node : AstNode , message : string ) =>
5772 violations . push ( { rule : RULE , file, line : lineAt ( source , node . start ) , message } ) ;
58- visitAst ( parseSync ( file , source ) . program , ( node ) => {
73+ visitAst ( program , ( node ) => {
74+ if ( node . type === 'ArrayExpression' && buildsSimctlArgv ( node ) ) {
75+ report ( node , SIMCTL_ARGV_MESSAGE ) ;
76+ }
5977 const argv = xcrunArgv ( node ) ;
60- if ( argv ?. type === 'ArrayExpression' && ! namesNonSimctlTool ( argv ) ) {
78+ if (
79+ argv ?. type === 'ArrayExpression' &&
80+ ! buildsSimctlArgv ( argv ) &&
81+ ! namesNonSimctlTool ( argv )
82+ ) {
6183 report ( argv , HAND_BUILT_MESSAGE ) ;
6284 }
63- if ( ! BRAND_MINTS . has ( file ) && castsToBrand ( node ) ) report ( node , FORGED_MESSAGE ) ;
85+ if ( ! isMint && castsToBrand ( node ) ) report ( node , FORGED_MESSAGE ) ;
6486 } ) ;
6587 }
6688 return violations ;
@@ -82,11 +104,39 @@ function xcrunArgv(node: AstNode): AstNode | undefined {
82104
83105/** A spread passes an argv through like a variable; any other first element must name the tool. */
84106function namesNonSimctlTool ( argv : AstNode ) : boolean {
85- const tool = ( argv . elements as AstNode [ ] ) [ 0 ] ;
107+ const tool = firstElement ( argv ) ;
86108 if ( tool ?. type === 'SpreadElement' ) return true ;
87109 return tool ?. type === 'Literal' && typeof tool . value === 'string' && tool . value !== 'simctl' ;
88110}
89111
112+ function firstElement ( array : AstNode ) : AstNode | undefined {
113+ return ( array . elements as ( AstNode | null ) [ ] ) [ 0 ] ?? undefined ;
114+ }
115+
116+ /** `'simctl'` or `` `simctl` ``, the two spellings of the tool name as a constant. */
117+ function namesSimctl ( node : AstNode | undefined ) : boolean {
118+ if ( isLiteral ( node , 'simctl' ) ) return true ;
119+ if ( node ?. type !== 'TemplateLiteral' || ( node . expressions as unknown [ ] ) . length > 0 ) return false ;
120+ const [ quasi ] = node . quasis as AstNode [ ] ;
121+ return ( quasi ?. value as { cooked ?: string } | undefined ) ?. cooked === 'simctl' ;
122+ }
123+
124+ /** Names bound anywhere in the file to a constant spelling of `simctl`, whatever their scope. */
125+ function simctlNameBindings ( program : unknown ) : Set < unknown > {
126+ const bindings = new Set < unknown > ( ) ;
127+ visitAst ( program , ( node ) => {
128+ const id = node . id as AstNode | undefined ;
129+ if (
130+ node . type === 'VariableDeclarator' &&
131+ id ?. type === 'Identifier' &&
132+ namesSimctl ( node . init as AstNode )
133+ ) {
134+ bindings . add ( id . name ) ;
135+ }
136+ } ) ;
137+ return bindings ;
138+ }
139+
90140function castsToBrand ( node : AstNode ) : boolean {
91141 if ( node . type !== 'TSAsExpression' && node . type !== 'TSTypeAssertion' ) return false ;
92142 let names = false ;
0 commit comments