Skip to content

Commit

Permalink
Output literals containing digits as numeric literals #4
Browse files Browse the repository at this point in the history
  • Loading branch information
frenic committed Feb 7, 2018
1 parent 1a83449 commit 938d20f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 23 deletions.
12 changes: 10 additions & 2 deletions __tests__/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ test('parser to parse CSS syntax group components', () => {
});

test('typer to type CSS syntax components', () => {
expect(typing(parse('something | <color>'))).toMatchObject([{ type: Type.Literal }, { type: Type.Alias }]);
expect(typing(parse('something | 100 | <color>'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.NumericLiteral },
{ type: Type.Alias },
]);
});

test('typer to type CSS syntax group components', () => {
expect(typing(parse('[ something | <color> ]'))).toMatchObject([{ type: Type.Literal }, { type: Type.Alias }]);
expect(typing(parse('[ something | 100 | <color> ]'))).toMatchObject([
{ type: Type.StringLiteral },
{ type: Type.NumericLiteral },
{ type: Type.Alias },
]);
});
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1146,7 +1146,7 @@ type FontVariantNumericProperty = All | NumericFigureValues | NumericSpacingValu

type FontVariantPositionProperty = All | "normal" | "sub" | "super";

type FontWeightProperty = All | "900" | "bold" | "bolder" | "lighter" | "100" | "200" | "normal" | "400" | "500" | "600" | "700" | "800" | "300";
type FontWeightProperty = All | "bold" | "bolder" | "lighter" | "normal" | 100 | 200 | 900 | 400 | 500 | 600 | 700 | 800 | 300;

type GridAreaProperty = All | GridLine;

Expand Down
10 changes: 4 additions & 6 deletions src/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ function unions(types: CompleteTypeType[]) {
return 'string';
case Type.Number:
return 'number';
case Type.Literal:
return quote(type.literal);
case Type.StringLiteral:
return JSON.stringify(type.literal);
case Type.NumericLiteral:
return type.literal;
case Type.Alias:
return type.name + generics(type.generics, true);
case Type.Length:
Expand All @@ -70,7 +72,3 @@ function generics(items: IGenerics[] | undefined, ignoreDefault = false) {
.map(({ name, defaults }) => (defaults && !ignoreDefault ? `${name} = ${defaults}` : name))
.join(', ')}>`;
}

export function quote(value: string) {
return `'${value}'`;
}
6 changes: 3 additions & 3 deletions src/pseudos.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as selectors from 'mdn-data/css/selectors.json';
import { ILiteral, Type } from './typer';
import { IStringLiteral, Type } from './typer';

const REGEX_PSEUDO_SELECTOR = /^:/;

const pseudos: ILiteral[] = [];
const pseudos: IStringLiteral[] = [];

for (const selector in selectors) {
if (REGEX_PSEUDO_SELECTOR.test(selector)) {
pseudos.push({ type: Type.Literal, literal: selector });
pseudos.push({ type: Type.StringLiteral, literal: selector });
}
}

Expand Down
35 changes: 24 additions & 11 deletions src/typer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import {
export enum Type {
Alias,
Length,
Literal,
StringLiteral,
NumericLiteral,
String,
Number,
}

export interface IBasic {
interface IBasic {
type: Type.String | Type.Number | Type.Length;
}

Expand All @@ -30,13 +31,18 @@ export type AliasType<TAliasAddons = {}> = {
dataTypeName?: string;
} & TAliasAddons;

export interface ILiteral {
type: Type.Literal;
export interface IStringLiteral {
type: Type.StringLiteral;
literal: string;
}

interface INumericLiteral {
type: Type.NumericLiteral;
literal: number;
}

// Yet another reminder; naming is hard
export type TypeType<TAliasAddons = {}> = IBasic | AliasType<TAliasAddons> | ILiteral;
export type TypeType<TAliasAddons = {}> = IBasic | AliasType<TAliasAddons> | IStringLiteral | INumericLiteral;

export const knownBasicDataTypes: { [name: string]: Type } = {
time: Type.String,
Expand All @@ -59,10 +65,17 @@ export default function typing(entities: EntityType[]): TypeType[] {
if (isComponent(entity)) {
switch (entity.component) {
case Component.Keyword:
types.push({
type: Type.Literal,
literal: entity.value,
});
if (String(Number(entity.value)) === entity.value) {
types.push({
type: Type.NumericLiteral,
literal: Number(entity.value),
});
} else {
types.push({
type: Type.StringLiteral,
literal: entity.value,
});
}
break;
case Component.DataType: {
const value = entity.value.slice(1, -1);
Expand Down Expand Up @@ -109,8 +122,8 @@ export default function typing(entities: EntityType[]): TypeType[] {
}

if (
groupType.type === Type.Literal &&
!types.every(type => !(type.type === Type.Literal && type.literal === groupType.literal))
groupType.type === Type.StringLiteral &&
!types.every(type => !(type.type === Type.StringLiteral && type.literal === groupType.literal))
) {
return false;
}
Expand Down

0 comments on commit 938d20f

Please sign in to comment.