Skip to content

Commit 71f91a7

Browse files
Copilotfelickz
andcommitted
Address code review feedback - improve tests and add NaN validation
Co-authored-by: felickz <1760475+felickz@users.noreply.github.com>
1 parent 0623b45 commit 71f91a7

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

__tests__/main.test.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,36 @@ describe('main', () => {
66
})
77

88
describe('CWE ID normalization', () => {
9+
const codeQlCweTagPrefix = 'external/cwe/cwe-'
10+
11+
function normalizeCweId(tag: string): string {
12+
const cweId = tag.replace(codeQlCweTagPrefix, '')
13+
const normalizedId = String(parseInt(cweId, 10))
14+
return normalizedId
15+
}
16+
917
it('should handle CWE IDs with leading zeros', () => {
1018
// Test that cwe-099 maps to 99
11-
const cweIdWithLeadingZero = 'cwe-099'
12-
const cweIdPrefix = 'cwe-'
13-
const extractedId = cweIdWithLeadingZero.replace(cweIdPrefix, '')
14-
const normalizedId = String(parseInt(extractedId, 10))
15-
19+
const normalizedId = normalizeCweId('external/cwe/cwe-099')
1620
expect(normalizedId).toBe('99')
1721
})
1822

1923
it('should handle CWE IDs without leading zeros', () => {
2024
// Test that cwe-89 maps to 89
21-
const cweIdNoLeadingZero = 'cwe-89'
22-
const cweIdPrefix = 'cwe-'
23-
const extractedId = cweIdNoLeadingZero.replace(cweIdPrefix, '')
24-
const normalizedId = String(parseInt(extractedId, 10))
25-
25+
const normalizedId = normalizeCweId('external/cwe/cwe-89')
2626
expect(normalizedId).toBe('89')
2727
})
2828

2929
it('should handle CWE IDs with multiple leading zeros', () => {
3030
// Test that cwe-020 maps to 20
31-
const cweIdWithLeadingZeros = 'cwe-020'
32-
const cweIdPrefix = 'cwe-'
33-
const extractedId = cweIdWithLeadingZeros.replace(cweIdPrefix, '')
34-
const normalizedId = String(parseInt(extractedId, 10))
35-
31+
const normalizedId = normalizeCweId('external/cwe/cwe-020')
3632
expect(normalizedId).toBe('20')
3733
})
34+
35+
it('should return NaN for non-numeric CWE IDs', () => {
36+
// Test that invalid CWE IDs return NaN
37+
const normalizedId = normalizeCweId('external/cwe/cwe-abc')
38+
expect(normalizedId).toBe('NaN')
39+
})
3840
})
3941
})

dist/index.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ JSONPath({
105105
const cweId = tag.replace(codeQlCweTagPrefix, '')
106106
// Normalize CWE ID by converting to integer to remove leading zeros
107107
const normalizedCweId = String(parseInt(cweId, 10))
108+
// Skip if the CWE ID is not a valid number
109+
if (normalizedCweId === 'NaN') {
110+
continue
111+
}
108112
if (cweIds.includes(normalizedCweId)) {
109113
tags.push(securityStandardTag)
110114
tags.push(...cweCategories[normalizedCweId])

0 commit comments

Comments
 (0)