Skip to content

Commit 1a2a33c

Browse files
authored
ref: Enable noUncheckedIndexedAccess TS config (#12461)
This PR enables the `noUncheckedIndexedAccess` TS config, ensuring that we always properly guard for array/object access. Sometimes, this leads to a bit of boilerplate, but enabling this def. found some places in the code where things could go wrong before. Closes #12440 Afterwards, we can look into enabling https://typescript-eslint.io/rules/no-unnecessary-condition/.
1 parent 1201eb2 commit 1a2a33c

File tree

152 files changed

+709
-621
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

152 files changed

+709
-621
lines changed

.github/workflows/build.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -896,15 +896,15 @@ jobs:
896896

897897
- name: Overwrite typescript version
898898
if: matrix.typescript
899-
run: yarn add --dev --ignore-workspace-root-check typescript@${{ matrix.typescript }}
899+
run: node ./scripts/use-ts-version.js ${{ matrix.typescript }}
900+
working-directory: dev-packages/node-integration-tests
900901

901902
- name: Run integration tests
902903
env:
903904
NODE_VERSION: ${{ matrix.node }}
904905
TS_VERSION: ${{ matrix.typescript }}
905-
run: |
906-
cd dev-packages/node-integration-tests
907-
yarn test
906+
working-directory: dev-packages/node-integration-tests
907+
run: yarn test
908908

909909
job_remix_integration_tests:
910910
name: Remix v${{ matrix.remix }} (Node ${{ matrix.node }}) Tests
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* eslint-disable no-console */
2+
const { execSync } = require('child_process');
3+
const { join } = require('path');
4+
const { writeFileSync } = require('fs');
5+
6+
const cwd = join(__dirname, '../../..');
7+
8+
const tsVersion = process.argv[2] || '3.8';
9+
10+
console.log(`Installing typescript@${tsVersion}...`);
11+
12+
execSync(`yarn add --dev --ignore-workspace-root-check typescript@${tsVersion}`, { stdio: 'inherit', cwd });
13+
14+
console.log('Removing unsupported tsconfig options...');
15+
16+
const baseTscConfigPath = join(cwd, 'packages/typescript/tsconfig.json');
17+
18+
const tsConfig = require(baseTscConfigPath);
19+
20+
// TS 3.8 fails build when it encounteres a config option it does not understand, so we remove it :(
21+
delete tsConfig.compilerOptions.noUncheckedIndexedAccess;
22+
23+
writeFileSync(baseTscConfigPath, JSON.stringify(tsConfig, null, 2));

dev-packages/node-integration-tests/suites/express/multiple-routers/complex-router/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ afterAll(() => {
99
conditionalTest({ min: 16 })('complex-router', () => {
1010
test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route', done => {
1111
// parse node.js major version
12-
const [major] = process.versions.node.split('.').map(Number);
12+
const [major = 0] = process.versions.node.split('.').map(Number);
1313
// Split test result base on major node version because regex d flag is support from node 16+
1414

1515
const EXPECTED_TRANSACTION =
@@ -36,7 +36,7 @@ conditionalTest({ min: 16 })('complex-router', () => {
3636

3737
test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route and original url has query params', done => {
3838
// parse node.js major version
39-
const [major] = process.versions.node.split('.').map(Number);
39+
const [major = 0] = process.versions.node.split('.').map(Number);
4040
// Split test result base on major node version because regex d flag is support from node 16+
4141
const EXPECTED_TRANSACTION =
4242
major >= 16
@@ -62,7 +62,7 @@ conditionalTest({ min: 16 })('complex-router', () => {
6262

6363
test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route and express used multiple middlewares with route and original url ends with trailing slash and has query params', done => {
6464
// parse node.js major version
65-
const [major] = process.versions.node.split('.').map(Number);
65+
const [major = 0] = process.versions.node.split('.').map(Number);
6666
// Split test result base on major node version because regex d flag is support from node 16+
6767
const EXPECTED_TRANSACTION =
6868
major >= 16

dev-packages/node-integration-tests/suites/express/multiple-routers/middle-layer-parameterized/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ afterAll(() => {
99
conditionalTest({ min: 16 })('middle-layer-parameterized', () => {
1010
test('should construct correct url with multiple parameterized routers, when param is also contain in middle layer route', done => {
1111
// parse node.js major version
12-
const [major] = process.versions.node.split('.').map(Number);
12+
const [major = 0] = process.versions.node.split('.').map(Number);
1313
// Split test result base on major node version because regex d flag is support from node 16+
1414
const EXPECTED_TRANSACTION =
1515
major >= 16

dev-packages/node-integration-tests/suites/public-api/LocalVariables/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ conditionalTest({ min: 18 })('LocalVariables integration', () => {
4444
.ignore('session')
4545
.expect({
4646
event: event => {
47-
for (const frame of event.exception?.values?.[0].stacktrace?.frames || []) {
47+
for (const frame of event.exception?.values?.[0]?.stacktrace?.frames || []) {
4848
expect(frame.vars).toBeUndefined();
4949
}
5050
},

dev-packages/node-integration-tests/suites/public-api/startSpan/with-nested-spans/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test('should report finished spans as children of the root transaction.', done =
1010
.expect({
1111
transaction: transaction => {
1212
const rootSpanId = transaction.contexts?.trace?.span_id;
13-
const span3Id = transaction.spans?.[1].span_id;
13+
const span3Id = transaction.spans?.[1]?.span_id;
1414

1515
expect(rootSpanId).toEqual(expect.any(String));
1616
expect(span3Id).toEqual(expect.any(String));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "../tsconfig.test.json",
3+
}

dev-packages/node-integration-tests/utils/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,13 @@ export class TestEnv {
196196
* @returns The extracted envelope.
197197
*/
198198
public async getEnvelopeRequest(options?: DataCollectorOptions): Promise<Array<Record<string, unknown>>> {
199-
return (await this.getMultipleEnvelopeRequest({ ...options, count: 1 }))[0];
199+
const requests = await this.getMultipleEnvelopeRequest({ ...options, count: 1 });
200+
201+
if (!requests[0]) {
202+
throw new Error('No requests found');
203+
}
204+
205+
return requests[0];
200206
}
201207

202208
/**
@@ -239,7 +245,7 @@ export class TestEnv {
239245
.post('/api/1337/envelope/', body => {
240246
const envelope = parseEnvelope(body);
241247

242-
if (envelopeType.includes(envelope[1].type as EnvelopeItemType)) {
248+
if (envelopeType.includes(envelope[1]?.type as EnvelopeItemType)) {
243249
envelopes.push(envelope);
244250
} else {
245251
return false;
@@ -296,7 +302,7 @@ export class TestEnv {
296302
.post('/api/1337/envelope/', body => {
297303
const envelope = parseEnvelope(body);
298304

299-
if (options.envelopeType.includes(envelope[1].type as EnvelopeItemType)) {
305+
if (options.envelopeType.includes(envelope[1]?.type as EnvelopeItemType)) {
300306
reqCount++;
301307
return true;
302308
}

dev-packages/overhead-metrics/src/util/github.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const octokit = new Octokit({
1313
// log: console,
1414
});
1515

16-
const [, owner, repo] = (await Git.repository).split('/');
16+
const [, owner, repo] = (await Git.repository).split('/') as [string, string, string];
1717
const defaultArgs = { owner, repo };
1818

1919
async function downloadArtifact(url: string, path: string): Promise<void> {
@@ -51,15 +51,10 @@ async function tryAddOrUpdateComment(commentBuilder: PrCommentBuilder): Promise<
5151
For example, refs/heads/feature-branch-1.
5252
*/
5353
let prNumber: number | undefined;
54-
if (
55-
typeof process.env.GITHUB_REF == 'string' &&
56-
process.env.GITHUB_REF.length > 0 &&
57-
process.env.GITHUB_REF.startsWith('refs/pull/')
58-
) {
59-
prNumber = parseInt(process.env.GITHUB_REF.split('/')[2]);
60-
console.log(
61-
`Determined PR number ${prNumber} based on GITHUB_REF environment variable: '${process.env.GITHUB_REF}'`,
62-
);
54+
const githubRef = process.env.GITHUB_REF;
55+
if (typeof githubRef == 'string' && githubRef.length > 0 && githubRef.startsWith('refs/pull/')) {
56+
prNumber = parseInt(githubRef.split('/')[2] as string);
57+
console.log(`Determined PR number ${prNumber} based on GITHUB_REF environment variable: '${githubRef}'`);
6358
} else if (!(await Git.branchIsBase)) {
6459
prNumber = (
6560
await octokit.rest.pulls.list({
@@ -155,15 +150,17 @@ export const GitHub = {
155150
status: 'success',
156151
});
157152

158-
if (workflowRuns.data.total_count == 0) {
153+
const firstRun = workflowRuns.data.workflow_runs[0];
154+
155+
if (workflowRuns.data.total_count == 0 || !firstRun) {
159156
console.warn(`Couldn't find any successful run for workflow '${workflow.name}'`);
160157
return;
161158
}
162159

163160
const artifact = (
164161
await octokit.actions.listWorkflowRunArtifacts({
165162
...defaultArgs,
166-
run_id: workflowRuns.data.workflow_runs[0].id,
163+
run_id: firstRun.id,
167164
})
168165
).data.artifacts.find(it => it.name == artifactName);
169166

dev-packages/test-utils/src/event-proxy-server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function startEventProxyServer(options: EventProxyServerOptions): P
5959
? zlib.gunzipSync(Buffer.concat(proxyRequestChunks)).toString()
6060
: Buffer.concat(proxyRequestChunks).toString();
6161

62-
const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0]);
62+
const envelopeHeader: EnvelopeItem[0] = JSON.parse(proxyRequestBody.split('\n')[0] as string);
6363

6464
const shouldForwardEventToSentry = options.forwardToSentry != null ? options.forwardToSentry : true;
6565

0 commit comments

Comments
 (0)