Skip to content

Commit

Permalink
Merge pull request #371 from bchanez/fix/ignore-numbers-in-comments
Browse files Browse the repository at this point in the history
fix(magicNumbers): exclude numbers in comments
  • Loading branch information
rrd108 authored Oct 2, 2024
2 parents 3de9bf9 + 8ce43ce commit 305c19f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
6 changes: 1 addition & 5 deletions src/rules/rrd/magicNumbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
`,
Expand Down
5 changes: 4 additions & 1 deletion src/rules/rrd/magicNumbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[] = []
Expand All @@ -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),
Expand All @@ -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
}
Expand Down

0 comments on commit 305c19f

Please sign in to comment.