Skip to content

Commit

Permalink
Merge pull request #43 from frenic/combinator-precedence-grouping
Browse files Browse the repository at this point in the history
Group syntax entities by combinator precedence
  • Loading branch information
frenic committed Apr 17, 2018
2 parents 479a957 + b223f71 commit aa33392
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 167 deletions.
164 changes: 147 additions & 17 deletions __tests__/parser.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,167 @@
import parse, { Combinator, Component } from '../src/parser';

describe('parsing of CSS syntax', () => {
it('combinators', () => {
expect(parse('something another-thing [ ] [ ] && | ||')).toMatchObject([
describe('parsing', () => {
it('parses combinators', () => {
expect(parse('something | another-thing')).toMatchObject([{}, { combinator: Combinator.SingleBar }, {}]);
expect(parse('something || another-thing')).toMatchObject([{}, { combinator: Combinator.DoubleBar }, {}]);
expect(parse('something && another-thing')).toMatchObject([{}, { combinator: Combinator.DoubleAmpersand }, {}]);
expect(parse('something another-thing')).toMatchObject([{}, { combinator: Combinator.Juxtaposition }, {}]);
});

it('parses components', () => {
expect(parse('something <something> [ something <something> ]')).toMatchObject([
{ component: Component.Keyword },
{},
{ combinator: Combinator.Juxtaposition },
{ component: Component.DataType },
{},
{ combinator: Combinator.Juxtaposition },
{
component: Component.Group,
entities: [{ component: Component.Keyword }, {}, { component: Component.DataType }],
},
]);
});

it('groups by combinator precedence', () => {
expect(parse('something | something || something && something something')).toMatchObject([
{},
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleAmpersand },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
],
},
],
},
]);

expect(parse('something something && something || something | something')).toMatchObject([
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.DoubleAmpersand },
{},
],
},
{ combinator: Combinator.DoubleBar },
{},
],
},
{ combinator: Combinator.SingleBar },
{},
]);

expect(parse('[ something | something || something && something ] something')).toMatchObject([
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.DoubleAmpersand }, {}],
},
],
},
],
},
{ combinator: Combinator.Juxtaposition },
{},
{ combinator: Combinator.DoubleAmpersand },
{ combinator: Combinator.SingleBar },
]);

expect(parse('something [ something && something || something | something ]')).toMatchObject([
{},
{ combinator: Combinator.Juxtaposition },
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.DoubleAmpersand }, {}],
},
{ combinator: Combinator.DoubleBar },
{},
],
},
{ combinator: Combinator.SingleBar },
{},
],
},
]);

expect(parse('something && something something || something')).toMatchObject([
{
component: Component.Group,
entities: [
{},
{ combinator: Combinator.DoubleAmpersand },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
],
},
{ combinator: Combinator.DoubleBar },
{},
]);
});

it('components', () => {
expect(parse('something <color> [ ]')).toMatchObject([
{ component: Component.Keyword },
expect(parse('something || something something && something')).toMatchObject([
{},
{ component: Component.DataType },
{ combinator: Combinator.DoubleBar },
{
component: Component.Group,
entities: [
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.DoubleAmpersand },
{},
],
},
]);

expect(parse('something | something something something')).toMatchObject([
{},
{ component: Component.Group },
{ combinator: Combinator.SingleBar },
{
component: Component.Group,
entities: [{}, { combinator: Combinator.Juxtaposition }, {}, { combinator: Combinator.Juxtaposition }, {}],
},
]);
});

it('group components', () => {
expect(parse('[ something | <color> ]')).toMatchObject([
expect(parse('something something something | something')).toMatchObject([
{
component: Component.Group,
entities: [{ component: Component.Keyword }, {}, { component: Component.DataType }],
entities: [{}, { combinator: Combinator.Juxtaposition }, {}, { combinator: Combinator.Juxtaposition }, {}],
},
{ combinator: Combinator.SingleBar },
{},
]);
});
});
4 changes: 2 additions & 2 deletions __tests__/typer.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import parse from '../src/parser';
import typing, { Type } from '../src/typer';

describe('typings of CSS syntax', () => {
describe('typing', () => {
it('types combinators', () => {
expect(typing(parse('something another-thing'))).toHaveLength(1);
expect(typing(parse('something && another-thing'))).toHaveLength(1);
expect(typing(parse('something | another-thing'))).toHaveLength(2);
expect(typing(parse('something || another-thing'))).toHaveLength(3);
expect(typing(parse('something | another-thing'))).toHaveLength(2);
});

it('types components', () => {
Expand Down
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3554,7 +3554,7 @@ type BorderImageOutsetProperty<TLength> = Globals | TLength | string | number;

type BorderImageRepeatProperty = Globals | "repeat" | "round" | "space" | "stretch" | string;

type BorderImageSliceProperty = Globals | "fill" | string | number;
type BorderImageSliceProperty = Globals | string | number;

type BorderImageSourceProperty = Globals | "none" | string;

Expand Down Expand Up @@ -3894,7 +3894,7 @@ type MaskBorderOutsetProperty<TLength> = Globals | TLength | string | number;

type MaskBorderRepeatProperty = Globals | "repeat" | "round" | "space" | "stretch" | string;

type MaskBorderSliceProperty = Globals | "fill" | string | number;
type MaskBorderSliceProperty = Globals | string | number;

type MaskBorderSourceProperty = Globals | "none" | string;

Expand Down Expand Up @@ -4184,7 +4184,7 @@ type BorderBottomProperty<TLength> = Globals | BrWidth<TLength> | BrStyle | Colo

type BorderColorProperty = Globals | Color | string;

type BorderImageProperty = Globals | "fill" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;
type BorderImageProperty = Globals | "none" | "repeat" | "round" | "space" | "stretch" | string | number;

type BorderInlineEndProperty<TLength> = Globals | BrWidth<TLength> | BrStyle | NamedColor | DeprecatedSystemColor | "currentcolor" | string;

Expand Down Expand Up @@ -4232,7 +4232,7 @@ type MarginProperty<TLength> = Globals | TLength | "auto" | string;

type MaskProperty<TLength> = Globals | MaskLayer<TLength> | string;

type MaskBorderProperty = Globals | "alpha" | "fill" | "luminance" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;
type MaskBorderProperty = Globals | "alpha" | "luminance" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;

type OffsetProperty<TLength> = Globals | Position<TLength> | GeometryBox | "auto" | "none" | string;

Expand Down
8 changes: 4 additions & 4 deletions index.js.flow
Original file line number Diff line number Diff line change
Expand Up @@ -3310,7 +3310,7 @@ type BorderImageOutsetProperty<TLength> = Globals | TLength | string | number;

type BorderImageRepeatProperty = Globals | "repeat" | "round" | "space" | "stretch" | string;

type BorderImageSliceProperty = Globals | "fill" | string | number;
type BorderImageSliceProperty = Globals | string | number;

type BorderImageSourceProperty = Globals | "none" | string;

Expand Down Expand Up @@ -3650,7 +3650,7 @@ type MaskBorderOutsetProperty<TLength> = Globals | TLength | string | number;

type MaskBorderRepeatProperty = Globals | "repeat" | "round" | "space" | "stretch" | string;

type MaskBorderSliceProperty = Globals | "fill" | string | number;
type MaskBorderSliceProperty = Globals | string | number;

type MaskBorderSourceProperty = Globals | "none" | string;

Expand Down Expand Up @@ -3940,7 +3940,7 @@ type BorderBottomProperty<TLength> = Globals | BrWidth<TLength> | BrStyle | Colo

type BorderColorProperty = Globals | Color | string;

type BorderImageProperty = Globals | "fill" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;
type BorderImageProperty = Globals | "none" | "repeat" | "round" | "space" | "stretch" | string | number;

type BorderInlineEndProperty<TLength> = Globals | BrWidth<TLength> | BrStyle | NamedColor | DeprecatedSystemColor | "currentcolor" | string;

Expand Down Expand Up @@ -3988,7 +3988,7 @@ type MarginProperty<TLength> = Globals | TLength | "auto" | string;

type MaskProperty<TLength> = Globals | MaskLayer<TLength> | string;

type MaskBorderProperty = Globals | "alpha" | "fill" | "luminance" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;
type MaskBorderProperty = Globals | "alpha" | "luminance" | "none" | "repeat" | "round" | "space" | "stretch" | string | number;

type OffsetProperty<TLength> = Globals | Position<TLength> | GeometryBox | "auto" | "none" | string;

Expand Down
4 changes: 2 additions & 2 deletions src/compat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Combinator, combinatorData, Component, componentData, componentGroupData, Entity, EntityType } from './parser';
import { Combinator, combinators, Component, componentData, componentGroupData, Entity, EntityType } from './parser';

const importsCache: { [cssPath: string]: MDN.PropertiesCompat | null } = {};

Expand Down Expand Up @@ -108,7 +108,7 @@ export function compatSyntax(data: MDN.CompatData, entities: EntityType[]): Enti
const alternativeEntities: EntityType[] = [entity];

for (const keyword of alternatives) {
alternativeEntities.push(combinatorData(Combinator.SingleBar), componentData(Component.Keyword, keyword));
alternativeEntities.push(combinators[Combinator.SingleBar], componentData(Component.Keyword, keyword));
}

compatEntities.push(componentGroupData(alternativeEntities));
Expand Down
Loading

0 comments on commit aa33392

Please sign in to comment.