Beispiel #1
0
        /// <summary>
        /// Applies the mappings of a sub source map to the current source map
        /// Each mapping to the supplied source file is rewritten using the supplied source map
        /// This is useful in situations where we have a to b to c, with mappings ba.map and cb.map
        /// Calling cb.ApplySourceMap(ba) will return mappings from c to a (ca)
        /// <param name="submap">The submap to apply</param>
        /// <param name="sourceFile">The filename of the source file. If not specified, submap's File property will be used</param>
        /// <returns>A new source map</returns>
        /// </summary>
        public SourceMap ApplySourceMap(SourceMap submap, string sourceFile = null)
        {
            if (submap == null)
            {
                throw new ArgumentNullException(nameof(submap));
            }

            if (sourceFile == null)
            {
                if (submap.File == null)
                {
                    throw new Exception("ApplySourceMap expects either the explicit source file to the map, or submap's 'file' property");
                }

                sourceFile = submap.File;
            }

            SourceMap newSourceMap = new SourceMap
            {
                File           = this.File,
                Version        = this.Version,
                Sources        = new List <string>(),
                Names          = new List <string>(),
                SourcesContent = new List <string>(),
                ParsedMappings = new List <MappingEntry>()
            };

            // transform mappings in this source map
            foreach (MappingEntry mappingEntry in this.ParsedMappings)
            {
                MappingEntry newMappingEntry = mappingEntry.Clone();

                if (mappingEntry.OriginalFileName == sourceFile && mappingEntry.OriginalSourcePosition != null)
                {
                    MappingEntry correspondingSubMapMappingEntry = submap.GetMappingEntryForGeneratedSourcePosition(mappingEntry.OriginalSourcePosition);

                    if (correspondingSubMapMappingEntry != null)
                    {
                        // Copy the mapping
                        newMappingEntry = new MappingEntry
                        {
                            GeneratedSourcePosition = mappingEntry.GeneratedSourcePosition.Clone(),
                            OriginalSourcePosition  = correspondingSubMapMappingEntry.OriginalSourcePosition.Clone(),
                            OriginalName            = correspondingSubMapMappingEntry.OriginalName ?? mappingEntry.OriginalName,
                            OriginalFileName        = correspondingSubMapMappingEntry.OriginalFileName ?? mappingEntry.OriginalFileName
                        };
                    }
                }

                // Copy into "Sources" and "Names"
                string originalFileName = newMappingEntry.OriginalFileName;
                string originalName     = newMappingEntry.OriginalName;

                if (originalFileName != null && !newSourceMap.Sources.Contains(originalFileName))
                {
                    newSourceMap.Sources.Add(originalFileName);
                }

                if (originalName != null && !newSourceMap.Names.Contains(originalName))
                {
                    newSourceMap.Names.Add(originalName);
                }

                newSourceMap.ParsedMappings.Add(newMappingEntry);
            }
            ;

            return(newSourceMap);
        }
Beispiel #2
0
        /// <summary>
        /// Applies the mappings of a sub source map to the current source map
        /// Each mapping to the supplied source file is rewritten using the supplied source map
        /// This is useful in situations where we have a to b to c, with mappings ba.map and cb.map
        /// Calling cb.ApplySourceMap(ba) will return mappings from c to a (ca)
        /// <param name="submap">The submap to apply</param>
        /// <param name="sourceFile">The filename of the source file. If not specified, submap's File property will be used</param>
        /// <returns>A new source map</returns>
        /// </summary>
        public SourceMap ApplySourceMap(SourceMap submap, string sourceFile = null)
        {
            if (submap == null)
            {
                throw new ArgumentNullException(nameof(submap));
            }

            if (sourceFile == null)
            {
                if (submap.File == null)
                {
                    throw new Exception($"{nameof(ApplySourceMap)} expects either the explicit source file to the map, or submap's 'file' property");
                }

                sourceFile = submap.File;
            }

            HashSet <string>    sources        = new HashSet <string>(StringComparer.Ordinal);
            HashSet <string>    names          = new HashSet <string>(StringComparer.Ordinal);
            List <MappingEntry> parsedMappings = new List <MappingEntry>(this.ParsedMappings.Count);

            // transform mappings in this source map
            foreach (MappingEntry mappingEntry in this.ParsedMappings)
            {
                MappingEntry newMappingEntry = mappingEntry;

                if (mappingEntry.OriginalFileName == sourceFile && mappingEntry.OriginalSourcePosition != SourcePosition.NotFound)
                {
                    MappingEntry?correspondingSubMapMappingEntry = submap.GetMappingEntryForGeneratedSourcePosition(mappingEntry.OriginalSourcePosition);

                    if (correspondingSubMapMappingEntry != null)
                    {
                        // Copy the mapping
                        newMappingEntry = new MappingEntry(
                            generatedSourcePosition: mappingEntry.GeneratedSourcePosition,
                            originalSourcePosition: correspondingSubMapMappingEntry.Value.OriginalSourcePosition,
                            originalName: correspondingSubMapMappingEntry.Value.OriginalName ?? mappingEntry.OriginalName,
                            originalFileName: correspondingSubMapMappingEntry.Value.OriginalFileName ?? mappingEntry.OriginalFileName);
                    }
                }

                // Copy into "Sources" and "Names"
                string originalFileName = newMappingEntry.OriginalFileName;
                string originalName     = newMappingEntry.OriginalName;

                if (originalFileName != null)
                {
                    sources.Add(originalFileName);
                }

                if (originalName != null)
                {
                    names.Add(originalName);
                }

                parsedMappings.Add(newMappingEntry);
            }

            SourceMap newSourceMap = new SourceMap(
                version: Version,
                file: File,
                mappings: default,