Skip to content

Commit 170e436

Browse files
committed
Redo AstRule API for further extension.
1 parent 0e517dc commit 170e436

File tree

7 files changed

+30
-60
lines changed

7 files changed

+30
-60
lines changed

docs/interfaces/AstFactory.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ const selector = ast.selector({
3030
})
3131
],
3232
pseudoElement: 'before',
33-
nestedRule: {
34-
rule: ast.rule({tag: ast.wildcardTag()}),
35-
combinator: '>'
36-
}
33+
nestedRule: ast.rule({combinator: '>', tag: ast.wildcardTag()})
3734
})
3835
]
3936
});
@@ -488,7 +485,7 @@ ___
488485

489486
### rule
490487

491-
**rule**: (`props?`: { `attributes?`: [`AstAttribute`](AstAttribute.md)[] ; `classNames?`: `string`[] ; `ids?`: `string`[] ; `nestedRule?`: { `combinator?`: `string` ; `rule`: [`AstRule`](AstRule.md) } ; `pseudoClasses?`: [`AstPseudoClass`](AstPseudoClass.md)[] ; `pseudoElement?`: `string` ; `tag?`: [`AstTagName`](AstTagName.md) \| [`AstWildcardTag`](AstWildcardTag.md) }) => [`AstRule`](AstRule.md)
488+
**rule**: (`props?`: { `attributes?`: [`AstAttribute`](AstAttribute.md)[] ; `classNames?`: `string`[] ; `combinator?`: `string` ; `ids?`: `string`[] ; `nestedRule?`: [`AstRule`](AstRule.md) ; `pseudoClasses?`: [`AstPseudoClass`](AstPseudoClass.md)[] ; `pseudoElement?`: `string` ; `tag?`: [`AstTagName`](AstTagName.md) \| [`AstWildcardTag`](AstWildcardTag.md) }) => [`AstRule`](AstRule.md)
492489

493490
#### Type declaration
494491

@@ -501,10 +498,9 @@ ___
501498
| `props?` | `Object` | - |
502499
| `props.attributes?` | [`AstAttribute`](AstAttribute.md)[] | List of attributes (i.e. `"[href][role=button]"` -> `[{name: 'href'}, {name: 'role', operator: '=', value: {type: 'String', value: 'button'}}]`) |
503500
| `props.classNames?` | `string`[] | List of CSS classes (i.e. `".c1.c2"` -> `['c1', 'c2']`) |
501+
| `props.combinator?` | `string` | Rule combinator which was used to nest this rule (i.e. `">"` in case of `"div > span"` if the current rule is `"span"`). |
504502
| `props.ids?` | `string`[] | List of IDs (i.e. `"#root"` -> `['root']`). |
505-
| `props.nestedRule?` | `Object` | Nested rule and combinator if specified (i.e. `"div > span"`). |
506-
| `props.nestedRule.combinator?` | `string` | Nested rule combinator (i.e. `">"` in case of `"div > span"`). |
507-
| `props.nestedRule.rule` | [`AstRule`](AstRule.md) | Nested rule definition. |
503+
| `props.nestedRule?` | [`AstRule`](AstRule.md) | Nested rule if specified (i.e. `"div > span"`). |
508504
| `props.pseudoClasses?` | [`AstPseudoClass`](AstPseudoClass.md)[] | Pseudo-classes (i.e. `":link"` -> `[{name: 'link'}]`). |
509505
| `props.pseudoElement?` | `string` | Pseudo-element (i.e. `"::before"` -> `'before'`). |
510506
| `props.tag?` | [`AstTagName`](AstTagName.md) \| [`AstWildcardTag`](AstWildcardTag.md) | Tag definition. Can be either TagName (i.e. `"div"`) or WildcardTag (`"*"`) if defined. |

docs/interfaces/AstRule.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Generated by [ast.rule](AstFactory.md#rule).
1212

1313
- [attributes](AstRule.md#attributes)
1414
- [classNames](AstRule.md#classnames)
15+
- [combinator](AstRule.md#combinator)
1516
- [ids](AstRule.md#ids)
1617
- [nestedRule](AstRule.md#nestedrule)
1718
- [pseudoClasses](AstRule.md#pseudoclasses)
@@ -37,6 +38,14 @@ List of CSS classes (i.e. `".c1.c2"` -> `['c1', 'c2']`)
3738

3839
___
3940

41+
### combinator
42+
43+
`Optional` **combinator**: `string`
44+
45+
Rule combinator which was used to nest this rule (i.e. `">"` in case of `"div > span"` if the current rule is `"span"`).
46+
47+
___
48+
4049
### ids
4150

4251
`Optional` **ids**: `string`[]
@@ -47,16 +56,9 @@ ___
4756

4857
### nestedRule
4958

50-
`Optional` **nestedRule**: `Object`
51-
52-
Nested rule and combinator if specified (i.e. `"div > span"`).
53-
54-
#### Type declaration
59+
`Optional` **nestedRule**: [`AstRule`](AstRule.md)
5560

56-
| Name | Type | Description |
57-
| :------ | :------ | :------ |
58-
| `combinator?` | `string` | Nested rule combinator (i.e. `">"` in case of `"div > span"`). |
59-
| `rule` | [`AstRule`](AstRule.md) | Nested rule definition. |
61+
Nested rule if specified (i.e. `"div > span"`).
6062

6163
___
6264

docs/modules.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,7 @@ const selector = ast.selector({
110110
})
111111
],
112112
pseudoElement: 'before',
113-
nestedRule: {
114-
rule: ast.rule({tag: ast.wildcardTag()}),
115-
combinator: '>'
116-
}
113+
nestedRule: ast.rule({combinator: '>', tag: ast.wildcardTag()})
117114
})
118115
]
119116
});

src/ast.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,10 @@ export interface AstRule {
3131
pseudoClasses?: AstPseudoClass[];
3232
/** List of attributes (i.e. `"[href][role=button]"` -> `[{name: 'href'}, {name: 'role', operator: '=', value: {type: 'String', value: 'button'}}]`) */
3333
attributes?: AstAttribute[];
34-
/** Nested rule and combinator if specified (i.e. `"div > span"`). */
35-
nestedRule?: {
36-
/** Nested rule combinator (i.e. `">"` in case of `"div > span"`). */
37-
combinator?: string;
38-
/** Nested rule definition. */
39-
rule: AstRule;
40-
};
34+
/** Rule combinator which was used to nest this rule (i.e. `">"` in case of `"div > span"` if the current rule is `"span"`). */
35+
combinator?: string;
36+
/** Nested rule if specified (i.e. `"div > span"`). */
37+
nestedRule?: AstRule;
4138
}
4239

4340
/**
@@ -261,10 +258,7 @@ type AstFactoryBase = {[K in keyof ToAstFactory<AstEntity>]: ToAstFactory<AstEnt
261258
* })
262259
* ],
263260
* pseudoElement: 'before',
264-
* nestedRule: {
265-
* rule: ast.rule({tag: ast.wildcardTag()}),
266-
* combinator: '>'
267-
* }
261+
* nestedRule: ast.rule({combinator: '>', tag: ast.wildcardTag()})
268262
* })
269263
* ]
270264
* });
@@ -301,10 +295,7 @@ export interface AstFactory extends AstFactoryBase {}
301295
* })
302296
* ],
303297
* pseudoElement: 'before',
304-
* nestedRule: {
305-
* rule: ast.rule({tag: ast.wildcardTag()}),
306-
* combinator: '>'
307-
* }
298+
* nestedRule: ast.rule({combinator: '>', tag: ast.wildcardTag()})
308299
* })
309300
* ]
310301
* });

src/parser.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -686,10 +686,8 @@ export function createParser(
686686
if (!isEof() && !is(',') && !is(')')) {
687687
const combinator = matchMulticharIndex(combinatorsIndex);
688688
skipWhitespace();
689-
rule.nestedRule = {
690-
combinator,
691-
rule: parseRule()
692-
};
689+
rule.nestedRule = parseRule();
690+
rule.nestedRule.combinator = combinator;
693691
}
694692
return rule as AstRule;
695693
}

src/render.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export function render(entity: AstSelector | AstRule): string {
132132
if (nestedRule.combinator) {
133133
result += ` ${nestedRule.combinator}`;
134134
}
135-
result += ` ${render(nestedRule.rule)}`;
135+
result += ` ${render(nestedRule)}`;
136136
}
137137
return result;
138138
}

test/parser.test.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,9 +1316,7 @@ describe('parse()', () => {
13161316
rules: [
13171317
ast.rule({
13181318
tag: ast.tagName({name: 'div'}),
1319-
nestedRule: {
1320-
rule: ast.rule({classNames: ['class']})
1321-
}
1319+
nestedRule: ast.rule({classNames: ['class']})
13221320
})
13231321
]
13241322
})
@@ -1330,10 +1328,7 @@ describe('parse()', () => {
13301328
rules: [
13311329
ast.rule({
13321330
tag: ast.tagName({name: 'div'}),
1333-
nestedRule: {
1334-
rule: ast.rule({classNames: ['class']}),
1335-
combinator: '>'
1336-
}
1331+
nestedRule: ast.rule({classNames: ['class'], combinator: '>'})
13371332
})
13381333
]
13391334
})
@@ -1345,10 +1340,7 @@ describe('parse()', () => {
13451340
rules: [
13461341
ast.rule({
13471342
tag: ast.tagName({name: 'div'}),
1348-
nestedRule: {
1349-
rule: ast.rule({classNames: ['class']}),
1350-
combinator: '>'
1351-
}
1343+
nestedRule: ast.rule({classNames: ['class'], combinator: '>'})
13521344
})
13531345
]
13541346
})
@@ -1360,10 +1352,7 @@ describe('parse()', () => {
13601352
rules: [
13611353
ast.rule({
13621354
tag: ast.tagName({name: 'div'}),
1363-
nestedRule: {
1364-
rule: ast.rule({classNames: ['class']}),
1365-
combinator: '||'
1366-
}
1355+
nestedRule: ast.rule({classNames: ['class'], combinator: '||'})
13671356
})
13681357
]
13691358
})
@@ -1375,10 +1364,7 @@ describe('parse()', () => {
13751364
rules: [
13761365
ast.rule({
13771366
tag: ast.tagName({name: 'div'}),
1378-
nestedRule: {
1379-
rule: ast.rule({classNames: ['class']}),
1380-
combinator: '||'
1381-
}
1367+
nestedRule: ast.rule({classNames: ['class'], combinator: '||'})
13821368
})
13831369
]
13841370
})

0 commit comments

Comments
 (0)