Skip to content

Commit

Permalink
fix: Missing detection of deprecated modules that export an interface
Browse files Browse the repository at this point in the history
Fixes: #86
  • Loading branch information
matz3 committed Aug 12, 2024
1 parent 48faecf commit 829e826
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
59 changes: 36 additions & 23 deletions src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand All @@ -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) {
Expand All @@ -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()));
}
}
11 changes: 10 additions & 1 deletion test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Generated by [AVA](https://avajs.dev).
[
{
coverageInfo: [],
errorCount: 17,
errorCount: 18,
fatalErrorCount: 0,
filePath: 'NoDeprecatedApi.js',
messages: [
Expand Down Expand Up @@ -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,
Expand Down
Binary file modified test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap
Binary file not shown.

0 comments on commit 829e826

Please sign in to comment.