Repository: rrd108/vue-mess-detector (ts)

Issue:
`magicNumbers` rule should skip comments matches
**Describe the bug**
The analyzer is detecting some numbers in comments lines (e.g ui/issues/**1289**) as magic numbers

**To Reproduce**
Run the analyzer in this code:
```js
export default defineAppConfig({
  ui: {
    primary: 'blue',
    gray: 'cool',
  },
  nuxtIcon: {}, // see https://github.com/nuxt/ui/issues/1289 and https://github.com/nuxt/ui/pull/1789
})
```

1. What is the exact code you are analyzing?
The code from above ⬆️

2. What is the output you are getting?
![image](https://github.com/user-attachments/assets/14e2042d-3139-4953-a2d3-a5e2a11878cb)

**Expected behavior**
It should ignore comments

**Screenshots**
N/A

**Used version number of vue-mess-detector:** `latest`

**Used version number of node & yarn:** `-`

**Additional context**
Reported via [Discord Server Help Channel](https://discord.com/channels/1280952778734305410/1281720249653395467/1290722684472266904)


Pull request:
fix(magicNumbers): exclude numbers in comments
### Summary
<!-- Provide a general summary of your changes in the title above -->
Removed comments from code before running number detection

### Description
<!-- Describe your changes in detail -->

### Related Issues
<!-- List any related issues or pull requests, using the format `Fixes #issue_number` -->
Fixes #370

### Type of Change
<!-- Please select the options that best describe your changes -->
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
- [ ] Documentation update

### Screenshots (if applicable)
<!-- Add screenshots to help expalin the changes -->


Patch:
diff --git a/src/rules/rrd/magicNumbers.ts b/src/rules/rrd/magicNumbers.ts
index c400fb1..b288654 100644
--- a/src/rules/rrd/magicNumbers.ts
+++ b/src/rules/rrd/magicNumbers.ts
@@ -2,6 +2,7 @@ import type { SFCScriptBlock } from '@vue/compiler-sfc'
 import type { FileCheckResult, Offense } from '../../types'
 
 import { anyOf, char, createRegExp, digit, global, linefeed, maybe, oneOrMore, wordBoundary } from 'magic-regexp'
+import { skipComments } from '../../helpers/skipComments'
 import getLineNumber from '../getLineNumber'
 
 const results: FileCheckResult[] = []
@@ -13,6 +14,8 @@ const checkMagicNumbers = (script: SFCScriptBlock | null, filePath: string) => {
     return
   }
 
+  const content = skipComments(script.content)
+
   const regex = createRegExp(
     maybe(oneOrMore(char)),
     anyOf(wordBoundary),
@@ -24,7 +27,7 @@ const checkMagicNumbers = (script: SFCScriptBlock | null, filePath: string) => {
   let match
   let lastLine = 0
   // eslint-disable-next-line no-cond-assign
-  while ((match = regex.exec(script.content)) !== null) {
+  while ((match = regex.exec(content)) !== null) {
     if (match[0].trim().startsWith('const') || match[0].trim().startsWith('let')) {
       return
     }


Test patch:
diff --git a/src/rules/rrd/magicNumbers.test.ts b/src/rules/rrd/magicNumbers.test.ts
index c12adb4..deca680 100644
--- a/src/rules/rrd/magicNumbers.test.ts
+++ b/src/rules/rrd/magicNumbers.test.ts
@@ -12,14 +12,10 @@ describe('checkMagicNumbers', () => {
     expect(result).toStrictEqual([])
   })
 
-  it.todo('should ignore numbers in comments', () => {
+  it('should ignore numbers in comments', () => {
     const script = {
       content: `
         export default defineAppConfig({
-          ui: {
-            primary: 'blue',
-            gray: 'cool',
-          },
           nuxtIcon: {}, // see https://github.com/nuxt/ui/issues/1289 and https://github.com/nuxt/ui/pull/1789
         })
       `,