Skip to content

Commit 5573ee1

Browse files
committed
Use ESM
1 parent 6ad87e6 commit 5573ee1

File tree

6 files changed

+84
-77
lines changed

6 files changed

+84
-77
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
.DS_Store
22
*.log
3-
.nyc_output/
43
coverage/
54
node_modules/
65
yarn.lock

.prettierignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
coverage/
2-
*.json
32
*.md

index.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
1-
'use strict'
2-
3-
module.exports = fromSelector
4-
5-
var h = require('hastscript')
6-
var s = require('hastscript/svg')
7-
var zwitch = require('zwitch')
8-
var Parser = require('css-selector-parser').CssSelectorParser
1+
import {h, s} from 'hastscript'
2+
import {zwitch} from 'zwitch'
3+
import {CssSelectorParser} from 'css-selector-parser'
94

105
var compile = zwitch('type', {
116
handlers: {
12-
selectors: selectors,
13-
ruleSet: ruleSet,
14-
rule: rule
7+
selectors,
8+
ruleSet,
9+
rule
1510
}
1611
})
1712

18-
var parser = new Parser()
13+
var parser = new CssSelectorParser()
1914

2015
parser.registerNestingOperators('>', '+', '~')
2116
// Register these so we can throw nicer errors.
2217
parser.registerAttrEqualityMods('~', '|', '^', '$', '*')
2318

24-
function fromSelector(selector, space) {
19+
export function fromSelector(selector, space) {
2520
var config = {space: (space && space.space) || space || 'html', root: true}
2621

27-
return compile(parser.parse(selector || ''), config) || build(config.space)()
22+
return (
23+
compile(parser.parse(selector || ''), config) || build(config.space)('')
24+
)
2825
}
2926

3027
function selectors() {
@@ -37,7 +34,7 @@ function ruleSet(query, config) {
3734

3835
function rule(query, config) {
3936
var parentSpace = config.space
40-
var name = query.tagName === '*' ? '' : query.tagName
37+
var name = query.tagName === '*' ? '' : query.tagName || ''
4138
var space = parentSpace === 'html' && name === 'svg' ? 'svg' : parentSpace
4239
var sibling
4340
var operator
@@ -61,7 +58,7 @@ function rule(query, config) {
6158
pseudosToHast(query.pseudos || []),
6259
attrsToHast(query.attrs || [])
6360
),
64-
!query.rule || sibling ? [] : compile(query.rule, {space: space})
61+
!query.rule || sibling ? [] : compile(query.rule, {space})
6562
)
6663

6764
return sibling ? [node, compile(query.rule, {space: parentSpace})] : node

package.json

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,30 @@
2626
"contributors": [
2727
"Titus Wormer <tituswormer@gmail.com> (https://wooorm.com)"
2828
],
29+
"sideEffects": false,
30+
"type": "module",
31+
"main": "index.js",
2932
"files": [
3033
"index.js"
3134
],
3235
"dependencies": {
33-
"css-selector-parser": "^1.3.0",
34-
"hastscript": "^6.0.0",
35-
"zwitch": "^1.0.0"
36+
"css-selector-parser": "^1.0.0",
37+
"hastscript": "^7.0.0",
38+
"zwitch": "^2.0.0"
3639
},
3740
"devDependencies": {
38-
"nyc": "^15.0.0",
41+
"c8": "^7.0.0",
3942
"prettier": "^2.0.0",
4043
"remark-cli": "^9.0.0",
4144
"remark-preset-wooorm": "^8.0.0",
4245
"tape": "^5.0.0",
43-
"unist-builder": "^2.0.0",
44-
"xo": "^0.38.0"
46+
"unist-builder": "^3.0.0",
47+
"xo": "^0.39.0"
4548
},
4649
"scripts": {
4750
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix",
48-
"test-api": "node test",
49-
"test-coverage": "nyc --reporter lcov tape test.js",
51+
"test-api": "node test.js",
52+
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js",
5053
"test": "npm run format && npm run test-coverage"
5154
},
5255
"prettier": {
@@ -59,17 +62,11 @@
5962
},
6063
"xo": {
6164
"prettier": true,
62-
"esnext": false,
6365
"rules": {
64-
"unicorn/prefer-includes": "off"
66+
"no-var": "off",
67+
"prefer-arrow-callback": "off"
6568
}
6669
},
67-
"nyc": {
68-
"check-coverage": true,
69-
"lines": 100,
70-
"functions": 100,
71-
"branches": 100
72-
},
7370
"remarkConfig": {
7471
"plugins": [
7572
"preset-wooorm"

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
## Install
1414

15+
This package is [ESM only](https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c):
16+
Node 12+ is needed to use it and it must be `import`ed instead of `require`d.
17+
1518
[npm][]:
1619

1720
```sh
@@ -21,9 +24,9 @@ npm install hast-util-from-selector
2124
## Use
2225

2326
```js
24-
var from = require('hast-util-from-selector')
27+
import {fromSelector} from 'hast-util-from-selector'
2528

26-
console.log(from('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10]'))
29+
console.log(fromSelector('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10]'))
2730
```
2831

2932
Yields:
@@ -53,6 +56,9 @@ Yields:
5356

5457
## API
5558

59+
This package exports the following identifiers: `fromSelector`.
60+
There is no default export.
61+
5662
### `fromSelector([selector][, options])`
5763

5864
Create one or more [*element*][element] [*node*][node]s from a CSS selector.

test.js

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,159 @@
1-
'use strict'
2-
3-
var test = require('tape')
4-
var h = require('hastscript')
5-
var s = require('hastscript/svg')
6-
var from = require('.')
1+
import test from 'tape'
2+
import {h, s} from 'hastscript'
3+
import {fromSelector} from './index.js'
74

85
test('fromSelector()', function (t) {
9-
t.equal(typeof from, 'function', 'should expose a function')
10-
116
t.throws(
127
function () {
13-
from('@supports (transform-origin: 5% 5%) {}')
8+
fromSelector('@supports (transform-origin: 5% 5%) {}')
149
},
1510
/Error: Rule expected but "@" found/,
1611
'should throw w/ invalid selector'
1712
)
1813

1914
t.throws(
2015
function () {
21-
from('a, b')
16+
fromSelector('a, b')
2217
},
2318
/Error: Cannot handle selector list/,
2419
'should throw w/ multiple selector'
2520
)
2621

2722
t.throws(
2823
function () {
29-
from('a + b')
24+
fromSelector('a + b')
3025
},
3126
/Error: Cannot handle sibling combinator `\+` at root/,
3227
'should throw w/ next-sibling combinator at root'
3328
)
3429

3530
t.throws(
3631
function () {
37-
from('a ~ b')
32+
fromSelector('a ~ b')
3833
},
3934
/Error: Cannot handle sibling combinator `~` at root/,
4035
'should throw w/ subsequent-sibling combinator at root'
4136
)
4237

4338
t.throws(
4439
function () {
45-
from('[foo%=bar]')
40+
fromSelector('[foo%=bar]')
4641
},
4742
/Error: Expected "=" but "%" found./,
4843
'should throw w/ attribute modifiers'
4944
)
5045

5146
t.throws(
5247
function () {
53-
from('[foo~=bar]')
48+
fromSelector('[foo~=bar]')
5449
},
5550
/Error: Cannot handle attribute equality modifier `~=`/,
5651
'should throw w/ attribute modifiers'
5752
)
5853

5954
t.throws(
6055
function () {
61-
from(':active')
56+
fromSelector(':active')
6257
},
6358
/Error: Cannot handle pseudo-selector `active`/,
6459
'should throw on pseudo classes'
6560
)
6661

6762
t.throws(
6863
function () {
69-
from(':nth-foo(2n+1)')
64+
fromSelector(':nth-foo(2n+1)')
7065
},
7166
/Error: Cannot handle pseudo-selector `nth-foo`/,
7267
'should throw on pseudo class “functions”'
7368
)
7469

7570
t.throws(
7671
function () {
77-
from('::before')
72+
fromSelector('::before')
7873
},
7974
/Error: Cannot handle pseudo-element or empty pseudo-class/,
8075
'should throw on invalid pseudo elements'
8176
)
8277

83-
t.deepEqual(from(), h(), 'should support no selector')
84-
t.deepEqual(from(''), h(), 'should support the empty string')
85-
t.deepEqual(from(' '), h(), 'should support whitespace only')
86-
t.deepEqual(from('*'), h(), 'should support the universal selector')
78+
t.deepEqual(fromSelector(), h(''), 'should support no selector')
79+
t.deepEqual(fromSelector(''), h(''), 'should support the empty string')
80+
t.deepEqual(fromSelector(' '), h(''), 'should support whitespace only')
81+
t.deepEqual(fromSelector('*'), h(''), 'should support the universal selector')
8782

8883
t.deepEqual(
89-
from('p i s'),
84+
fromSelector('p i s'),
9085
h('p', h('i', h('s'))),
9186
'should support the descendant combinator'
9287
)
9388

9489
t.deepEqual(
95-
from('p > i > s'),
90+
fromSelector('p > i > s'),
9691
h('p', h('i', h('s'))),
9792
'should support the child combinator'
9893
)
9994

10095
t.deepEqual(
101-
from('p i + s'),
96+
fromSelector('p i + s'),
10297
h('p', [h('i'), h('s')]),
10398
'should support the next-sibling combinator'
10499
)
105100

106101
t.deepEqual(
107-
from('p i ~ s'),
102+
fromSelector('p i ~ s'),
108103
h('p', [h('i'), h('s')]),
109104
'should support the subsequent-sibling combinator'
110105
)
111106

112-
t.deepEqual(from('a'), h('a'), 'should support a tag name')
113-
t.deepEqual(from('.a'), h('.a'), 'should support a class')
114-
t.deepEqual(from('a.b'), h('a.b'), 'should support a tag and a class')
115-
t.deepEqual(from('#b'), h('#b'), 'should support an id')
116-
t.deepEqual(from('a#b'), h('a#b'), 'should support a tag and an id')
117-
t.deepEqual(from('a#b.c.d'), h('a#b.c.d'), 'should support all together')
118-
t.deepEqual(from('a#b#c'), h('a#c'), 'should use the last id')
119-
t.deepEqual(from('A').tagName, 'a', 'should normalize casing')
107+
t.deepEqual(fromSelector('a'), h('a'), 'should support a tag name')
108+
t.deepEqual(fromSelector('.a'), h('.a'), 'should support a class')
109+
t.deepEqual(fromSelector('a.b'), h('a.b'), 'should support a tag and a class')
110+
t.deepEqual(fromSelector('#b'), h('#b'), 'should support an id')
111+
t.deepEqual(fromSelector('a#b'), h('a#b'), 'should support a tag and an id')
112+
t.deepEqual(
113+
fromSelector('a#b.c.d'),
114+
h('a#b.c.d'),
115+
'should support all together'
116+
)
117+
t.deepEqual(fromSelector('a#b#c'), h('a#c'), 'should use the last id')
118+
t.deepEqual(fromSelector('A').tagName, 'a', 'should normalize casing')
120119

121-
t.deepEqual(from('[a]'), h('', {a: true}), 'should support attributes (#1)')
122-
t.deepEqual(from('[a=b]'), h('', {a: 'b'}), 'should support attributes (#2)')
120+
t.deepEqual(
121+
fromSelector('[a]'),
122+
h('', {a: true}),
123+
'should support attributes (#1)'
124+
)
125+
t.deepEqual(
126+
fromSelector('[a=b]'),
127+
h('', {a: 'b'}),
128+
'should support attributes (#2)'
129+
)
123130

124131
t.deepEqual(
125-
from('.a.b[class=c]'),
132+
fromSelector('.a.b[class=c]'),
126133
h('.a.b.c'),
127134
'should support class and class attributes'
128135
)
129136

130-
t.deepEqual(from('altGlyph').tagName, 'altglyph', 'space (#1)')
137+
t.deepEqual(fromSelector('altGlyph').tagName, 'altglyph', 'space (#1)')
131138

132-
t.deepEqual(from('altGlyph', 'svg').tagName, 'altGlyph', 'space (#2)')
139+
t.deepEqual(fromSelector('altGlyph', 'svg').tagName, 'altGlyph', 'space (#2)')
133140

134141
t.deepEqual(
135-
from('svg altGlyph').children[0].tagName,
142+
fromSelector('svg altGlyph').children[0].tagName,
136143
'altGlyph',
137144
'space (#3)'
138145
)
139146

140147
t.deepEqual(
141-
from('div svg + altGlyph').children[1].tagName,
148+
fromSelector('div svg + altGlyph').children[1].tagName,
142149
'altglyph',
143150
'space (#4)'
144151
)
145152

146153
t.deepEqual(
147-
from('p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10] altGlyph'),
154+
fromSelector(
155+
'p svg[viewbox=0 0 10 10] circle[cx=10][cy=10][r=10] altGlyph'
156+
),
148157
h('p', [
149158
s('svg', {viewBox: '0 0 10 10'}, [
150159
s('circle', {cx: '10', cy: '10', r: '10'}, [s('altGlyph')])

0 commit comments

Comments
 (0)