Back to all reviewers

Use modern null-safe operators

microsoft/playwright
Based on 3 comments
TSX

Prefer optional chaining (`?.`) and nullish coalescing (`??`) operators over verbose null checks and ternary expressions. These modern JavaScript operators provide cleaner, more readable code while preventing runtime errors from null or undefined values.

Null Handling TSX

Reviewer Prompt

Prefer optional chaining (?.) and nullish coalescing (??) operators over verbose null checks and ternary expressions. These modern JavaScript operators provide cleaner, more readable code while preventing runtime errors from null or undefined values.

Instead of explicit null checks:

// Avoid
const annotations = item.testCase ? [...item.testCase.annotations, ...item.testCase.results.flatMap(r => r.annotations)] : [];

// Prefer
const annotations = item.testCase?.results[0] ? [...item.testCase.annotations, ...item.testCase.results[0].annotations] : [];

For array access and method calls, use optional chaining with nullish coalescing:

// Avoid
annotations.push(...test.results[selectedResultIndex].annotations);

// Prefer  
annotations.push(...test.results[selectedResultIndex]?.annotations ?? []);

This approach reduces boilerplate code, improves readability, and provides built-in null safety without sacrificing functionality.

3
Comments Analyzed
TSX
Primary Language
Null Handling
Category

Source Discussions