diff --git a/src/linter/ui5Types/SourceFileLinter.ts b/src/linter/ui5Types/SourceFileLinter.ts index cd22ed1b4..e5c5b0e96 100644 --- a/src/linter/ui5Types/SourceFileLinter.ts +++ b/src/linter/ui5Types/SourceFileLinter.ts @@ -11,6 +11,22 @@ interface DeprecationInfo { messageDetails?: string; } +function isSourceFileOfUi5Type(sourceFile: ts.SourceFile) { + return /@openui5|@sapui5|@ui5/.test(sourceFile.fileName); +} + +function isSourceFileOfUi5OrThirdPartyType(sourceFile: ts.SourceFile) { + return /@openui5|@sapui5|@ui5|@types\/jquery/.test(sourceFile.fileName); +} + +function isSourceFileOfJquerySapType(sourceFile: ts.SourceFile) { + return sourceFile.fileName === "/types/@ui5/linter/overrides/jquery.sap.d.ts"; +} + +function isSourceFileOfPseudoModuleType(sourceFile: ts.SourceFile) { + return sourceFile.fileName.startsWith("/types/@ui5/linter/overrides/library/"); +} + export default class SourceFileLinter { #resourcePath: ResourcePath; #sourceFile: ts.SourceFile; @@ -507,10 +523,13 @@ export default class SourceFileLinter { return; } const symbol = this.#checker.getSymbolAtLocation(moduleSpecifierNode); + if (!symbol) { + return; + } // Only check for the "default" export regardless of what's declared // as UI5 / AMD only supports importing the default anyways. // TODO: This needs to be enhanced in future - const defaultExportSymbol = symbol?.exports?.get("default" as ts.__String); + const defaultExportSymbol = symbol.exports?.get("default" as ts.__String); const deprecationInfo = this.getDeprecationInfo(defaultExportSymbol); if (deprecationInfo) { this.#reporter.addMessage({ @@ -523,7 +542,7 @@ export default class SourceFileLinter { }); } - if (this.isSymbolOfPseudoType(symbol)) { + if (this.isSymbolOfPseudoModuleType(symbol)) { const moduleNamespaceName = moduleSpecifierNode.text.replaceAll("/", "."); const isDataType = !!this.#dataTypes[moduleNamespaceName]; if (isDataType) { @@ -547,34 +566,28 @@ export default class SourceFileLinter { } isSymbolOfUi5Type(symbol: ts.Symbol) { - if (symbol.name.startsWith("sap/")) { - return true; - } else { - const sourceFile = symbol.valueDeclaration?.getSourceFile(); - if (sourceFile?.fileName.match(/@openui5|@sapui5|@ui5/)) { - return true; - } - } - return false; + return this.checkSymbolDeclarationSourceFile(symbol, isSourceFileOfUi5Type); } isSymbolOfUi5OrThirdPartyType(symbol: ts.Symbol) { - if (symbol.name.startsWith("sap/")) { - return true; - } else { - const sourceFile = symbol.valueDeclaration?.getSourceFile(); - if (sourceFile?.fileName.match(/@openui5|@sapui5|@ui5|@types\/jquery/)) { - return true; - } - } - return false; + return this.checkSymbolDeclarationSourceFile(symbol, isSourceFileOfUi5OrThirdPartyType); } isSymbolOfJquerySapType(symbol: ts.Symbol) { - return symbol.valueDeclaration?.getSourceFile().fileName === "/types/@ui5/linter/overrides/jquery.sap.d.ts"; + return this.checkSymbolDeclarationSourceFile(symbol, isSourceFileOfJquerySapType); + } + + isSymbolOfPseudoModuleType(symbol: ts.Symbol) { + return this.checkSymbolDeclarationSourceFile(symbol, isSourceFileOfPseudoModuleType); } - isSymbolOfPseudoType(symbol: ts.Symbol | undefined) { - return symbol?.valueDeclaration?.getSourceFile().fileName.startsWith("/types/@ui5/linter/overrides/library/"); + checkSymbolDeclarationSourceFile( + symbol: ts.Symbol, checkFunction: (sourceFile: ts.SourceFile) => boolean + ) { + const declarations = symbol.getDeclarations(); + if (!declarations) { + return false; + } + return declarations.some((declaration) => checkFunction(declaration.getSourceFile())); } } diff --git a/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md b/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md index ff059402c..bef9f7130 100644 --- a/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md +++ b/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md @@ -70,7 +70,7 @@ Generated by [AVA](https://avajs.dev). [ { coverageInfo: [], - errorCount: 17, + errorCount: 18, fatalErrorCount: 0, filePath: 'NoDeprecatedApi.js', messages: [ @@ -101,6 +101,15 @@ Generated by [AVA](https://avajs.dev). ruleId: 'ui5-linter-no-deprecated-api', severity: 2, }, + { + column: 69, + fatal: undefined, + line: 3, + message: 'Import of deprecated module \'sap/ui/core/Configuration\'', + messageDetails: 'Deprecated test message', + ruleId: 'ui5-linter-no-deprecated-api', + severity: 2, + }, { column: 3, fatal: undefined, diff --git a/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap b/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap index f73632f2e..2703175e6 100644 Binary files a/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap and b/test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap differ