fix(lint): modernize string and array checks with prefer-includes and prefer-string-starts-ends-with - #11146
fix(lint): modernize string and array checks with prefer-includes and prefer-string-starts-ends-with#11146joehan wants to merge 7 commits into
Conversation
… prefer-string-starts-ends-with Modernizes legacy indexOf and RegExp membership/prefix checks to prefer-includes and prefer-string-starts-ends-with across 9 files. Promotes @typescript-eslint/prefer-includes and @typescript-eslint/prefer-string-starts-ends-with from 'warn' to 'error' in .eslintrc.js. b/565073781
There was a problem hiding this comment.
Code Review
This pull request modernizes string and array checks across the codebase by replacing regular expression tests and indexOf calls with modern alternatives like includes and startsWith, and updates ESLint rules to enforce these practices. The review feedback identifies three key issues: a type assertion mismatch in backend.ts where optional chaining is used on supposedly non-nullable types, a redundant nullish coalescing operator in downloadableEmulators.ts where a type guard already guarantees a string, and an off-by-one substring index bug in profileReport.ts resulting from the change to startsWith.
| return ( | ||
| hasMessage(err) && | ||
| /Unknown system error/.test(err.message ?? "") && | ||
| (err.message ?? "").includes("Unknown system error") && |
There was a problem hiding this comment.
Since hasMessage(err) is a type guard that guarantees err.message is a string, the nullish coalescing operator (?? "") is redundant. Removing it simplifies the expression and improves readability.
| (err.message ?? "").includes("Unknown system error") && | |
| err.message.includes("Unknown system error") && |
There was a problem hiding this comment.
Addressed in commit d6542d9: Removed the redundant ?? "" fallback since hasMessage(err) type guard already guarantees that err.message is a string.
|
|
||
| export function extractJSON(line: string, input: any): string | null { | ||
| if (!input && !DATA_LINE_REGEX.test(line)) { | ||
| if (!input && !line.startsWith("data: ")) { |
There was a problem hiding this comment.
Since the prefix check has been modernized to line.startsWith('data: ') (which is 6 characters long), the subsequent line.substring(5) on line 30 is now inconsistent as it leaves a leading space in the string before parsing. Consider updating it to line.substring(6) or line.slice(6) to cleanly remove the entire prefix.
There was a problem hiding this comment.
Addressed in commit d6542d9: Updated line.substring(5) to line.substring(6) to correctly strip the 6-character "data: " prefix matching line.startsWith("data: "). Also added comprehensive unit tests for extractJSON covering SSE and non-SSE cases in src/profileReport.spec.ts.
| "@typescript-eslint/prefer-includes": "error", | ||
| "@typescript-eslint/prefer-regexp-exec": "warn", // TODO(bkendall): remove, allow to error. | ||
| "@typescript-eslint/prefer-string-starts-ends-with": "warn", // TODO(bkendall): remove, allow to error. | ||
| "@typescript-eslint/prefer-string-starts-ends-with": "error", |
There was a problem hiding this comment.
Please remove so that this just falls back to the default
There was a problem hiding this comment.
Removed @typescript-eslint/prefer-string-starts-ends-with from .eslintrc.js in commit 4abdb0b so that it falls back to the recommended default.
| "@typescript-eslint/no-use-before-define": ["warn", { functions: false, typedefs: false }], // TODO(bkendall): change to error. | ||
| "@typescript-eslint/no-var-requires": "warn", // TODO(bkendall): remove, allow to error. | ||
| "@typescript-eslint/prefer-includes": "warn", // TODO(bkendall): remove, allow to error. | ||
| "@typescript-eslint/prefer-includes": "error", |
There was a problem hiding this comment.
Please remove so that this just falls back to the default
There was a problem hiding this comment.
Removed @typescript-eslint/prefer-includes from .eslintrc.js in commit 4abdb0b so that it falls back to the recommended default.
…task-modernize-string-and-array-che
…des to use defaults (b/565073781)
Description
Modernizes legacy
indexOfandRegExpmembership and prefix checks across the codebase to use standardString.prototype.includes,Array.prototype.includes, andString.prototype.startsWith. Promotes@typescript-eslint/prefer-includesand@typescript-eslint/prefer-string-starts-ends-withfrom"warn"to"error"in.eslintrc.js.Resolves b/565073781 (parent goal b/565072516).
Changes
src/apphosting/backend.ts: Converted regex test on error code tomaybeNodeError?.cause?.code?.includes("HANDSHAKE_FAILURE").src/deploy/firestore/prepare.ts: Converted 5targets.indexOf(...) >= 0checks totargets.includes(...).src/emulator/apphosting/serve.ts: ConvertedfirebaseUtilPaths.indexOf(...) === -1to!firebaseUtilPaths.includes(...).src/emulator/auth/utils.ts: Converted/^\+/.test(phoneNumber)tophoneNumber.startsWith("+").src/emulator/dataconnect/pgliteServer.ts: Converted regex test toerr.message.includes("Database already exists").src/emulator/download.ts: Convertedfile.indexOf(...) < 0to!file.includes(...).src/emulator/downloadableEmulators.ts: Converted regex test to(err.message ?? "").includes("Unknown system error").src/firestore/validator.ts: Convertedvalid.indexOf(...) < 0to!valid.includes(...).src/profileReport.ts: Converted regex prefix test to!line.startsWith("data: ")and cleaned up unused constant..eslintrc.js: Promoted@typescript-eslint/prefer-includesand@typescript-eslint/prefer-string-starts-ends-withto"error".Verification
0 errorswith rules set to"error".npm run build: passed.