@@ -13,15 +13,18 @@ import BabelPluginReactCompiler, {
13
13
type CompilerErrorDetailOptions ,
14
14
type PluginOptions ,
15
15
} 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" ;
17
17
import chalk from "chalk" ;
18
18
19
+ type LoggerEvent = RawLoggerEvent & { filename : string | null } ;
20
+
19
21
const SucessfulCompilation : Array < LoggerEvent > = [ ] ;
20
22
const ActionableFailures : Array < LoggerEvent > = [ ] ;
21
23
const OtherFailures : Array < LoggerEvent > = [ ] ;
22
24
23
25
const logger = {
24
- logEvent ( _ : string | null , event : LoggerEvent ) {
26
+ logEvent ( filename : string | null , rawEvent : RawLoggerEvent ) {
27
+ const event = { ...rawEvent , filename} ;
25
28
switch ( event . kind ) {
26
29
case "CompileSuccess" : {
27
30
SucessfulCompilation . push ( event ) ;
@@ -104,6 +107,29 @@ function compile(sourceCode: string, filename: string) {
104
107
105
108
const JsFileExtensionRE = / ( j s | t s | j s x | t s x ) $ / ;
106
109
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
+
107
133
export default {
108
134
run ( source : string , path : string ) : void {
109
135
if ( JsFileExtensionRE . exec ( path ) !== null ) {
@@ -114,8 +140,8 @@ export default {
114
140
report ( ) : void {
115
141
const totalComponents =
116
142
SucessfulCompilation . length +
117
- OtherFailures . length +
118
- ActionableFailures . length ;
143
+ countUniqueLocInEvents ( OtherFailures ) +
144
+ countUniqueLocInEvents ( ActionableFailures )
119
145
console . log (
120
146
chalk . green (
121
147
`Successfully compiled ${ SucessfulCompilation . length } out of ${ totalComponents } components.`
0 commit comments