Skip to content

Commit 6c37bc9

Browse files
committed
CLean up code from types
1 parent cb93b65 commit 6c37bc9

4 files changed

Lines changed: 33 additions & 140 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
# Change Log
22
This project adheres to [Semantic Versioning](http://semver.org/).
33

4-
## Upcoming...
5-
* ... <!-- Add new lines here. -->
6-
* fix: Correctly handle `with`/`without` parameters on `@at-root`
7-
* feat: Add option `rootRuleName` to rename the custom `@at-root` rule
8-
* fix: Errors when handling sibling `@at-root` rule blocks
9-
* fix: Move all preceeding comments with rule
10-
* fix: `@layer` blocks should also bubble
11-
124
## 5.0.6
135
* Fixed custom at-rules nesting (by @bsak-shell).
146

index.js

Lines changed: 26 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
// @ts-check
21
const { Rule, AtRule } = require('postcss')
32
let parser = require('postcss-selector-parser')
43

5-
/** @typedef {import('postcss').Container} Container */
6-
/** @typedef {import('postcss').ChildNode} ChildNode */
7-
/** @typedef {import('postcss').Comment} Comment */
8-
/** @typedef {import('postcss').Declaration} Declaration */
9-
/** @typedef {import('postcss').Rule} PostcssRule */
10-
/** @typedef {typeof import('postcss').Rule} RuleConstructor */
11-
/** @typedef {parser.Root} Root */
12-
/** @typedef {parser.Node} Node */
13-
/** @typedef {parser.Selector} Selector */
14-
/** @typedef {Record<string, true>} RuleMap Simple lookup table for \@-rules */
15-
164
/**
175
* Run a selector string through postcss-selector-parser
18-
*
19-
* @param {string} rawSelector
20-
* @param {PostcssRule} [rule]
21-
* @returns {Selector}
226
*/
237
function parse(rawSelector, rule) {
24-
/** @type {Root | undefined} */
258
let nodes
269
try {
2710
parser(parsed => {
@@ -34,54 +17,41 @@ function parse(rawSelector, rule) {
3417
throw rule ? rule.error(e.message) : e
3518
}
3619
}
37-
// Should be safe, but @ts-check can't deduce the side-effect
38-
// triggered by `saver.processSync(str)`
39-
return /** @type {Root} */ (nodes).at(0)
20+
return nodes.at(0)
4021
}
4122

4223
/**
4324
* Replaces the "&" token in a node's selector with the parent selector
4425
* similar to what SCSS does.
4526
*
4627
* Mutates the nodes list
47-
*
48-
* @param {Extract<Node, { nodes: Array }>} nodes
49-
* @param {Selector} parent
50-
* @returns {boolean} Indicating whether a replacement took place or not.
5128
*/
5229
function interpolateAmpInSelector(nodes, parent) {
5330
let replaced = false
54-
nodes.each(
55-
/** @type {Node} */ node => {
56-
if (node.type === 'nesting') {
57-
let clonedParent = parent.clone({})
58-
if (node.value !== '&') {
59-
node.replaceWith(
60-
parse(node.value.replace('&', clonedParent.toString()))
61-
)
62-
} else {
63-
node.replaceWith(clonedParent)
64-
}
31+
nodes.each(node => {
32+
if (node.type === 'nesting') {
33+
let clonedParent = parent.clone({})
34+
if (node.value !== '&') {
35+
node.replaceWith(
36+
parse(node.value.replace('&', clonedParent.toString()))
37+
)
38+
} else {
39+
node.replaceWith(clonedParent)
40+
}
41+
replaced = true
42+
} else if ('nodes' in node && node.nodes) {
43+
if (interpolateAmpInSelector(node, parent)) {
6544
replaced = true
66-
} else if ('nodes' in node && node.nodes) {
67-
if (interpolateAmpInSelector(node, parent)) {
68-
replaced = true
69-
}
7045
}
7146
}
72-
)
47+
})
7348
return replaced
7449
}
7550

7651
/**
7752
* Combines parent and child selectors, in a SCSS-like way
78-
*
79-
* @param {PostcssRule} parent
80-
* @param {PostcssRule} child
81-
* @returns {Array<string>} An array of new, merged selectors
8253
*/
8354
function mergeSelectors(parent, child) {
84-
/** @type {Array<string>} */
8555
let merged = []
8656
parent.selectors.forEach(sel => {
8757
let parentNode = parse(sel, parent)
@@ -93,10 +63,8 @@ function mergeSelectors(parent, child) {
9363
let node = parse(selector, child)
9464
let replaced = interpolateAmpInSelector(node, parentNode)
9565
if (!replaced) {
96-
// NOTE: The type definitions for `postcss-selector-parser` seem to be
97-
// badly outdated.
98-
node.prepend(/** @type {any} */ (parser.combinator({ value: ' ' })))
99-
node.prepend(/** @type {Selector} */ (parentNode.clone({})))
66+
node.prepend(parser.combinator({ value: ' ' }))
67+
node.prepend(parentNode.clone({}))
10068
}
10169
merged.push(node.toString())
10270
})
@@ -106,10 +74,6 @@ function mergeSelectors(parent, child) {
10674

10775
/**
10876
* Move a child and its preceeding comment(s) to after "after"
109-
*
110-
* @param {ChildNode} child
111-
* @param {ChildNode} after
112-
* @returns {ChildNode} updated "after" node
11377
*/
11478
function breakOut(child, after) {
11579
let prev = child.prev()
@@ -122,17 +86,8 @@ function breakOut(child, after) {
12286
return child
12387
}
12488

125-
/**
126-
* @param {RuleMap} bubble
127-
*/
12889
function createFnAtruleChilds(bubble) {
129-
/**
130-
* @param {PostcssRule} rule
131-
* @param {AtRule} atrule
132-
* @param {boolean} bubbling
133-
*/
13490
return function atruleChilds(rule, atrule, bubbling, mergeSels = bubbling) {
135-
/** @type {Array<ChildNode>} */
13691
let children = []
13792
atrule.each(child => {
13893
if (child.type === 'rule' && bubbling) {
@@ -161,11 +116,6 @@ function createFnAtruleChilds(bubble) {
161116
}
162117
}
163118

164-
/**
165-
* @param {string} selector
166-
* @param {Array<ChildNode>} declarations
167-
* @param {ChildNode} after
168-
*/
169119
function pickDeclarations(selector, declarations, after) {
170120
let parent = new Rule({
171121
selector,
@@ -176,12 +126,7 @@ function pickDeclarations(selector, declarations, after) {
176126
return parent
177127
}
178128

179-
/**
180-
* @param {Array<string>} defaults,
181-
* @param {Array<string>} [custom]
182-
*/
183129
function atruleNames(defaults, custom) {
184-
/** @type {RuleMap} */
185130
let list = {}
186131
for (let name of defaults) {
187132
list[name] = true
@@ -194,13 +139,6 @@ function atruleNames(defaults, custom) {
194139
return list
195140
}
196141

197-
/** @typedef {{ type: 'basic', selector?: string, escapes?: never }} AtRootBParams */
198-
/** @typedef {{ type: 'withrules', escapes: (rule: string) => boolean, selector?: never }} AtRootWParams */
199-
/** @typedef {{ type: 'unknown', selector?: never, escapes?: never }} AtRootUParams */
200-
/** @typedef {{ type: 'noop', selector?: never, escapes?: never }} AtRootNParams */
201-
/** @typedef {AtRootBParams | AtRootWParams | AtRootNParams | AtRootUParams} AtRootParams */
202-
203-
/** @type {(params: string) => AtRootParams } */
204142
function parseRootRuleParams(params) {
205143
params = params.trim()
206144
let braceBlock = params.match(/^\((.*)\)$/)
@@ -210,7 +148,6 @@ function parseRootRuleParams(params) {
210148
let bits = braceBlock[1].match(/^(with(?:out)?):(.+)$/)
211149
if (bits) {
212150
let allowlist = bits[1] === 'with'
213-
/** @type {RuleMap} */
214151
let rules = Object.fromEntries(
215152
bits[2]
216153
.trim()
@@ -236,52 +173,39 @@ function parseRootRuleParams(params) {
236173
return { type: 'unknown' }
237174
}
238175

239-
/**
240-
* @param {AtRule} leaf
241-
* @returns {Array<AtRule>}
242-
*/
243176
function getAncestorRules(leaf) {
244-
/** @type {Array<AtRule>} */
245-
const lineage = []
246-
/** @type {Container | ChildNode | Document | undefined} */
177+
let lineage = []
247178
let parent = leaf.parent
248179

249180
while (parent && parent instanceof AtRule) {
250-
lineage.push(/** @type {AtRule} */ (parent))
181+
lineage.push(parent)
251182
parent = parent.parent
252183
}
253184
return lineage
254185
}
255186

256-
/**
257-
* @param {AtRule} rule
258-
*/
259187
function unwrapRootRule(rule) {
260-
const escapes = rule[rootRuleEscapes]
188+
let escapes = rule[rootRuleEscapes]
261189

262190
if (!escapes) {
263191
rule.after(rule.nodes)
264192
} else {
265-
const nodes = rule.nodes
193+
let nodes = rule.nodes
266194

267-
/** @type {AtRule | undefined} */
268195
let topEscaped
269196
let topEscapedIdx = -1
270-
/** @type {AtRule | undefined} */
271197
let breakoutLeaf
272-
/** @type {AtRule | undefined} */
273198
let breakoutRoot
274-
/** @type {AtRule | undefined} */
275199
let clone
276200

277-
const lineage = getAncestorRules(rule)
201+
let lineage = getAncestorRules(rule)
278202
lineage.forEach((parent, i) => {
279203
if (escapes(parent.name)) {
280204
topEscaped = parent
281205
topEscapedIdx = i
282206
breakoutRoot = clone
283207
} else {
284-
const oldClone = clone
208+
let oldClone = clone
285209
clone = parent.clone({ nodes: [] })
286210
oldClone && clone.append(oldClone)
287211
breakoutLeaf = breakoutLeaf || clone
@@ -293,20 +217,18 @@ function unwrapRootRule(rule) {
293217
} else if (!breakoutRoot) {
294218
topEscaped.after(nodes)
295219
} else {
296-
const leaf = /** @type {AtRule} */ (breakoutLeaf)
220+
let leaf = breakoutLeaf
297221
leaf.append(nodes)
298222
topEscaped.after(breakoutRoot)
299223
}
300224

301225
if (rule.next() && topEscaped) {
302-
/** @type {AtRule | undefined} */
303226
let restRoot
304227
lineage.slice(0, topEscapedIdx + 1).forEach((parent, i, arr) => {
305-
const oldRoot = restRoot
228+
let oldRoot = restRoot
306229
restRoot = parent.clone({ nodes: [] })
307230
oldRoot && restRoot.append(oldRoot)
308231

309-
/** @type {Array<ChildNode>} */
310232
let nextSibs = []
311233
let _child = arr[i - 1] || rule
312234
let next = _child.next()
@@ -326,12 +248,9 @@ function unwrapRootRule(rule) {
326248
const rootRuleMergeSel = Symbol('rootRuleMergeSel')
327249
const rootRuleEscapes = Symbol('rootRuleEscapes')
328250

329-
/**
330-
* @param {AtRule} rule
331-
*/
332251
function normalizeRootRule(rule) {
333252
let { params } = rule
334-
const { type, selector, escapes } = parseRootRuleParams(params)
253+
let { type, selector, escapes } = parseRootRuleParams(params)
335254
if (type === 'unknown') {
336255
throw rule.error(
337256
`Unknown @${rule.name} parameter ${JSON.stringify(params)}`
@@ -348,9 +267,6 @@ function normalizeRootRule(rule) {
348267

349268
const hasRootRule = Symbol('hasRootRule')
350269

351-
// ---------------------------------------------------------------------------
352-
353-
/** @type {import('./').Nested} */
354270
module.exports = (opts = {}) => {
355271
let bubble = atruleNames(['media', 'supports', 'layer'], opts.bubble)
356272
let atruleChilds = createFnAtruleChilds(bubble)
@@ -379,10 +295,8 @@ module.exports = (opts = {}) => {
379295

380296
Rule(rule) {
381297
let unwrapped = false
382-
/** @type {ChildNode} */
383298
let after = rule
384299
let copyDeclarations = false
385-
/** @type {Array<ChildNode>} */
386300
let declarations = []
387301

388302
rule.each(child => {

index.test.js

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,20 @@
1-
// @ts-check
21
let { equal, throws } = require('uvu/assert')
32
let { test } = require('uvu')
43
let postcss = require('postcss').default
54

65
let plugin = require('./')
76

8-
/**
9-
* @param {string} css
10-
* @returns {string}
11-
*/
12-
function normalise(css) {
7+
function normalize(css) {
138
return css
149
.replace(/([:;{}]|\*\/|\/\*)/g, ' $1 ')
1510
.replace(/\s\s+/g, ' ')
1611
.replace(/ ([;:])/g, '$1')
1712
.trim()
1813
}
1914

20-
/**
21-
* @param {string} input
22-
* @param {string} output
23-
* @param {plugin.Options | undefined} [opts]
24-
*/
2515
function run(input, output, opts) {
2616
let result = postcss([plugin(opts)]).process(input, { from: '/test.css' })
27-
equal(normalise(result.css), normalise(output))
17+
equal(normalize(result.css), normalize(output))
2818
equal(result.warnings().length, 0)
2919
}
3020

@@ -694,7 +684,7 @@ test('works with other visitors', () => {
694684
}
695685
}
696686
}
697-
mixinPlugin.postcss = /** @type {const} */ (true)
687+
mixinPlugin.postcss = true
698688
let out = postcss([plugin, mixinPlugin]).process(css, {
699689
from: undefined
700690
}).css
@@ -713,7 +703,7 @@ test('works with other visitors #2', () => {
713703
}
714704
}
715705
}
716-
mixinPlugin.postcss = /** @type {const} */ (true)
706+
mixinPlugin.postcss = true
717707
let out = postcss([plugin, mixinPlugin]).process(css, {
718708
from: undefined
719709
}).css
@@ -772,14 +762,14 @@ test('third level dependencies #2', () => {
772762
})
773763

774764
test('Name of at-root is configurable', () => {
775-
const rootRuleName = '_foobar_'
765+
let rootRuleName = '_foobar_'
776766
run(`a { & {} @${rootRuleName} { b {} } }`, `a {} b {}`, {
777767
rootRuleName
778768
})
779769
})
780770

781771
test('The rooRuleName option may start with "@"', () => {
782-
const rootRuleName = '@_foobar_'
772+
let rootRuleName = '@_foobar_'
783773
run(`a { & {} ${rootRuleName} { b {} } }`, `a {} b {}`, {
784774
rootRuleName
785775
})

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@
5151
"trailingComma": "none"
5252
},
5353
"eslintConfig": {
54-
"extends": "@logux/eslint-config",
55-
"rules": {
56-
"prefer-let/prefer-let": 0
57-
}
54+
"extends": "@logux/eslint-config"
5855
},
5956
"c8": {
6057
"exclude": [

0 commit comments

Comments
 (0)