| name | sonar-compliance-testing |
| description | SonarQube compliance rules for TypeScript/React code and tests (assertions, complexity, ternaries) — assign to frontend test phases |
🛡️ Sonar Best Practices to Avoid Issues
1. Tests - Mandatory Rules
it('should do something', () => {
render(<Component />);
});
it('should do something', () => {
render(<Component />);
expect(screen.getByText('Expected')).toBeInTheDocument();
});
2. Nested Ternaries (S3358)
const result = a ? (b ? 'x' : 'y') : 'z';
const getResult = () => {
if (!a) return 'z';
return b ? 'x' : 'y';
};
3. Cognitive Complexity (S3776)
function complexFunction() {
if (a) {
if (b) {
if (c) {
}
}
}
}
function complexFunction() {
if (!a) return handleNotA();
if (!b) return handleNotB();
return handleC(c);
}
4. Optional Chaining (S6582)
const value = obj && obj.prop && obj.prop.value;
const value = obj?.prop?.value;
5. Promise in void Context (S6544)
<button onClick={async () => await fetchData()}/>
<button onClick={() => { void fetchData(); }}>
// or
<button onClick={() => { fetchData().catch(console.error); }}>
6. Index as React Key (S6479)
{items.map((item, index) => <Item key={index} />)}
{items.map(item => <Item key={item.id} />)}
{items.map(item => <Item key={`${item.name}-${item.type}`} />)}
7. Read-Only Props (S6759)
interface Props {
value: string;
}
interface Props {
readonly value: string;
}
type Props = Readonly<{
value: string;
}>;
8. Redundant Fragment (S6749)
return <>{content}</>;
return content;
9. Context Value Memoization (S6481)
<Context.Provider value={{ user, setUser }}>
const value = useMemo(() => ({ user, setUser }), [user, setUser]);
<Context.Provider value={value}>
10. Redundant Type Alias (S6564)
type UUID = string;
type UUID = string & { readonly __brand: unique symbol };
📋 Validation Checklist Before Commit