Skip to content

Commit

Permalink
fix: Detect correctly sap.ui.define calls
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Jan 6, 2025
1 parent 2017067 commit 5e4cbf3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export default class SourceFileLinter {
this.visitNode(this.sourceFile);

if (this.sourceFile.fileName.includes(".qunit.js") &&
!metadata?.transformedImports?.get("sap.ui.define")?.size) {
!metadata?.transformedImports?.get("sap.ui.define")) {
this.#reporter.addMessage(MESSAGE.PREFER_TEST_STARTER, this.sourceFile);
}

Expand Down
13 changes: 9 additions & 4 deletions src/linter/ui5Types/amdTranspiler/tsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,13 @@ function transform(
const moduleDeclaration = parseModuleDeclaration(node.arguments, checker);
const moduleDefinition = moduleDeclarationToDefinition(moduleDeclaration, sourceFile, nodeFactory);
moduleDefinitions.push(moduleDefinition);
moduleDefinition.imports.forEach((importStatement) =>
addModuleMetadata(metadata, "sap.ui.define", importStatement));
if (moduleDefinition.imports.length) {
moduleDefinition.imports.forEach((importStatement) =>
addModuleMetadata(metadata, "sap.ui.define", importStatement));
} else {
// Empty sap.ui.define (no imports, no body)
addModuleMetadata(metadata, "sap.ui.define");
}
pruneNode(node); // Mark the define call for removal
} catch (err) {
if (err instanceof UnsupportedModuleError) {
Expand Down Expand Up @@ -278,12 +283,12 @@ function transform(
}
});

function addModuleMetadata(metadata: LintMetadata, importType: string, importStatement: ts.ImportDeclaration) {
function addModuleMetadata(metadata: LintMetadata, importType: string, importStatement?: ts.ImportDeclaration) {
if (!metadata.transformedImports) {
metadata.transformedImports = new Map<string, Set<string>>();
}
const curResource = metadata.transformedImports.get(importType) ?? new Set<string>();
if (ts.isStringLiteral(importStatement.moduleSpecifier)) {
if (importStatement && ts.isStringLiteral(importStatement.moduleSpecifier)) {
curResource.add(importStatement.moduleSpecifier.text);
}
metadata.transformedImports.set(importType, curResource);
Expand Down
20 changes: 2 additions & 18 deletions test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -2956,16 +2956,8 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'no-deprecated-theme',
severity: 2,
},
{
column: 9,
line: 3,
message: 'To save boilerplate code and ensure compliance with UI5 2.x best practices, please migrate to the Test Starter concept',
messageDetails: 'Test Starter (https://ui5.sap.com/#/topic/032be2cb2e1d4115af20862673bedcdb)',
ruleId: 'prefer-test-starter',
severity: 1,
},
],
warningCount: 1,
warningCount: 0,
},
]

Expand Down Expand Up @@ -3044,16 +3036,8 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'no-deprecated-theme',
severity: 2,
},
{
column: 9,
line: 3,
message: 'To save boilerplate code and ensure compliance with UI5 2.x best practices, please migrate to the Test Starter concept',
messageDetails: 'Test Starter (https://ui5.sap.com/#/topic/032be2cb2e1d4115af20862673bedcdb)',
ruleId: 'prefer-test-starter',
severity: 1,
},
],
warningCount: 1,
warningCount: 0,
},
]

Expand Down
Binary file modified test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap
Binary file not shown.

0 comments on commit 5e4cbf3

Please sign in to comment.