src/com/google/javascript/jscomp/AbstractCommandLineRunner.java
/**
   * Create a map of constant names to constant values from a textual description of the map.
   *
   * @param definitions A list of overriding definitions for defines in the form {@code
   *     <name>[=<val>]}, where {@code <val>} is a number, boolean, or single-quoted string without
   *     single quotes.
   */
Create a map of constant names to constant values from a textual description of the map.
@param definitions A list of overriding definitions for defines in the form {@code
[=]}, where   is a number, boolean, or single-quoted string without
single quotes.
Create a map of constant names to constant values from a textual description of the map . @param definitions A list of overriding definitions for defines in the form { @code [ = ] } where is a number boolean or single - quoted string without single quotes .
@VisibleForTesting
  public static void createDefineOrTweakReplacements(
      List<String> definitions, CompilerOptions options, boolean tweaks) {
    for (String override : definitions) {
      String[] assignment = override.split("=", 2);
      String defName = assignment[0];
      if (defName.length() > 0) {
        String defValue = assignment.length == 1 ? "true" : assignment[1];
        boolean isTrue = defValue.equals("true");
        boolean isFalse = defValue.equals("false");
        if (isTrue || isFalse) {
          if (tweaks) {
            options.setTweakToBooleanLiteral(defName, isTrue);
          } else {
            options.setDefineToBooleanLiteral(defName, isTrue);
          }
          continue;
        } else if (defValue.length() > 1
            && ((defValue.charAt(0) == '\'' &&
                defValue.charAt(defValue.length() - 1) == '\'')
                || (defValue.charAt(0) == '\"' &&
                    defValue.charAt(defValue.length() - 1) == '\"'))) {
          String maybeStringVal =
              defValue.substring(1, defValue.length() - 1);
          if (maybeStringVal.indexOf(defValue.charAt(0)) == -1) {
            if (tweaks) {
              options.setTweakToStringLiteral(defName, maybeStringVal);
            } else {
              options.setDefineToStringLiteral(defName, maybeStringVal);
            }
            continue;
          }
        } else {
          try {
            double value = Double.parseDouble(defValue);
            if (tweaks) {
              options.setTweakToDoubleLiteral(defName, value);
            } else {
              options.setDefineToDoubleLiteral(defName, value);
            }
            continue;
          } catch (NumberFormatException e) {
          }
          if (defValue.length() > 0) {
            if (tweaks) {
              options.setTweakToStringLiteral(defName, defValue);
            } else {
              options.setDefineToStringLiteral(defName, defValue);
            }
            continue;
          }
        }
      }
      if (tweaks) {
        throw new RuntimeException(
            "--tweak flag syntax invalid: " + override);
      }
      throw new RuntimeException(
          "--define flag syntax invalid: " + override);
    }
  }
@ VisibleForTesting public static void createDefineOrTweakReplacements ( List < String > definitions , CompilerOptions options , boolean tweaks ) { for ( String override : definitions ) { String [ ] assignment = override . split ( "=" , 2 ) ; String defName = assignment [ 0 ] ; if ( defName . length ( ) > 0 ) { String defValue = assignment . length == 1 ? "true" : assignment [ 1 ] ; boolean isTrue = defValue . equals ( "true" ) ; boolean isFalse = defValue . equals ( "false" ) ; if ( isTrue || isFalse ) { if ( tweaks ) { options . setTweakToBooleanLiteral ( defName , isTrue ) ; } else { options . setDefineToBooleanLiteral ( defName , isTrue ) ; } continue ; } else if ( defValue . length ( ) > 1 && ( ( defValue . charAt ( 0 ) == '\'' && defValue . charAt ( defValue . length ( ) - 1 ) == '\'' ) || ( defValue . charAt ( 0 ) == '\"' && defValue . charAt ( defValue . length ( ) - 1 ) == '\"' ) ) ) { String maybeStringVal = defValue . substring ( 1 , defValue . length ( ) - 1 ) ; if ( maybeStringVal . indexOf ( defValue . charAt ( 0 ) ) == - 1 ) { if ( tweaks ) { options . setTweakToStringLiteral ( defName , maybeStringVal ) ; } else { options . setDefineToStringLiteral ( defName , maybeStringVal ) ; } continue ; } } else { try { double value = Double . parseDouble ( defValue ) ; if ( tweaks ) { options . setTweakToDoubleLiteral ( defName , value ) ; } else { options . setDefineToDoubleLiteral ( defName , value ) ; } continue ; } catch ( NumberFormatException e ) { } if ( defValue . length ( ) > 0 ) { if ( tweaks ) { options . setTweakToStringLiteral ( defName , defValue ) ; } else { options . setDefineToStringLiteral ( defName , defValue ) ; } continue ; } } } if ( tweaks ) { throw new RuntimeException ( "--tweak flag syntax invalid: " + override ) ; } throw new RuntimeException ( "--define flag syntax invalid: " + override ) ; } }
Create a map of constant names to constant values from a textual description of the map.
Create a map of constant names to constant values from a textual description of the map .
// Parse the definitions // If the value starts and ends with a single quote, // we assume that it's a string. // do nothing, it will be caught at the end