Skip to content

Commit

Permalink
[TASK] Implement @typescript-eslint/prefer-string-starts-ends-with
Browse files Browse the repository at this point in the history
…rule

The eslint configuration now takes the rule
`@typescript-eslint/prefer-string-starts-ends-with` [1] into account,
enforcing the usage of `startsWith()` and `endWith()` over index
checking in strings.

[1] https://typescript-eslint.io/rules/prefer-string-starts-ends-with

Resolves: #103374
Releases: main, 12.4
Change-Id: I88640f71cbe960db2a2e2b162410ff5a79e7c55c
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83448
Tested-by: core-ci <typo3@b13.com>
Reviewed-by: Benjamin Franzke <ben@bnf.dev>
Tested-by: Andreas Kienast <a.fernandez@scripting-base.de>
Reviewed-by: Andreas Kienast <a.fernandez@scripting-base.de>
  • Loading branch information
andreaskienast committed Mar 14, 2024
1 parent 9f861f5 commit 24f0a59
Show file tree
Hide file tree
Showing 13 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions Build/.eslintrc.js
Expand Up @@ -37,6 +37,7 @@ module.exports = {
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/member-ordering": "error",
"@typescript-eslint/prefer-readonly": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/naming-convention": [
"error",
{
Expand Down
2 changes: 1 addition & 1 deletion Build/Sources/TypeScript/backend/form-engine-validation.ts
Expand Up @@ -646,7 +646,7 @@ export default (function() {
FormEngineValidation.parseDouble = function(value: number|string|boolean, precision: number = 2): string {
let theVal = '' + value;
theVal = theVal.replace(/[^0-9,.-]/g, '');
const negative = theVal.substring(0, 1) === '-';
const negative = theVal.startsWith('-');
theVal = theVal.replace(/-/g, '');
theVal = theVal.replace(/,/g, '.');
if (theVal.indexOf('.') === -1) {
Expand Down
2 changes: 1 addition & 1 deletion Build/Sources/TypeScript/core/ajax/ajax-request.ts
Expand Up @@ -161,7 +161,7 @@ class AjaxRequest {

private composeRequestUrl(): string {
let url = this.url;
if (url.charAt(0) === '?') {
if (url.startsWith('?')) {
// URL is a search string only, prepend current location
url = window.location.origin + window.location.pathname + url;
}
Expand Down
2 changes: 1 addition & 1 deletion Build/Sources/TypeScript/core/requirejs-loader.ts
Expand Up @@ -203,7 +203,7 @@ interface Require {

if (
inPath(context.config, name) ||
url.charAt(0) === '/' ||
url.startsWith('/') ||
(context.config.typo3BaseUrl as unknown as boolean) === false
) {
originalLoad.call(req, context, name, url);
Expand Down
2 changes: 1 addition & 1 deletion Build/Sources/TypeScript/rte_ckeditor/rte-link-browser.ts
Expand Up @@ -92,7 +92,7 @@ class RteLinkBrowser {
link = linkMatch[1] + linkMatch[2];
const paramsPrefix = linkMatch[2].length > 0 ? '&' : '?';
if (queryParams.length > 0) {
if (queryParams[0] === '&') {
if (queryParams.startsWith('&')) {
queryParams = queryParams.substr(1);
}
// If params is set, append it
Expand Down
Expand Up @@ -97,7 +97,7 @@ export class TsCodeCompletion {
let childNode;
// if the childnode has a value and there is a part of a reference operator ('<')
// and it does not look like a html tag ('>')
if (childNodes[key].v && childNodes[key].v[0] === '<' && childNodes[key].v.indexOf('>') === -1) {
if (childNodes[key].v && childNodes[key].v.startsWith('<') && !childNodes[key].v.includes('>')) {
const path = childNodes[key].v.replace(/</, '').trim();
// if there are still whitespaces it's no path
if (path.indexOf(' ') === -1) {
Expand Down
6 changes: 3 additions & 3 deletions Build/Sources/TypeScript/t3editor/autocomplete/ts-parser.ts
Expand Up @@ -191,11 +191,11 @@ export class TsParser {
if (i < tokens.length && tokens[i].string.length > 0) {
const tokenValue = tokens[i].string;

if (tokenValue[0] === '#') {
if (tokenValue.startsWith('#')) {
stack.push('#');
} else if (tokenValue === '(') {
stack.push('(');
} else if (tokenValue[0] === '/' && tokenValue[1] === '*') {
} else if (tokenValue.startsWith('/*')) {
stack.push('/*');
} else if (tokenValue === '{') {
// TODO: ignore whole block if wrong whitespaces in this line
Expand Down Expand Up @@ -237,7 +237,7 @@ export class TsParser {
if (tokenValue === ')') {
stack.popIfLastElementEquals('(');
}
if (tokenValue[0] === '*' && tokenValue[1] === '/') {
if (tokenValue.startsWith('*/')) {
stack.popIfLastElementEquals('/*');
ignoreLine = true;
}
Expand Down

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 24f0a59

Please sign in to comment.