Skip to content

Commit

Permalink
Fix entry points by adding missed NestedSelector node type
Browse files Browse the repository at this point in the history
Entry points tests was reworked as well to prevent such an issue in the future
  • Loading branch information
lahmatiy committed Dec 14, 2022
1 parent 49b4006 commit 8a52f8d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## next

- Added `:host`, `:host()` and `:host-context()` pseudo class support (#216)
- Fixed `generator`, `parse` and `parse-selector` entry points by adding missed `NestedSelector` node type
- Removed npm > 7 version requirement (#218)

## 2.3.0 (November 30, 2022)

- Added [CSS Nesting](https://www.w3.org/TR/css-nesting-1/) support:
Expand Down
78 changes: 33 additions & 45 deletions lib/__tests/exports.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from 'fs';
import assert from 'assert';
import * as csstree from 'css-tree';
import * as tokenizer from 'css-tree/tokenizer';
import parse from 'css-tree/parser';
import parseSelector from 'css-tree/selector-parser';
Expand All @@ -11,35 +13,10 @@ import data from 'css-tree/definition-syntax-data';
import dataPatch from 'css-tree/definition-syntax-data-patch';
import * as utils from 'css-tree/utils';


const stringifyWithNoInfo = ast => JSON.stringify(ast, (key, value) => key !== 'loc' ? value : undefined, 4);
const css = '.a{}';
const expectedAst = {
type: 'StyleSheet',
children: [
{
type: 'Rule',
prelude: {
type: 'SelectorList',
children: [
{
type: 'Selector',
children: [
{
type: 'ClassSelector',
name: 'a'
}
]
}
]
},
block: {
type: 'Block',
children: []
}
}
]
};
const fixtureFilename = './fixtures/stringify.css';
const css = fs.readFileSync(fixtureFilename, 'utf-8');
const expectedAst = JSON.parse(fs.readFileSync(fixtureFilename.replace('.css', '.ast'), 'utf8'));

describe('exports / entry points', () => {
it('tokenizer', () => {
Expand Down Expand Up @@ -115,45 +92,56 @@ describe('exports / entry points', () => {
assert.strictEqual(stringifyWithNoInfo(ast), stringifyWithNoInfo(expectedAst));
});

it('selector-parser', () => {
const ast = parseSelector('.a');
assert.strictEqual(stringifyWithNoInfo(ast), stringifyWithNoInfo(expectedAst.children[0].prelude));
describe('selector-parser', () => {
const selectors = [];

// collect all the selectors
csstree.walk(expectedAst, function(node) {
if (node.type === 'SelectorList' || node.type === 'Selector') {
selectors.push({ css: csstree.generate(node), expected: node });

return csstree.walk.skip;
}
});

for (const test of selectors) {
it(test.css, () => {
const ast = parseSelector(test.css);
assert.strictEqual(stringifyWithNoInfo(ast), stringifyWithNoInfo(test.expected));
});
}
});

it('generator', () => {
assert.strictEqual(generate(expectedAst), css);
assert.strictEqual(generate(expectedAst), csstree.generate(expectedAst));
});

it('walker', () => {
const types = [];
const actualTypes = [];
const expectedTypes = [];

walk(expectedAst, node => types.push(node.type));
walk(expectedAst, node => actualTypes.push(node.type));
csstree.walk(expectedAst, node => expectedTypes.push(node.type));

assert.deepStrictEqual(types, [
'StyleSheet',
'Rule',
'SelectorList',
'Selector',
'ClassSelector',
'Block'
]);
assert.deepStrictEqual(actualTypes, expectedTypes);
});

it('convertor', () => {
const ast = parse(css);
const findFirstAtrule = ast => csstree.walk.find(ast, node => node.type === 'Atrule');

assert.strictEqual(ast.children instanceof utils.List, true);
assert.strictEqual(ast.children.first.prelude.children instanceof utils.List, true);
assert.strictEqual(findFirstAtrule(ast).prelude.children instanceof utils.List, true);

convertor.toPlainObject(ast);

assert.strictEqual(Array.isArray(ast.children), true);
assert.strictEqual(Array.isArray(ast.children[0].prelude.children), true);
assert.strictEqual(Array.isArray(findFirstAtrule(ast).prelude.children), true);

convertor.fromPlainObject(ast);

assert.strictEqual(ast.children instanceof utils.List, true);
assert.strictEqual(ast.children.first.prelude.children instanceof utils.List, true);
assert.strictEqual(findFirstAtrule(ast).prelude.children instanceof utils.List, true);

assert.deepStrictEqual(Object.keys(convertor).sort(), [
'fromPlainObject',
Expand Down
1 change: 1 addition & 0 deletions lib/syntax/node/index-generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { generate as IdSelector } from './IdSelector.js';
export { generate as MediaFeature } from './MediaFeature.js';
export { generate as MediaQuery } from './MediaQuery.js';
export { generate as MediaQueryList } from './MediaQueryList.js';
export { generate as NestingSelector } from './NestingSelector.js';
export { generate as Nth } from './Nth.js';
export { generate as Number } from './Number.js';
export { generate as Operator } from './Operator.js';
Expand Down
1 change: 1 addition & 0 deletions lib/syntax/node/index-parse-selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { parse as ClassSelector } from './ClassSelector.js';
export { parse as Combinator } from './Combinator.js';
export { parse as Identifier } from './Identifier.js';
export { parse as IdSelector } from './IdSelector.js';
export { parse as NestingSelector } from './NestingSelector.js';
export { parse as Nth } from './Nth.js';
export { parse as Percentage } from './Percentage.js';
export { parse as PseudoClassSelector } from './PseudoClassSelector.js';
Expand Down
1 change: 1 addition & 0 deletions lib/syntax/node/index-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { parse as IdSelector } from './IdSelector.js';
export { parse as MediaFeature } from './MediaFeature.js';
export { parse as MediaQuery } from './MediaQuery.js';
export { parse as MediaQueryList } from './MediaQueryList.js';
export { parse as NestingSelector } from './NestingSelector.js';
export { parse as Nth } from './Nth.js';
export { parse as Number } from './Number.js';
export { parse as Operator } from './Operator.js';
Expand Down

0 comments on commit 8a52f8d

Please sign in to comment.