@@ -304,6 +304,8 @@ export function planMove(source, member, moduleFile, className = CLASS_NAME) {
304304 if ( ! cls ) return { refusals : [ { line : 0 , reason : `no class ${ className } ` } ] } ;
305305 const method = methodOf ( cls , member ) ;
306306 if ( ! method ) return { refusals : [ { line : 0 , reason : `no instance method ${ member } with a body` } ] } ;
307+ const already = stubKindOf ( method ) ;
308+ if ( already ) return { refusals : [ { line : 0 , reason : `already a stub ('${ already } '): its body moved before` } ] } ;
307309 const table = memberTable ( cls ) ;
308310 const { sites, refusals, used } = rewriteSites ( sf , method , table , className ) ;
309311 const sig = signatureParts ( sf , method ) ;
@@ -526,6 +528,41 @@ export function checkMove({ baseSource, headSource, moduleSource, moduleFile, me
526528
527529// ── Survey ──────────────────────────────────────────────────────────────────
528530
531+ /**
532+ * The function a per-member stub calls: `fn` in `fn.call(host, …)`, and in the
533+ * generic spelling `(fn<R>).call(host, …)` (an instantiation expression in
534+ * parentheses), which the per-member series wrote for a generic method.
535+ */
536+ function calleeOf ( node ) {
537+ let n = node ;
538+ while ( ts . isParenthesizedExpression ( n ) || ts . isExpressionWithTypeArguments ( n ) ) n = n . expression ;
539+ return n ;
540+ }
541+
542+ /**
543+ * Is this method already a stub — its body moved before? One of three shapes,
544+ * its last statement returning (awaited or not):
545+ * 'call' — `fn.call(host, …)` or `(fn<R>).call(host, …)` (the per-member series);
546+ * 'host' — `fn(protocolHost(this), …)` (this tool);
547+ * 'plain' — `fn(<its own parameters, in order>)`, `fn` named as the method:
548+ * a body that read no member moved as a plain function.
549+ */
550+ export function stubKindOf ( m ) {
551+ const last = m . body ?. statements [ m . body . statements . length - 1 ] ;
552+ let call = last && ts . isReturnStatement ( last ) ? last . expression : undefined ;
553+ if ( call && ts . isAwaitExpression ( call ) ) call = call . expression ;
554+ if ( ! call || ! ts . isCallExpression ( call ) ) return undefined ;
555+ if ( ts . isPropertyAccessExpression ( call . expression ) && call . expression . name . text === 'call'
556+ && ts . isIdentifier ( calleeOf ( call . expression . expression ) ) && ! ! call . arguments [ 0 ] && ts . isIdentifier ( call . arguments [ 0 ] ) ) return 'call' ;
557+ if ( call . arguments [ 0 ] && ts . isCallExpression ( call . arguments [ 0 ] ) && ts . isIdentifier ( call . arguments [ 0 ] . expression )
558+ && call . arguments [ 0 ] . expression . text === HOST_OF ) return 'host' ;
559+ const name = nameOf ( m ) ;
560+ const params = m . parameters . map ( ( p ) => ( ts . isIdentifier ( p . name ) ? p . name . text : undefined ) ) ;
561+ if ( ts . isIdentifier ( call . expression ) && call . expression . text === name && call . arguments . length === params . length
562+ && call . arguments . every ( ( x , i ) => ts . isIdentifier ( x ) && x . text === params [ i ] ) ) return 'plain' ;
563+ return undefined ;
564+ }
565+
529566/** Every instance method with a body: its line count, sites and refusals under the set. */
530567export function survey ( source , className = CLASS_NAME ) {
531568 const sf = parse ( 'protocol.ts' , source ) ;
@@ -536,14 +573,7 @@ export function survey(source, className = CLASS_NAME) {
536573 if ( ! ts . isMethodDeclaration ( m ) || ! m . body || hasModifier ( m , ts . SyntaxKind . StaticKeyword ) ) continue ;
537574 const name = nameOf ( m ) ;
538575 const { sites, refusals } = rewriteSites ( sf , m , table , className ) ;
539- const last = m . body . statements [ m . body . statements . length - 1 ] ;
540- let call = last && ts . isReturnStatement ( last ) ? last . expression : undefined ;
541- if ( call && ts . isAwaitExpression ( call ) ) call = call . expression ;
542- const stub = ! ! call && ts . isCallExpression ( call ) && ts . isPropertyAccessExpression ( call . expression )
543- && call . expression . name . text === 'call' && ts . isIdentifier ( call . expression . expression )
544- && ! ! call . arguments [ 0 ] && ts . isIdentifier ( call . arguments [ 0 ] ) ? 'call'
545- : ! ! call && ts . isCallExpression ( call ) && call . arguments [ 0 ] && ts . isCallExpression ( call . arguments [ 0 ] )
546- && ts . isIdentifier ( call . arguments [ 0 ] . expression ) && call . arguments [ 0 ] . expression . text === HOST_OF ? 'host' : undefined ;
576+ const stub = stubKindOf ( m ) ;
547577 const lines = sf . getLineAndCharacterOfPosition ( m . getEnd ( ) ) . line - sf . getLineAndCharacterOfPosition ( m . getStart ( sf ) ) . line + 1 ;
548578 rows . push ( { name, lines, sites : sites . length , refusals, stub } ) ;
549579 }
@@ -593,7 +623,7 @@ function main(argv) {
593623 const all = survey ( readFileSync ( PROTOCOL , 'utf8' ) ) ;
594624 const { clashes } = all ;
595625 const stubs = all . rows . filter ( ( r ) => r . stub ) ;
596- console . log ( `already stubs: ${ stubs . length } (${ stubs . filter ( ( r ) => r . stub === 'call' ) . length } by \`fn.call(host, …)\` from the per-member series, ${ stubs . filter ( ( r ) => r . stub === 'host' ) . length } by \`fn(${ HOST_OF } (this), …)\`)` ) ;
626+ console . log ( `already stubs: ${ stubs . length } (${ stubs . filter ( ( r ) => r . stub === 'call' ) . length } by \`fn.call(host, …)\` from the per-member series, ${ stubs . filter ( ( r ) => r . stub === 'host' ) . length } by \`fn(${ HOST_OF } (this), …)\`, ${ stubs . filter ( ( r ) => r . stub === 'plain' ) . length } by a plain \`fn(<params>)\` )` ) ;
597627 const rows = all . rows . filter ( ( r ) => ! r . stub ) ;
598628 const movable = rows . filter ( ( r ) => ! r . refusals . length ) ;
599629 const reasons = new Map ( ) ;
0 commit comments