<commit_before>/*
 * Copyright 2011 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.debugging.sourcemap;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.nullToEmpty;

import com.google.common.base.Preconditions;
import com.google.debugging.sourcemap.SourceMapConsumerV3.EntryVisitor;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;

/**
 * Collects information mapping the generated (compiled) source back to
 * its original source for debugging purposes.
 *
 * Source Map Revision 3 Proposal:
 * https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?usp=sharing
 */
public final class SourceMapGeneratorV3 implements SourceMapGenerator {

  /**
   * This interface provides the merging strategy when an extension conflict
   * appears because of merging two source maps on method
   * {@link #mergeMapSection}.
   */
  public interface ExtensionMergeAction {

    /**
     * Returns the merged value between two extensions with the same name when
     * merging two source maps
     *
     * @param extensionKey The extension name in conflict
     * @param currentValue The extension value in the current source map
     * @param newValue The extension value in the input source map
     * @return The merged value
     */
    Object merge(String extensionKey, Object currentValue,
        Object newValue);
  }

  private static final int UNMAPPED = -1;


  /**
   * A pre-order traversal ordered list of mappings stored in this map.
   */
  private final List<Mapping> mappings = new ArrayList<>();

  /**
   * A map of source names to source name index
   */
  private final LinkedHashMap<String, Integer> sourceFileMap =
       new LinkedHashMap<>();

  /**
   * A map of source names to source file contents
   */
  private final LinkedHashMap<String, String> sourceFileContentMap =
       new LinkedHashMap<>();

  /**
   * A map of source names to source name index
   */
  private final LinkedHashMap<String, Integer> originalNameMap =
       new LinkedHashMap<>();

  /**
   * Cache of the last mappings source name.
   */
  private String lastSourceFile = null;

  /**
   * Cache of the last mappings source name index.
   */
  private int lastSourceFileIndex = -1;

  /**
   * For validation store the last mapping added.
   */
  private Mapping lastMapping;

  /**
   * The position that the current source map is offset in the
   * buffer being used to generated the compiled source file.
   */
  private FilePosition offsetPosition = new FilePosition(0, 0);

  /**
   * The position that the current source map is offset in the
   * generated the compiled source file by the addition of a
   * an output wrapper prefix.
   */
  private FilePosition prefixPosition = new FilePosition(0, 0);

  /**
   * A list of extensions to be added to sourcemap. The value is a object
   * to permit single values, like strings or numbers, and JsonObject or
   * JsonArray objects.
   */
  private final LinkedHashMap<String, Object> extensions = new LinkedHashMap<>();

  /**
   * The source root path for relocating source fails or avoid duplicate values
   * on the source entry.
   */
  private String sourceRootPath;

  /**
   * {@inheritDoc}
   */
  @Override
  public void reset() {
    mappings.clear();
    lastMapping = null;
    sourceFileMap.clear();
    sourceFileContentMap.clear();
    originalNameMap.clear();
    lastSourceFile = null;
    lastSourceFileIndex = -1;
    offsetPosition = new FilePosition(0, 0);
    prefixPosition = new FilePosition(0, 0);
  }

  /**
   * @param validate Whether to perform (potentially costly) validation on the
   * generated source map.
   */
  @Override
  public void validate(boolean validate) {
    // Nothing currently.
  }

  /**<commit_msg>Do not clear sourceFileContentMap when resetting a SourceMapGeneratorV3.<commit_after>/*
 * Copyright 2011 The Closure Compiler Authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.debugging.sourcemap;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Strings.nullToEmpty;

import com.google.common.base.Preconditions;
import com.google.debugging.sourcemap.SourceMapConsumerV3.EntryVisitor;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import javax.annotation.Nullable;

/**
 * Collects information mapping the generated (compiled) source back to
 * its original source for debugging purposes.
 *
 * Source Map Revision 3 Proposal:
 * https://docs.google.com/document/d/1U1RGAehQwRypUTovF1KRlpiOFze0b-_2gc6fAH0KY0k/edit?usp=sharing
 */
public final class SourceMapGeneratorV3 implements SourceMapGenerator {

  /**
   * This interface provides the merging strategy when an extension conflict
   * appears because of merging two source maps on method
   * {@link #mergeMapSection}.
   */
  public interface ExtensionMergeAction {

    /**
     * Returns the merged value between two extensions with the same name when
     * merging two source maps
     *
     * @param extensionKey The extension name in conflict
     * @param currentValue The extension value in the current source map
     * @param newValue The extension value in the input source map
     * @return The merged value
     */
    Object merge(String extensionKey, Object currentValue,
        Object newValue);
  }

  private static final int UNMAPPED = -1;


  /**
   * A pre-order traversal ordered list of mappings stored in this map.
   */
  private final List<Mapping> mappings = new ArrayList<>();

  /**
   * A map of source names to source name index
   */
  private final LinkedHashMap<String, Integer> sourceFileMap =
       new LinkedHashMap<>();

  /**
   * A map of source names to source file contents
   */
  private final LinkedHashMap<String, String> sourceFileContentMap =
       new LinkedHashMap<>();

  /**
   * A map of source names to source name index
   */
  private final LinkedHashMap<String, Integer> originalNameMap =
       new LinkedHashMap<>();

  /**
   * Cache of the last mappings source name.
   */
  private String lastSourceFile = null;

  /**
   * Cache of the last mappings source name index.
   */
  private int lastSourceFileIndex = -1;

  /**
   * For validation store the last mapping added.
   */
  private Mapping lastMapping;

  /**
   * The position that the current source map is offset in the
   * buffer being used to generated the compiled source file.
   */
  private FilePosition offsetPosition = new FilePosition(0, 0);

  /**
   * The position that the current source map is offset in the
   * generated the compiled source file by the addition of a
   * an output wrapper prefix.
   */
  private FilePosition prefixPosition = new FilePosition(0, 0);

  /**
   * A list of extensions to be added to sourcemap. The value is a object
   * to permit single values, like strings or numbers, and JsonObject or
   * JsonArray objects.
   */
  private final LinkedHashMap<String, Object> extensions = new LinkedHashMap<>();

  /**
   * The source root path for relocating source fails or avoid duplicate values
   * on the source entry.
   */
  private String sourceRootPath;

  /**
   * {@inheritDoc}
   */
  @Override
  public void reset() {
    mappings.clear();
    lastMapping = null;
    sourceFileMap.clear();
    originalNameMap.clear();
    lastSourceFile = null;
    lastSourceFileIndex = -1;
    offsetPosition = new FilePosition(0, 0);
    prefixPosition = new FilePosition(0, 0);
  }

  /**
   * @param validate Whether to perform (potentially costly) validation on the
   * generated source map.
   */
  @Override
  public void validate(boolean validate) {
    // Nothing currently.
  }

  /**
{'fraction_non_alphanumeric': 0.08314412368904746, 'fraction_numerical': 0.006595307600821711, 'mean_word_length': 3.3818095689246803, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2651122', 'n_tokens_mistral': 2529, 'n_tokens_neox': 2454, 'n_words': 968}