While creating custom TSLint rules, I encountered a reliability issue with (ASTObject).kind
across different TypeScript versions.
For instance, in TypeScript version 3.4.5
, the values for enum ts.SyntaxKind
are ImportDeclaration = 249
and ImportClause = 250
.
However, in TypeScript version 3.5.3
, the same enums are changed to ImportDeclaration = 250
and ImportClause = 251
.
This discrepancy caused issues with my lint rules. Is there a more effective method to identify this problem or a configuration error that might be causing the enum values to misalign?
I couldn't find any relevant documentation or discussions on this matter, which leaves me unsure if this is an oversight (unlikely) or if I am not utilizing it correctly.
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;
...
}
}
}
}
...
}
}
}
}