From 8ce43cef6e193cf8cead85eabc5b0a49c3c5122e Mon Sep 17 00:00:00 2001 From: Bastien Chanez Date: Wed, 2 Oct 2024 01:48:59 +0200 Subject: [PATCH] fix(magicNumbers): exclude numbers in comments --- src/rules/rrd/magicNumbers.test.ts | 6 +----- src/rules/rrd/magicNumbers.ts | 5 ++++- 2 files changed, 5 insertions(+), 6 deletions(-) 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 }) `, 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 }