From e4cb6bbb4bbf772e31bcd6ef72457aa7b33884c7 Mon Sep 17 00:00:00 2001 From: cipchk Date: Thu, 16 Jan 2025 19:02:18 +0800 Subject: [PATCH] fix: fix character can't be parsed as a number - close #78 --- examples/demo.less | 4 ++++ src/hover.ts | 6 +++--- src/interface.ts | 4 ++-- src/line-annotation.ts | 4 +++- src/process.ts | 2 +- src/rules.ts | 13 ++++++++++++- 6 files changed, 25 insertions(+), 8 deletions(-) diff --git a/examples/demo.less b/examples/demo.less index f3a4740..d6f20aa 100644 --- a/examples/demo.less +++ b/examples/demo.less @@ -2,6 +2,10 @@ body { font-size: 48px; padding: 10px 12px 24px 28.8px; margin: 7.5px 1vw; + + &.remove { + font-size: 24px; + } } // cssrem-disable-next-line diff --git a/src/hover.ts b/src/hover.ts index 04d511c..a241c46 100644 --- a/src/hover.ts +++ b/src/hover.ts @@ -41,14 +41,14 @@ export default class implements HoverProvider { .map((rule) => rule.hoverFn!(text)) .filter((h) => h != null && h.documentation); if (cog.hover === 'onlyMark') { - results = results.filter((w) => !line.includes(`/* ${w.type} */`)); + results = results.filter((w) => !line.includes(`/* ${w!.type} */`)); } if (results.length === 0) return null; if (results.length === 1) - return new Hover(new MarkdownString(results[0].documentation)); + return new Hover(new MarkdownString(results[0]!.documentation)); return new Hover( - new MarkdownString(results.map((h) => `- ${h.documentation}`).join('\n')) + new MarkdownString(results.map((h) => `- ${h!.documentation}`).join('\n')) ); } } diff --git a/src/interface.ts b/src/interface.ts index d8a1132..8273563 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -61,9 +61,9 @@ export interface Rule { type: Type; all: RegExp; single?: RegExp; - fn: (text: string) => ConvertResult | undefined; + fn: (text: string) => ConvertResult | undefined | null; hover?: RegExp | null; - hoverFn?: (text: string) => HoverResult; + hoverFn?: (text: string) => HoverResult | undefined | null; } export type Type = diff --git a/src/line-annotation.ts b/src/line-annotation.ts index 4300458..5050ee0 100644 --- a/src/line-annotation.ts +++ b/src/line-annotation.ts @@ -119,7 +119,9 @@ export class LineAnnotation implements Disposable { text: str, rule: RULES.filter( (w) => w.hover && w.hover.test(str) && w.hoverFn != null - ).map((h) => h.hoverFn!(str)), + ) + .map((h) => h.hoverFn!(str)) + .filter((h) => h != null), })) .filter((item) => item.rule.length > 0); diff --git a/src/process.ts b/src/process.ts index 11eee4d..e35d60f 100644 --- a/src/process.ts +++ b/src/process.ts @@ -11,7 +11,7 @@ export class CssRemProcess { return null; } - return res.map((i) => i.rule.fn(i.text)!); + return res.map((i) => i.rule.fn(i.text)).filter((w) => w != null); } private convertAll(code: string, ignores: string[], type: Type): string { diff --git a/src/rules.ts b/src/rules.ts index 29c8bad..6e99c7c 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -22,6 +22,7 @@ export function resetRules(): void { single: /([-]?[\d.]+)px?$/, fn: (text) => { const px = parseFloat(text); + if (isNaN(px)) return null; const resultValue = +(px / cog.rootFontSize).toFixed(cog.fixedDigits); const value = cleanZero(resultValue) + 'rem'; const label = `${px}px -> ${value}`; @@ -47,6 +48,7 @@ export function resetRules(): void { hover: cog.remHover ? /([-]?[\d.]+)px/ : null, hoverFn: (pxText) => { const px = parseFloat(pxText); + if (isNaN(px)) return null; const rem = +(px / cog.rootFontSize).toFixed(cog.fixedDigits); return { type: 'remToPx', @@ -64,10 +66,11 @@ export function resetRules(): void { }, { type: 'remToPx', - all: /([-]?[\d.]+)rem/g, + all: /([-]?[\d.]+)(rem)/g, single: /([-]?[\d.]+)r(e|em)?$/, fn: (text) => { const px = parseFloat(text); + if (isNaN(px)) return null; const resultValue = +(px * cog.rootFontSize).toFixed(cog.fixedDigits); const value = cleanZero(resultValue) + 'px'; const label = `${px}rem -> ${value}`; @@ -93,6 +96,7 @@ export function resetRules(): void { hover: /([-]?[\d.]+)rem/, hoverFn: (remText) => { const rem = parseFloat(remText); + if (isNaN(rem)) return null; const px = +(rem * cog.rootFontSize).toFixed(cog.fixedDigits); return { type: 'remToPx', @@ -126,6 +130,7 @@ export function resetRules(): void { single: /([-]?[\d.]+)px?$/, fn: (text) => { const px = parseFloat(text); + if (isNaN(px)) return null; const resultValue = +(px / (cog.vwDesign / 100.0)).toFixed( cog.fixedDigits ); @@ -154,6 +159,7 @@ export function resetRules(): void { hover: cog.vwHover ? /([-]?[\d.]+)px/ : null, hoverFn: (pxText) => { const px = parseFloat(pxText); + if (isNaN(px)) return null; const vw = +(px / (cog.vwDesign / 100.0)).toFixed(cog.fixedDigits); return { type: 'pxToVw', @@ -177,6 +183,7 @@ export function resetRules(): void { single: /([-]?[\d.]+)vw?$/, fn: (text) => { const vw = parseFloat(text); + if (isNaN(vw)) return null; const resultValue = +(vw * (cog.vwDesign / 100.0)).toFixed( cog.fixedDigits ); @@ -205,6 +212,7 @@ export function resetRules(): void { hover: /([-]?[\d.]+)vw/, hoverFn: (rpxText) => { const vw = parseFloat(rpxText); + if (isNaN(vw)) return null; const px = +(vw * (cog.vwDesign / 100.0)).toFixed(cog.fixedDigits); return { type: 'vwToPx', @@ -240,6 +248,7 @@ export function resetRules(): void { single: /([-]?[\d.]+)px?$/, fn: (text) => { const px = parseFloat(text); + if (isNaN(px)) return null; const resultValue = +( px * (cog.wxssScreenWidth / cog.wxssDeviceWidth) @@ -273,6 +282,7 @@ export function resetRules(): void { single: /([-]?[\d.]+)r(p|px)?$/, fn: (text) => { const rpx = parseFloat(text); + if (isNaN(rpx)) return null; const resultValue = +( rpx / (cog.wxssScreenWidth / cog.wxssDeviceWidth) @@ -302,6 +312,7 @@ export function resetRules(): void { hover: /([-]?[\d.]+)rpx/, hoverFn: (rpxText) => { const rpx = parseFloat(rpxText); + if (isNaN(rpx)) return null; const px = +( rpx / (cog.wxssScreenWidth / cog.wxssDeviceWidth)