@@ -13,15 +13,18 @@ import BabelPluginReactCompiler, {
1313 type CompilerErrorDetailOptions ,
1414 type PluginOptions ,
1515} from "babel-plugin-react-compiler/src" ;
16- import { LoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint" ;
16+ import { LoggerEvent as RawLoggerEvent } from "babel-plugin-react-compiler/src/Entrypoint" ;
1717import chalk from "chalk" ;
1818
19+ type LoggerEvent = RawLoggerEvent & { filename : string | null } ;
20+
1921const SucessfulCompilation : Array < LoggerEvent > = [ ] ;
2022const ActionableFailures : Array < LoggerEvent > = [ ] ;
2123const OtherFailures : Array < LoggerEvent > = [ ] ;
2224
2325const logger = {
24- logEvent ( _ : string | null , event : LoggerEvent ) {
26+ logEvent ( filename : string | null , rawEvent : RawLoggerEvent ) {
27+ const event = { ...rawEvent , filename} ;
2528 switch ( event . kind ) {
2629 case "CompileSuccess" : {
2730 SucessfulCompilation . push ( event ) ;
@@ -104,6 +107,29 @@ function compile(sourceCode: string, filename: string) {
104107
105108const JsFileExtensionRE = / ( j s | t s | j s x | t s x ) $ / ;
106109
110+ /**
111+ * Counts unique source locations (filename + function definition location)
112+ * in source.
113+ * The compiler currently occasionally emits multiple error events for a
114+ * single file (e.g. to report multiple rules of react violations in the
115+ * same pass).
116+ * TODO: enable non-destructive `CompilerDiagnostic` logging in dev mode,
117+ * and log a "CompilationStart" event for every function we begin processing.
118+ */
119+ function countUniqueLocInEvents ( events : Array < LoggerEvent > ) : number {
120+ const seenLocs = new Set < string > ( ) ;
121+ let count = 0 ;
122+ for ( const e of events ) {
123+ if ( e . filename != null && e . fnLoc != null ) {
124+ seenLocs . add ( `${ e . filename } :${ e . fnLoc . start } :${ e . fnLoc . end } ` ) ;
125+ } else {
126+ // failed to dedup due to lack of source locations
127+ count ++ ;
128+ }
129+ }
130+ return count + seenLocs . size ;
131+ }
132+
107133export default {
108134 run ( source : string , path : string ) : void {
109135 if ( JsFileExtensionRE . exec ( path ) !== null ) {
@@ -114,8 +140,8 @@ export default {
114140 report ( ) : void {
115141 const totalComponents =
116142 SucessfulCompilation . length +
117- OtherFailures . length +
118- ActionableFailures . length ;
143+ countUniqueLocInEvents ( OtherFailures ) +
144+ countUniqueLocInEvents ( ActionableFailures )
119145 console . log (
120146 chalk . green (
121147 `Successfully compiled ${ SucessfulCompilation . length } out of ${ totalComponents } components.`
0 commit comments