Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TSLint中可靠地检测TypeScript SyntaxKind?_Typescript_Enums_Tslint - Fatal编程技术网

如何在TSLint中可靠地检测TypeScript SyntaxKind?

如何在TSLint中可靠地检测TypeScript SyntaxKind?,typescript,enums,tslint,Typescript,Enums,Tslint,我正在编写一些定制的TSLint规则,发现我不能在TypeScript版本之间依赖(asobject).kind 例如,在TypeScript3.4.5中,enum ts.SyntaxKindImportDeclaration=249和importcause=250 但是在TypeScript中,3.5.3,enum ts.SyntaxKindImportDeclaration=250和importcause=251 这打破了我的皮棉规则。有没有更好的方法来检测这一点,或者是没有使用目标项目中的

我正在编写一些定制的TSLint规则,发现我不能在TypeScript版本之间依赖
(asobject).kind

例如,在TypeScript
3.4.5
中,
enum ts.SyntaxKind
ImportDeclaration=249
importcause=250

但是在TypeScript中,
3.5.3
enum ts.SyntaxKind
ImportDeclaration=250
importcause=251

这打破了我的皮棉规则。有没有更好的方法来检测这一点,或者是没有使用目标项目中的枚举和/或导致它们未对齐的配置问题

我找不到关于这个的文档或其他讨论,所以我不确定它是否被忽视(不太可能),或者我没有正确地使用它

export class Rule extends Rules.AbstractRule {
  public apply(sourceFile: ts.SourceFile): RuleFailure[] {

    for (const statement of sourceFile.statements) {

      // statement.kind / ts.SyntaxKind.ImportDeclaration
      // is 249 in TypeScript 3.4.5, 250 in TypeScript 3.5.3,
      // object property changes for target code, enum stays the same as lint source
      if (statement && statement.kind === ts.SyntaxKind.ImportDeclaration) {
        const importDeclaration: ts.ImportDeclaration = statement as ts.ImportDeclaration;

        // importDeclaration.moduleSpecifier.kind / ts.SyntaxKind.StringLiteral
        // 10 in TypeScript 3.4.5, 10 in TypeScript 3.5.3
        if (importDeclaration.moduleSpecifier && importDeclaration.moduleSpecifier.kind === ts.SyntaxKind.StringLiteral) {
          const moduleSpecifierStringLiteral: ts.StringLiteral = importDeclaration.moduleSpecifier as ts.StringLiteral;
        ...
        }

        // importDeclaration.importClause.kind / ts.SyntaxKind.ImportClause
        // is 250 in TypeScript 3.4.5, 251 in TypeScript 3.5.3
        // object property changes for target code, enum stays the same as lint source
        if (importDeclaration.importClause) {
          if (importDeclaration.importClause.namedBindings) {
            const namedBindings: ts.NamespaceImport | ts.NamedImports = importDeclaration.importClause.namedBindings;
            // namedBindings.kind / ts.SyntaxKind.NamedImports
            // is 252 in TypeScript 3.4.5, 253 in TypeScript 3.5.3
            // object property changes for target code, enum stays the same as lint source
            if (namedBindings && namedBindings.kind === ts.SyntaxKind.NamedImports) {
              const namedImports: ts.NamedImports = namedBindings as ts.NamedImports;
              for (const element of namedImports.elements) {
                const importName: string = element.name.text;
              ...
              }
            }
          }
        }
      ...
      }
    }
  }
}

我想说一个好的尝试是切换到typescript的“is方法”,如:

if (!ts.isImportDeclaration(statement)) {
    return;
}

希望有帮助;)

我从比较开始,直接使用这些方法,但似乎它们在不同版本之间也会失败。这似乎是一个超级奇怪的问题,他们会解释,而我只是没有找到正确的方法来做这件事。我可能会求助于抓取对象,并在每次事件的基础上处理它们。