Skip to content

Commit

Permalink
fix: fix character can't be parsed as a number
Browse files Browse the repository at this point in the history
- close #78
  • Loading branch information
cipchk committed Jan 16, 2025
1 parent cc12f1f commit e4cb6bb
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions examples/demo.less
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
);
}
}
4 changes: 2 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
4 changes: 3 additions & 1 deletion src/line-annotation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 12 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand All @@ -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',
Expand All @@ -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}`;
Expand All @@ -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',
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -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',
Expand All @@ -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
);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit e4cb6bb

Please sign in to comment.