Skip to content

Commit

Permalink
refactor: Resolve the new eslint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Aug 2, 2024
1 parent 228537c commit e0aae21
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
42 changes: 13 additions & 29 deletions src/formatter/lib/resolveLinks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,21 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
}

// If the link has a protocol, do not modify, but open in a new window
if (/:\/\//.test(sTarget)) {
if (sTarget.includes("://")) {
return formatUrlToLink(sTarget, sText);
}
// topic:xxx Topic
aMatch = sTarget.match(
/^topic:(\w{32}(?:#\w*)?(?:\/\w*)?)$/
);
aMatch = /^topic:(\w{32}(?:#\w*)?(?:\/\w*)?)$/.exec(sTarget);
if (aMatch) {
return formatUrlToLink(`${ui5Url}/#/topic/${aMatch[1]}`, sText);
}
// demo:xxx Demo, open the demonstration page in a new window
aMatch = sTarget.match(/^demo:([a-zA-Z0-9/.]*)$/);
aMatch = /^demo:([a-zA-Z0-9/.]*)$/.exec(sTarget);
if (aMatch) {
return formatUrlToLink(`${ui5Url}/${ui5Version}/#/test-resources/${aMatch[1]}`, sText);
}
// sap.x.Xxx.prototype.xxx - In case of prototype we have a link to method
aMatch = sTarget.match(
/([a-zA-Z0-9.$_]+?)\.prototype\.([a-zA-Z0-9.$_]+)$/
);
aMatch = /([a-zA-Z0-9.$_]+?)\.prototype\.([a-zA-Z0-9.$_]+)$/.exec(sTarget);
if (aMatch) {
return createLink({
name: aMatch[2],
Expand All @@ -114,9 +110,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
// Heuristics: Extend is always a static method
// sap.x.Xxx.extend
// module:sap/x/Xxx.extend
aMatch = sTarget.match(
/^(module:)?([a-zA-Z0-9.$_/]+?)\.extend$/
);
aMatch = /^(module:)?([a-zA-Z0-9.$_/]+?)\.extend$/.exec(sTarget);
if (aMatch) {
const [, sModule, sClass] = aMatch;
return createLink({
Expand All @@ -132,9 +126,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
// sap.x.Xxx#constructor
// module:sap/x/Xxx.constructor
// #constructor
aMatch = sTarget.match(
/^(module:)?([a-zA-Z0-9.$_/]+?)?[.#]constructor$/i
);
aMatch = /^(module:)?([a-zA-Z0-9.$_/]+?)?[.#]constructor$/i.exec(sTarget);
if (aMatch) {
const [, sModule, sClass] = aMatch;
let sName = "";
Expand All @@ -151,7 +143,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
// #.setText - local static method
// #setText - local instance method
// #.setText.from - local nested method
aMatch = sTarget.match(/^#(\.)?([a-zA-Z0-9.$_]+)$/);
aMatch = /^#(\.)?([a-zA-Z0-9.$_]+)$/.exec(sTarget);
if (aMatch) {
return createLink({
name: aMatch[2],
Expand All @@ -162,7 +154,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
});
}
// #annotation:TextArrangement - local annotation
aMatch = sTarget.match(/^#annotation:([a-zA-Z0-9$_]+)$/);
aMatch = /^#annotation:([a-zA-Z0-9$_]+)$/.exec(sTarget);
if (aMatch) {
return createLink({
name: aMatch[1],
Expand All @@ -177,9 +169,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
// sap.ui.comp.smartfield.SmartField.annotation:TextArrangement
// module:sap/ui/comp/smartfield/SmartField.annotation:TextArrangement
// module:sap/ui/comp/smartfield/SmartField#annotation:TextArrangement
aMatch = sTarget.match(
/^(module:)?([a-zA-Z0-9.$_/]+?)[.#]annotation:([a-zA-Z0-9$_]+)$/
);
aMatch = /^(module:)?([a-zA-Z0-9.$_/]+?)[.#]annotation:([a-zA-Z0-9$_]+)$/.exec(sTarget);
if (aMatch) {
const [, sModule, sClass, sAnnotation] = aMatch;
return createLink({
Expand All @@ -191,7 +181,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
});
}
// #event:press - local event
aMatch = sTarget.match(/^#event:([a-zA-Z0-9$_]+)$/);
aMatch = /^#event:([a-zA-Z0-9$_]+)$/.exec(sTarget);
if (aMatch) {
return createLink({
name: aMatch[1],
Expand All @@ -206,9 +196,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
// sap.m.Button.event:press
// module:sap/m/Button.event:press
// module:sap/m/Button#event:press
aMatch = sTarget.match(
/^(module:)?([a-zA-Z0-9.$_/]+?)[.#]event:([a-zA-Z0-9$_]+)$/
);
aMatch = /^(module:)?([a-zA-Z0-9.$_/]+?)[.#]event:([a-zA-Z0-9$_]+)$/.exec(sTarget);
if (aMatch) {
const [, sModule, sClass, sEvent] = aMatch;
return createLink({
Expand All @@ -221,9 +209,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
}
// sap.m.Button#setText - instance method
// module:sap/m/Button#setText
aMatch = sTarget.match(
/^(module:)?([a-zA-Z0-9.$_/]+)#([a-zA-Z0-9.$_]+)$/
);
aMatch = /^(module:)?([a-zA-Z0-9.$_/]+)#([a-zA-Z0-9.$_]+)$/.exec(sTarget);
if (aMatch) {
const [, sModule, sClass, sMethod] = aMatch;
return createLink({
Expand All @@ -235,9 +221,7 @@ function _preProcessLinksInTextBlock(sText: string, ui5Url: string, ui5Version:
});
}
// module:sap/m/Button.setText
aMatch = sTarget.match(
/^(module:)([a-zA-Z0-9.$_/]+)\.([a-zA-Z0-9.$_]+)$/
);
aMatch = /^(module:)([a-zA-Z0-9.$_/]+)\.([a-zA-Z0-9.$_]+)$/.exec(sTarget);
if (aMatch) {
const [, sModule, sClass, sMethod] = aMatch;
return createLink({
Expand Down
2 changes: 1 addition & 1 deletion src/linter/xmlTemplate/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ export default class Parser {
const parentNode = this._findParentNode(
NodeKind.Control | NodeKind.Aggregation | NodeKind.FragmentDefinition);

if (moduleName.match(/^[a-z]/)) {
if (/^[a-z]/.exec(moduleName)) {
const aggregationName = moduleName;
// TODO: Replace the above with a check against known controls. Even though there are
// no known cases of lower case control names in the framework.
Expand Down
28 changes: 18 additions & 10 deletions src/linter/xmlTemplate/generator/Writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@ export default class Writer {
if (str === null || str === "") {
return;
}
start && this.#addMapping(start);
if (start) {
this.#addMapping(start);
}

const strSplit = str.split(NL);
this.lineOffset += strSplit.length - 1;
this.columnOffset += strSplit[strSplit.length - 1].length;
this.#buf += str;

end && this.#addMapping(end);
if (end) {
this.#addMapping(end);
}
}

writeln(str: string, start?: Position, end?: Position) {
Expand All @@ -54,18 +58,22 @@ export default class Writer {

this.#shiftMappings(lineOffset + 1); // Adding one for the additional new line we'll be adding

start && this.#addMapping(start, {
line: 0,
column: 0,
});
if (start) {
this.#addMapping(start, {
line: 0,
column: 0,
});
}

this.lineOffset += lineOffset + 1; // Adding one for the additional new line
this.#buf = str + NL + this.#buf;

end && this.#addMapping(end, {
line: lineOffset,
column: columnOffset,
});
if (end) {
this.#addMapping(end, {
line: lineOffset,
column: columnOffset,
});
}
}

getString() {
Expand Down

0 comments on commit e0aae21

Please sign in to comment.