11/*
2- Copyright 2012-2015, Yahoo Inc.
3- Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4- */
5- import { transformSync } from "@babel/core" ;
6- import type { FileResult , InputOptions , NodePath , PluginObject , types as t } from "@babel/core" ;
7- import type { ParserPlugin } from "@babel/parser" ;
2+ Copyright 2012-2015, Yahoo Inc.
3+ Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
4+ */
85import { defaults } from "@istanbuljs/schema" ;
6+ import remapping from "@jridgewell/remapping" ;
7+ import type { EncodedSourceMap } from "@jridgewell/trace-mapping" ;
8+ import MagicString from "magic-string" ;
99
10+ import { parse } from "./parse" ;
1011import readInitialCoverage from "./read-coverage" ;
1112import type { InputSourceMap , SourceCoverageData } from "./source-coverage" ;
12- import programVisitor from "./visitor" ;
13- import type { VisitorExitResult } from "./visitor" ;
13+ import instrumentProgram from "./visitor" ;
1414
15- /** source map produced by babel's transform */
16- type SourceMap = NonNullable < FileResult [ "map" ] > ;
15+ /** source map produced for the instrumented code */
16+ export type SourceMap = EncodedSourceMap ;
1717
1818/** options accepted by {@link Instrumenter} and {@link createInstrumenter} */
1919export interface InstrumenterOptions {
2020 /** name of global coverage variable (default `__coverage__`) */
2121 coverageVariable : string ;
2222 /** report boolean value of logical expressions (default `false`) */
2323 reportLogic : boolean ;
24- /** preserve comments in output (default `false`) */
24+ /**
25+ * @deprecated has no effect, comments of the original code are always preserved
26+ */
2527 preserveComments : boolean ;
26- /** generate compact code (default `true`) */
28+ /**
29+ * @deprecated has no effect, the original code is preserved as-is
30+ */
2731 compact : boolean ;
2832 /** set to true to instrument ES6 modules */
2933 esModules : boolean ;
@@ -41,26 +45,23 @@ export interface InstrumenterOptions {
4145 * and the source map URL.
4246 */
4347 sourceMapUrlCallback : ( ( filename : string , url : string ) => void ) | null ;
44- /** turn debugging on (default `false`) */
48+ /**
49+ * @deprecated has no effect
50+ */
4551 debug : boolean ;
46- /** set babel parser plugins, see @istanbuljs/schema for defaults */
47- parserPlugins : ParserPlugin [ ] ;
4852 /** the global coverage variable scope (default `this`) */
4953 coverageGlobalScope : string ;
5054 /** use an evaluated function to find coverageGlobalScope (default `true`) */
5155 coverageGlobalScopeFunc : boolean ;
52- /** set babel generator options */
53- generatorOpts ?: Record < string , unknown > ;
5456}
5557
5658/** callback for the callback-style {@link Instrumenter#instrument} method */
5759export type InstrumenterCallback = ( error : Error | null , code ?: string ) => void ;
5860
5961/**
6062 * Instrumenter is the public API for the instrument library.
61- * It is typically used for ES5 code. For ES6 code that you
62- * are already running under `babel` use the coverage plugin
63- * instead.
63+ * The code is parsed with OXC and coverage counters are inserted
64+ * into the original code with `magic-string`.
6465 * @param opts optional. See {@link InstrumenterOptions}.
6566 */
6667class Instrumenter {
@@ -95,67 +96,35 @@ class Instrumenter {
9596 }
9697 filename = filename || String ( new Date ( ) . getTime ( ) ) + ".js" ;
9798 const { opts } = this ;
98- let output : VisitorExitResult | undefined ;
99- const babelOpts : InputOptions = {
100- configFile : false ,
101- babelrc : false ,
102- ast : true ,
103- filename : filename || String ( new Date ( ) . getTime ( ) ) + ".js" ,
104- // trace-mapping allows `sources: (string | null)[]`, babel's types don't — same runtime shape
105- inputSourceMap : inputSourceMap as InputOptions [ "inputSourceMap" ] ,
106- sourceMaps : opts . produceSourceMap ,
107- compact : opts . compact ,
108- comments : opts . preserveComments ,
109- parserOpts : {
110- allowReturnOutsideFunction : opts . autoWrap ,
111- sourceType : opts . esModules ? "module" : "script" ,
112- plugins : opts . parserPlugins ,
113- } ,
114- generatorOpts : opts . generatorOpts ,
115- plugins : [
116- ( { types } : { types : typeof t } ) : PluginObject => {
117- const ee = programVisitor ( types , filename as string , {
118- coverageVariable : opts . coverageVariable ,
119- reportLogic : opts . reportLogic ,
120- coverageGlobalScope : opts . coverageGlobalScope ,
121- coverageGlobalScopeFunc : opts . coverageGlobalScopeFunc ,
122- ignoreClassMethods : opts . ignoreClassMethods ,
123- ignoreLines : opts . ignoreLines ,
124- inputSourceMap,
125- } ) ;
99+ const { program, comments } = parse ( code , filename , opts ) ;
100+ const s = new MagicString ( code ) ;
126101
127- return {
128- visitor : {
129- Program : {
130- enter : ee . enter ,
131- exit ( path : NodePath < t . Program > ) {
132- output = ee . exit ( path ) ;
133- } ,
134- } ,
135- } ,
136- } ;
137- } ,
138- ] ,
139- } ;
140-
141- const codeMap = transformSync ( code , babelOpts ) ! ;
102+ const output = instrumentProgram ( s , program , comments , filename , {
103+ coverageVariable : opts . coverageVariable ,
104+ reportLogic : opts . reportLogic ,
105+ coverageGlobalScope : opts . coverageGlobalScope ,
106+ coverageGlobalScopeFunc : opts . coverageGlobalScopeFunc ,
107+ ignoreClassMethods : opts . ignoreClassMethods ,
108+ ignoreLines : opts . ignoreLines ,
109+ inputSourceMap,
110+ } ) ;
142111
143- if ( ! output || ! output . fileCoverage ) {
112+ if ( ! output ) {
144113 const initialCoverage =
145- readInitialCoverage ( codeMap . ast ! ) || /* istanbul ignore next: paranoid check */ { } ;
114+ readInitialCoverage ( program ) || /* istanbul ignore next: paranoid check */ { } ;
146115 this . fileCoverage = ( initialCoverage as { coverageData ?: SourceCoverageData } ) . coverageData ;
147116 this . sourceMap = inputSourceMap ;
148117 return code ;
149118 }
150119
151120 this . fileCoverage = output . fileCoverage ;
152- this . sourceMap = codeMap . map ;
121+ this . sourceMap = opts . produceSourceMap ? createSourceMap ( s , filename , inputSourceMap ) : null ;
153122 const cb = this . opts . sourceMapUrlCallback ;
154123 if ( cb && output . sourceMappingURL ) {
155124 cb ( filename , output . sourceMappingURL ) ;
156125 }
157126
158- return codeMap . code as string ;
127+ return s . toString ( ) ;
159128 }
160129 /**
161130 * callback-style instrument method that calls back with an error
@@ -202,4 +171,34 @@ class Instrumenter {
202171 }
203172}
204173
174+ // maps the instrumented code to the input code, and further to the original
175+ // sources when the input code has a source map
176+ function createSourceMap (
177+ s : MagicString ,
178+ filename : string ,
179+ inputSourceMap ?: InputSourceMap ,
180+ ) : SourceMap {
181+ const map = s . generateMap ( { hires : "boundary" , source : filename , includeContent : true } ) ;
182+ if ( ! inputSourceMap ) {
183+ return {
184+ version : 3 ,
185+ names : map . names ,
186+ sources : map . sources ,
187+ sourcesContent : map . sourcesContent ,
188+ mappings : map . mappings ,
189+ } ;
190+ }
191+ const remapped = remapping ( [ map as EncodedSourceMap , inputSourceMap ] , ( ) => null ) ;
192+ return {
193+ version : 3 ,
194+ names : remapped . names ,
195+ sources : remapped . sources ,
196+ sourcesContent : remapped . sourcesContent ,
197+ mappings : remapped . mappings as string ,
198+ ...( remapped . ignoreList && remapped . ignoreList . length > 0
199+ ? { ignoreList : remapped . ignoreList }
200+ : { } ) ,
201+ } ;
202+ }
203+
205204export default Instrumenter ;
0 commit comments