Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Detect non-Test Starter test setups #448

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/linter/LinterContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface PositionRange {
export interface LintMetadata {
// The metadata holds information to be shared across linters
directives: Set<Directive>;
transformedImports: Map<string, Set<string>>;
}

export default class LinterContext {
Expand Down
19 changes: 19 additions & 0 deletions src/linter/html/transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default async function transpileHtml(
lintBootstrapAttributes(bootstrapTag, report);
}

detectTestStarter(resourcePath, scriptTags, context);

scriptTags.forEach((tag) => {
// Tags with src attribute do not parse and run inline code
const hasSrc = tag.attributes.some((attr) => {
Expand Down Expand Up @@ -52,6 +54,23 @@ export default async function transpileHtml(
}
}

function detectTestStarter(resourcePath: ResourcePath, scriptTags: Tag[], context: LinterContext) {
const shouldBeMigrated = scriptTags.some((tag) => {
return (resourcePath.includes("testsuite.qunit.html") && !tag.attributes.some((attr) => {
return attr.name.value.toLowerCase() === "src" &&
attr.value.value.includes("resources/sap/ui/test/starter/createSuite.js");
})) ||
(resourcePath.includes("qunit.html") && !tag.attributes.some((attr) => {
return attr.name.value.toLowerCase() === "src" &&
attr.value.value.includes("resources/sap/ui/test/starter/runTest.js");
}));
});

if (shouldBeMigrated) {
context.addLintingMessage(resourcePath, MESSAGE.PREFER_TEST_STARTER, undefined as never);
}
}

function findBootstrapTag(tags: Tag[]): Tag | undefined {
// First search for script tag with id "sap-ui-bootstrap"
for (const tag of tags) {
Expand Down
11 changes: 11 additions & 0 deletions src/linter/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const RULES = {
"no-pseudo-modules": "no-pseudo-modules",
"parsing-error": "parsing-error",
"ui5-class-declaration": "ui5-class-declaration",
"prefer-test-starter": "prefer-test-starter",
} as const;

export enum LintMessageSeverity {
Expand Down Expand Up @@ -63,6 +64,7 @@ export enum MESSAGE {
PARTIALLY_DEPRECATED_ODATA_MODEL_V2_CREATE_ENTRY,
PARTIALLY_DEPRECATED_ODATA_MODEL_V2_CREATE_ENTRY_PROPERTIES_ARRAY,
PARTIALLY_DEPRECATED_PARAMETERS_GET,
PREFER_TEST_STARTER,
REDUNDANT_BOOTSTRAP_PARAM,
REDUNDANT_BOOTSTRAP_PARAM_ERROR,
REDUNDANT_VIEW_CONFIG_PROPERTY,
Expand Down Expand Up @@ -550,6 +552,15 @@ export const MESSAGE_INFO = {
},
},

[MESSAGE.PREFER_TEST_STARTER]: {
severity: LintMessageSeverity.Warning,
ruleId: RULES["prefer-test-starter"],

message: () => "To save boilerplate code and ensure compliance with UI5 2.x best practices," +
" please migrate to the Test Starter concept",
details: () => "{@link topic:032be2cb2e1d4115af20862673bedcdb Test Starter}",
},

[MESSAGE.REPLACED_BOOTSTRAP_PARAM]: {
severity: LintMessageSeverity.Error,
ruleId: RULES["no-deprecated-api"],
Expand Down
24 changes: 23 additions & 1 deletion src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ export default class SourceFileLinter {
findDirectives(this.sourceFile, metadata);
}
this.visitNode(this.sourceFile);

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

this.#reporter.deduplicateMessages();
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
Expand Down Expand Up @@ -642,6 +648,13 @@ export default class SourceFileLinter {
}

analyzeNewExpression(node: ts.NewExpression) {
if (this.sourceFile.fileName.includes(".qunit.js") &&
((ts.isPropertyAccessExpression(node.expression) && node.expression.name.getText() === "jsUnitTestSuite") ||
(ts.isIdentifier(node.expression) && node.expression.getText() === "jsUnitTestSuite")
)) {
this.#reporter.addMessage(MESSAGE.PREFER_TEST_STARTER, node);
}

const nodeType = this.checker.getTypeAtLocation(node); // checker.getContextualType(node);
if (!nodeType.symbol || !this.isSymbolOfUi5OrThirdPartyType(nodeType.symbol)) {
return;
Expand Down Expand Up @@ -790,6 +803,9 @@ export default class SourceFileLinter {
this.#analyzeMobileInit(node);
} else if (symbolName === "setTheme" && moduleName === "sap/ui/core/Theming") {
this.#analyzeThemingSetTheme(node);
} else if (this.sourceFile.fileName.includes(".qunit.js") &&
symbolName === "ready" && moduleName === "sap/ui/core/Core") {
this.#reporter.addMessage(MESSAGE.PREFER_TEST_STARTER, node);
}
}

Expand Down Expand Up @@ -826,11 +842,17 @@ export default class SourceFileLinter {
}
}

const propName = getPropertyName(reportNode);

this.#reporter.addMessage(MESSAGE.DEPRECATED_FUNCTION_CALL, {
functionName: getPropertyName(reportNode),
functionName: propName,
additionalMessage,
details: deprecationInfo.messageDetails,
}, reportNode);

if (propName === "attachInit" && this.sourceFile.fileName.includes(".qunit.js")) {
this.#reporter.addMessage(MESSAGE.PREFER_TEST_STARTER, reportNode);
}
}

getSymbolModuleDeclaration(symbol: ts.Symbol) {
Expand Down
18 changes: 17 additions & 1 deletion src/linter/ui5Types/amdTranspiler/tsTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import replaceNodeInParent, {NodeReplacement} from "./replaceNodeInParent.js";
import {toPosStr, UnsupportedModuleError} from "./util.js";
import rewriteExtendCall, {UnsupportedExtendCall} from "./rewriteExtendCall.js";
import insertNodesInParent from "./insertNodesInParent.js";
import LinterContext from "../../LinterContext.js";
import LinterContext, {LintMetadata} from "../../LinterContext.js";
import {findDirectives} from "../directives.js";

const log = getLogger("linter:ui5Types:amdTranspiler:TsTransformer");
Expand Down Expand Up @@ -113,6 +113,8 @@ 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));
pruneNode(node); // Mark the define call for removal
} catch (err) {
if (err instanceof UnsupportedModuleError) {
Expand All @@ -127,6 +129,8 @@ function transform(
if (requireExpression.async) {
const res = transformAsyncRequireCall(node, requireExpression, nodeFactory);
requireImports.push(...res.imports);
res.imports.forEach((importStatement) =>
addModuleMetadata(metadata, "sap.ui.require", importStatement));
if (res.callback) {
replaceNode(node, res.callback);
if (res.errback) {
Expand All @@ -149,6 +153,7 @@ function transform(
} else {
const res = transformSyncRequireCall(node, requireExpression, nodeFactory);
requireImports.push(res.import);
addModuleMetadata(metadata, "sap.ui.require", res.import);
replaceNode(node, res.requireStatement);
}
} catch (err) {
Expand Down Expand Up @@ -273,6 +278,17 @@ function transform(
}
});

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)) {
curResource.add(importStatement.moduleSpecifier.text);
}
metadata.transformedImports.set(importType, curResource);
}

function getCommentsFromNode(node: ts.Node, sourceFile?: ts.SourceFile): NodeComments {
const sourceText = sourceFile?.getFullText() ?? fullSourceText;
const leadingComments = ts.getLeadingCommentRanges(sourceText, node.getFullStart()) ?? [];
Expand Down
33 changes: 29 additions & 4 deletions test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -567,8 +567,17 @@ Generated by [AVA](https://avajs.dev).
errorCount: 0,
fatalErrorCount: 0,
filePath: 'Configuration.beforeBootstrap.qunit.js',
messages: [],
warningCount: 0,
messages: [
{
column: 1,
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,
},
]

Expand Down Expand Up @@ -2947,8 +2956,16 @@ 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: 0,
warningCount: 1,
},
]

Expand Down Expand Up @@ -3027,8 +3044,16 @@ 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: 0,
warningCount: 1,
},
]

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