6

私はGoogle Closure、特にタイプセーフを強制するための注釈付けを試しています。テストするために、私は何か間違ったことをしましたが、コンパイラはそれがそうであることを教えてくれません...

コードは次のとおりです。

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
 * A card.
 * @constructor
 * @param {String} cardName The exact name of the card
 * @param {Kinetic.Layer} layer The layer for the card
 */
function CardObject(cardName, layer)
{
    /** @type {Number} */
    var number = cardName;
}

numberだから、私が言う変数がありNumber、それに文字列を割り当てようとします。これは不可能ですよね?コンパイラはそれを教えてくれませんが...

なぜそれが間違っていると教えてくれないのですか?

4

2 に答える 2

7

Closure Compiler は警告レベルを使用して、コンパイル プロセス中にどのチェックを有効にするかを決定します。3 つの警告レベルは次のとおりです。

  • 静かな
  • デフォルト
  • 詳細

たとえば、コンパイル レベルを使用SIMPLE_OPTIMIZATIONSすると、警告レベルが に設定された型チェック警告が表示されVERBOSEます。

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level SIMPLE_OPTIMIZATIONS
// @warning_level VERBOSE
// ==/ClosureCompiler==

/**
 * A card.
 * @constructor
 * @param {String} cardName The exact name of the card
 * @param {Kinetic.Layer} layer The layer for the card
 */
function CardObject(cardName, layer)
{
    /** @type {Number} */
    var number = cardName;
}

出力

Number of warnings: 2

JSC_TYPE_PARSE_ERROR: Bad type annotation. Unknown type Kinetic.Layer at line 5
character 10  
* @param {Kinetic.Layer} layer The layer for the card
          ^
JSC_TYPE_MISMATCH: initializing variable
found   : (String|null|undefined)
required: (Number|null) at line 10 character 13
var number = cardName;
             ^

各警告レベルに関連付けられているチェックを正確に理解するために、WarningLevels.java の関連コードを次に示します。

静かな

/**
 * Silence all non-essential warnings.
 */
private static void silenceAllWarnings(CompilerOptions options) {
  // Just use a ShowByPath warnings guard, so that we don't have
  // to maintain a separate class of warnings guards for silencing warnings.
  options.addWarningsGuard(
      new ShowByPathWarningsGuard(
          "the_longest_path_that_cannot_be_expressed_as_a_string"));

  // Allow passes that aren't going to report anything to be skipped.

  options.checkRequires = CheckLevel.OFF;
  options.checkProvides = CheckLevel.OFF;
  options.checkMissingGetCssNameLevel = CheckLevel.OFF;
  options.aggressiveVarCheck = CheckLevel.OFF;
  options.checkTypes = false;
  options.setWarningLevel(DiagnosticGroups.CHECK_TYPES, CheckLevel.OFF);
  options.checkUnreachableCode = CheckLevel.OFF;
  options.checkMissingReturn = CheckLevel.OFF;
  options.setWarningLevel(DiagnosticGroups.ACCESS_CONTROLS, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.CONST, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.CONSTANT_PROPERTY, CheckLevel.OFF);
  options.checkGlobalNamesLevel = CheckLevel.OFF;
  options.checkSuspiciousCode = false;
  options.checkGlobalThisLevel = CheckLevel.OFF;
  options.setWarningLevel(DiagnosticGroups.GLOBAL_THIS, CheckLevel.OFF);
  options.setWarningLevel(DiagnosticGroups.ES5_STRICT, CheckLevel.OFF);
  options.checkCaja = false;
}

デフォルト

/**
 * Add the default checking pass to the compilation options.
 * @param options The CompilerOptions object to set the options on.
 */
private static void addDefaultWarnings(CompilerOptions options) {
  options.checkSuspiciousCode = true;
  options.checkUnreachableCode = CheckLevel.WARNING;
  options.checkControlStructures = true;
}

詳細

/**
 * Add all the check pass that are possibly relevant to a non-googler.
 * @param options The CompilerOptions object to set the options on.
 */
private static void addVerboseWarnings(CompilerOptions options) {
  addDefaultWarnings(options);

  // checkSuspiciousCode needs to be enabled for CheckGlobalThis to get run.
  options.checkSuspiciousCode = true;
  options.checkGlobalThisLevel = CheckLevel.WARNING;
  options.checkSymbols = true;
  options.checkMissingReturn = CheckLevel.WARNING;

  // checkTypes has the side-effect of asserting that the
  // correct number of arguments are passed to a function.
  // Because the CodingConvention used with the web service does not provide a
  // way for optional arguments to be specified, these warnings may result in
  // false positives.
  options.checkTypes = true;
  options.checkGlobalNamesLevel = CheckLevel.WARNING;
  options.aggressiveVarCheck = CheckLevel.WARNING;
  options.setWarningLevel(
      DiagnosticGroups.MISSING_PROPERTIES, CheckLevel.WARNING);
  options.setWarningLevel(
      DiagnosticGroups.DEPRECATED, CheckLevel.WARNING);
}

options.checkTypes = true;VERBOSE 警告レベルに対してのみ設定されていることに注意してください。Speransky Danilが指摘したように、コンパイル レベルを使用すると型チェックも有効になりますADVANCED_OPTIMIZATIONS

さらに、警告のクラスは、コンパイラ フラグを使用して Closure Compiler アプリケーション (jar ファイル) で個別に制御できます。

  • --jscomp_off
  • --jscomp_warning
  • --jscomp_error

指定できる警告クラスは次のとおりです。

  • アクセス制御
  • ambiguousFunctionDecl
  • checkRegExp
  • チェックタイプ
  • チェック変数
  • 定数
  • 定数プロパティ
  • 非推奨
  • 重複メッセージ
  • es5Strict
  • externsValidation
  • fileoverviewTags
  • globalThis
  • internetExplorerChecks
  • invalidCasts
  • missingProperties
  • nonStandardJsDocs
  • strictModuleDepCheck
  • typeInvalidation
  • undefinedNames
  • undefinedVars
  • unknownDefines
  • uselessCode
  • visibility

For example, type checking warnings could be enabled individually:

--jscomp_warning=checkTypes
于 2012-09-02T15:45:12.807 に答える
0

高度な最適化モードを選択する必要があります。

// @compilation_level ADVANCED_OPTIMIZATIONS

次に、このコードの場合、たとえば:

// ==ClosureCompiler==
// @output_file_name default.js
// @compilation_level ADVANCED_OPTIMIZATIONS
// ==/ClosureCompiler==

/**
* @param {String} str
*/
function func(str) {
  /** @type {Number} */
  var num = str;
}

警告があります:

JSC_TYPE_MISMATCH: initializing variable
found   : (String|null)
required: (Number|null) at line 6 character 10
var num = str;

ご存知だと思いますが、そうでない場合は、オンラインでここで遊ぶことができます:http: //closure-compiler.appspot.com/home

于 2012-09-02T15:22:48.160 に答える