@@ -26,12 +26,25 @@ const readFilep = promisify(fs.readFile);
2626
2727/** @define {string} */ const MAP_EXT = '.map' ;
2828
29+ /** Represents one source map file. */
2930export interface MapInfoInput {
31+ // The path of generated output file in the source map. For example, if
32+ // "src/index1.ts" and "src/index2.ts" are generated into "dist/index.js" and
33+ // "dist/index.js.map", then this field is "dist/index.js (relative to the
34+ // process's working directory).
3035 outputFile : string ;
36+
37+ // The source map's path (relative to the process's working directory). For
38+ // the same example above, this field is "dist/index.js.map".
3139 mapFile : string ;
40+
41+ // The SourceMapConsumer object after parsing the content in the mapFile.
3242 mapConsumer : sourceMap . SourceMapConsumer ;
33- // the sources are in ascending order from
34- // shortest to longest
43+
44+ // The original sources in the source map. Each value is relative to the
45+ // source map file. For the same example above, this field is
46+ // ['../src/index1.ts', '../src/index2.ts']. the sources are in ascending
47+ // order from shortest to longest.
3548 sources : string [ ] ;
3649}
3750
@@ -41,6 +54,12 @@ export interface MapInfoOutput {
4154 column ?: number ;
4255}
4356
57+ export class MultiFileMatchError implements Error {
58+ readonly name = 'MultiFileMatchError' ;
59+ readonly message = 'Error: matching multiple files' ;
60+ constructor ( readonly files : string [ ] ) { }
61+ }
62+
4463/**
4564 * @param {!Map } infoMap The map that maps input source files to
4665 * SourceMapConsumer objects that are used to calculate mapping information
@@ -167,13 +186,22 @@ export class SourceMapper {
167186 * source file provided there isn't any ambiguity with associating the input
168187 * path to exactly one output transpiled file.
169188 *
170- * @param inputPath The (possibly relative) path to the original source file.
189+ * If there are more than one matches, throw the error to include all the
190+ * matched candidates.
191+ *
192+ * If there is no such mapping, it could be because the input file is not
193+ * the input to a transpilation process or it is the input to a transpilation
194+ * process but its corresponding .map file was not given to the constructor of
195+ * this mapper.
196+ *
197+ * @param inputPath The path to an input file that could possibly be the input
198+ * to a transpilation process.
199+ * The path can be relative to the process's current working directory.
171200 * @return The `MapInfoInput` object that describes the transpiled file
172- * associated with the specified input path. `null` is returned if either
173- * zero files are associated with the input path or if more than one file
174- * could possibly be associated with the given input path.
201+ * associated with the specified input path. `null` is returned if there is
202+ * no files that are associated with the input path.
175203 */
176- private getMappingInfo ( inputPath : string ) : MapInfoInput | null {
204+ getMapInfoInput ( inputPath : string ) : MapInfoInput | null {
177205 if ( this . infoMap . has ( path . normalize ( inputPath ) ) ) {
178206 return this . infoMap . get ( inputPath ) as MapInfoInput ;
179207 }
@@ -182,30 +210,17 @@ export class SourceMapper {
182210 inputPath ,
183211 Array . from ( this . infoMap . keys ( ) )
184212 ) ;
213+
185214 if ( matches . length === 1 ) {
186215 return this . infoMap . get ( matches [ 0 ] ) as MapInfoInput ;
187216 }
217+ if ( matches . length > 1 ) {
218+ throw new MultiFileMatchError ( matches ) ;
219+ }
188220
189221 return null ;
190222 }
191223
192- /**
193- * Used to determine if the source file specified by the given path has
194- * a .map file and an output file associated with it.
195- *
196- * If there is no such mapping, it could be because the input file is not
197- * the input to a transpilation process or it is the input to a transpilation
198- * process but its corresponding .map file was not given to the constructor
199- * of this mapper.
200- *
201- * @param {string } inputPath The path to an input file that could
202- * possibly be the input to a transpilation process. The path should be
203- * relative to the process's current working directory.
204- */
205- hasMappingInfo ( inputPath : string ) : boolean {
206- return this . getMappingInfo ( inputPath ) !== null ;
207- }
208-
209224 /**
210225 * @param {string } inputPath The path to an input file that could possibly
211226 * be the input to a transpilation process. The path should be relative to
@@ -225,20 +240,18 @@ export class SourceMapper {
225240 * If the given input file does not have mapping information associated
226241 * with it then null is returned.
227242 */
228- mappingInfo (
243+ getMapInfoOutput (
229244 inputPath : string ,
230245 lineNumber : number ,
231- colNumber : number
246+ colNumber : number ,
247+ entry : MapInfoInput
232248 ) : MapInfoOutput | null {
233249 inputPath = path . normalize ( inputPath ) ;
234- const entry = this . getMappingInfo ( inputPath ) ;
235- if ( entry === null ) {
236- return null ;
237- }
238250
239251 const relPath = path
240252 . relative ( path . dirname ( entry . mapFile ) , inputPath )
241253 . replace ( / \\ / g, '/' ) ;
254+
242255 /**
243256 * Note: Since `entry.sources` is in ascending order from shortest
244257 * to longest, the first source path that ends with the
0 commit comments