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: Add ui5lint-disable directives #327

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
26 changes: 26 additions & 0 deletions src/linter/ui5Types/SourceFileLinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ import {getPropertyName} from "./utils.js";

const log = getLogger("linter:ui5Types:SourceFileLinter");

/* Match things like:
ui5-lint-disable-next-line no-deprecated-api no-global
ui5-lint-disable
no-deprecated-api
no-global
ui5-lint-enable-next-line
*/
const disableCommentRegex = /\/[/*]\s*ui5lint-(enable|disable)((?:-next)?-line)?((?:\s+[\w-]+,)*(?:\s+[\w-]+))?\s*(?:\*\/|$)/mg;

interface DeprecationInfo {
symbol: ts.Symbol;
messageDetails: string;
Expand Down Expand Up @@ -72,6 +81,7 @@ export default class SourceFileLinter {
// eslint-disable-next-line @typescript-eslint/require-await
async lint() {
try {
this.collectPossibleComments(this.#sourceFile);
this.visitNode(this.#sourceFile);
this.#reporter.deduplicateMessages();
} catch (err) {
Expand Down Expand Up @@ -126,6 +136,22 @@ export default class SourceFileLinter {
ts.forEachChild(node, this.#boundVisitNode);
}

collectPossibleComments(sourceFile: ts.SourceFile) {
const text = sourceFile.getFullText();
let match;
const comments = new Set();
while ((match = disableCommentRegex.exec(text)) !== null) {
const [, action, nextLineOrLine, rules] = match;
const pos = match.index;
const length = match[0].length;
const ruleNames = rules?.trim().split(",") ?? [];
const isLine = nextLineOrLine === "-line";
const isNextLine = nextLineOrLine === "-next-line";
comments.add({action, isLine, isNextLine, ruleNames, pos, length});
}
return comments;
}

analyzeIdentifier(node: ts.Identifier) {
const type = this.#checker.getTypeAtLocation(node);
if (!type?.symbol || !this.isSymbolOfUi5OrThirdPartyType(type.symbol)) {
Expand Down
97 changes: 97 additions & 0 deletions test/fixtures/linter/rules/Directives/Directives.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
sap.ui.define([
"sap/m/Button", "sap/m/DateTimeInput", "sap/base/util/includes", "sap/ui/Device", "sap/ui/core/library", "sap/ui/generic/app/navigation/service/NavigationHandler",
"sap/ui/table/Table", "sap/ui/table/plugins/MultiSelectionPlugin", "sap/ui/core/Configuration", "sap/m/library"
], function(Button, DateTimeInput, includes, Device, coreLib, NavigationHandler, Table, MultiSelectionPlugin, Configuration, mobileLib) {

// ui5lint-disable-next-line no-deprecated-api
var dateTimeInput = new DateTimeInput(); // IGNORE: Control is deprecated. A finding only appears for the module dependency, not for the usage.

var btn = new Button({
blocked: true, // ui5lint-disable-line no-deprecated-parameter -- IGNORE: Property "blocked" is deprecated
// ui5lint-disable-next-line no-deprecated-parameter
tap: () => console.log("Tapped") // IGNORE: Event "tap" is deprecated
});

/* ui5lint-disable */
btn.attachTap(function() { // IGNORE: Method "attachTap" is deprecated
console.log("Tapped");
});
/* ui5lint-enable */

btn.attachTap(function() { // REPORT
console.log("Tapped");
});

/* ui5lint-disable
no-deprecated-parameter,
no-deprecated-api
*/
var table = new Table({
plugins: [ // IGNORE: Aggregation "plugins" is deprecated
new MultiSelectionPlugin()
],
groupBy: "some-column" // IGNORE: Association "groupBy" is deprecated
});
/* ui5lint-enable no-deprecated-parameter */

includes([1], 1); // IGNORE: Function "includes" is deprecated

const getIncludesFunction = () => includes;
getIncludesFunction()([1], 1); // IGNORE: Function "includes" is deprecated
/* ui5lint-enable */

includes([1], 1); // REPORT: Function "includes" is deprecated

/* ui5lint-disable-next-line */
Configuration.getCompatibilityVersion("sapMDialogWithPadding"); // IGNORE: Method "getCompatibilityVersion" is deprecated
/* ui5lint-disable-next-line */
Configuration["getCompatibilityVersion"]("sapMDialogWithPadding"); // IGNORE: Method "getCompatibilityVersion" is deprecated

/* ui5lint-disable-next-line no-deprecated-property */
Device.browser.webview; // IGNORE: "webview" is deprecated
// ui5lint-disable-next-line no-deprecated-property
Device.browser["webview"]; // IGNORE: "webview" is deprecated

Device.browser["webview"]; // REPORT: "webview" is deprecated

// ui5lint-disable-next-line no-deprecated-property
Configuration.AnimationMode; // IGNORE: Property "AnimationMode" (Enum) is deprecated

// ui5lint-disable-next-line no-deprecated-property
coreLib.MessageType; // IGNORE: Enum "MessageType" is deprecated

// ui5lint-disable-next-line no-deprecated-property -- Followed by an intentionally Empty line
coreLib.MessageType; // REPORT: Enum "MessageType" is deprecated

// ui5lint-disable-next-line no-deprecated-property
let {BarColor, MessageType} = coreLib; // IGNORE: Enum "MessageType" is deprecated
// ui5lint-disable-next-line no-deprecated-property
({MessageType} = coreLib); // IGNORE: Enum "MessageType" is deprecated
MessageType.Error;

// ui5lint-disable-next-line no-deprecated-property
let {BarColor, MessageType: mt} = coreLib; // IGNORE: Enum "MessageType" is deprecated
// ui5lint-disable-next-line no-deprecated-property
({BarColor, MessageType: mt} = coreLib); // IGNORE: Enum "MessageType" is deprecated
mt.Error;

// ui5lint-disable-next-line no-deprecated-property
mobileLib.InputType.Date; // IGNORE: Enum value "InputType.Date" is deprecated

const navigationHandler = new NavigationHandler({});
// ui5lint-disable-next-line no-deprecated-property, no-deprecated-api
navigationHandler.storeInnerAppState({}); // IGNORE: Method "storeInnerAppState" is deprecated


// ui5lint-disable no-deprecated-property, no-global
new sap.m.Button({ // IGNORE: Global variable "sap"
blocked: true, // IGNORE: Property "blocked" is deprecated
tap: () => console.log("Tapped") // IGNORE: Event "tap" is deprecated
});
// ui5lint-enable

new sap.m.Button({ // REPORT: Global variable "sap"
blocked: true, // REPORT: Property "blocked" is deprecated
tap: () => console.log("Tapped") // REPORT: Event "tap" is deprecated
});
});
1 change: 1 addition & 0 deletions test/fixtures/linter/rules/Directives/Directives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// TODO: TS version of Directives.js
5 changes: 5 additions & 0 deletions test/lib/linter/rules/Directives.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {fileURLToPath} from "node:url";
import {runLintRulesTests} from "../_linterHelper.js";

const filePath = fileURLToPath(import.meta.url);
runLintRulesTests(filePath);
Loading
Loading