@vltpkg/query 0.0.0-0.1730239248325
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +90 -0
- package/dist/esm/attribute.d.ts +15 -0
- package/dist/esm/attribute.d.ts.map +1 -0
- package/dist/esm/attribute.js +131 -0
- package/dist/esm/attribute.js.map +1 -0
- package/dist/esm/class.d.ts +6 -0
- package/dist/esm/class.d.ts.map +1 -0
- package/dist/esm/class.js +127 -0
- package/dist/esm/class.js.map +1 -0
- package/dist/esm/combinator.d.ts +6 -0
- package/dist/esm/combinator.d.ts.map +1 -0
- package/dist/esm/combinator.js +108 -0
- package/dist/esm/combinator.js.map +1 -0
- package/dist/esm/id.d.ts +6 -0
- package/dist/esm/id.d.ts.map +1 -0
- package/dist/esm/id.js +20 -0
- package/dist/esm/id.js.map +1 -0
- package/dist/esm/index.d.ts +13 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +118 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/pseudo.d.ts +13 -0
- package/dist/esm/pseudo.d.ts.map +1 -0
- package/dist/esm/pseudo.js +365 -0
- package/dist/esm/pseudo.js.map +1 -0
- package/dist/esm/types.d.ts +40 -0
- package/dist/esm/types.d.ts.map +1 -0
- package/dist/esm/types.js +93 -0
- package/dist/esm/types.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
Copyright (c) vlt technology, Inc.
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
7
|
+
Subject to the terms and conditions of this license, each copyright holder and contributor hereby grants to those receiving rights under this license a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except for failure to satisfy the conditions of this license) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer this software, where such license applies only to those patent claims, already acquired or hereafter acquired, licensable by such copyright holder or contributor that are necessarily infringed by:
|
|
8
|
+
|
|
9
|
+
(a) their Contribution(s) (the licensed copyrights of copyright holders and non-copyrightable additions of contributors, in source or binary form) alone; or
|
|
10
|
+
(b) combination of their Contribution(s) with the work of authorship to which such Contribution(s) was added by such copyright holder or contributor, if, at the time the Contribution is added, such addition causes such combination to be necessarily infringed. The patent license shall not apply to any other combinations which include the Contribution.
|
|
11
|
+
Except as expressly stated above, no rights or licenses from any copyright holder or contributor is granted under this license, whether expressly, by implication, estoppel or otherwise.
|
|
12
|
+
|
|
13
|
+
DISCLAIMER
|
|
14
|
+
|
|
15
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# @vltkpkg/query
|
|
2
|
+
|
|
3
|
+
The **vlt** query syntax engine.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
```js
|
|
8
|
+
import { Query } from '@vltpkg/query'
|
|
9
|
+
|
|
10
|
+
const query = new Query({ nodes })
|
|
11
|
+
query.search(':root > *')
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Examples
|
|
15
|
+
|
|
16
|
+
Querying nodes from a local `node_modules` folder.
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import { actual } from '@vltpkg/graph'
|
|
20
|
+
import { Query } from '@vltpkg/query'
|
|
21
|
+
|
|
22
|
+
const graph = await actual.load({ projectRoot: process.cwd() })
|
|
23
|
+
const query = new Query([...graph.nodes.values()])
|
|
24
|
+
query.search(':root > *')
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Supported Syntax Reference
|
|
28
|
+
|
|
29
|
+
The vlt query syntax enable usage of css-selector-like strings to filter
|
|
30
|
+
packages.
|
|
31
|
+
|
|
32
|
+
Many of the common elements of the CSS language are available, notably:
|
|
33
|
+
|
|
34
|
+
- `*` Universal selector, matches all selected items.
|
|
35
|
+
- `&` Nesting selector, allows for nesting selectors.
|
|
36
|
+
- `{}` Curly braces, when querying can be used to nest selectors.
|
|
37
|
+
|
|
38
|
+
Split by group of selectors below is the complete reference to supported
|
|
39
|
+
elements.
|
|
40
|
+
|
|
41
|
+
### Attribute selectors
|
|
42
|
+
|
|
43
|
+
Attribute selectors are used to match a value found in the `package.json`
|
|
44
|
+
metadata for each of the nodes being queried to a arbitrary value you choose.
|
|
45
|
+
|
|
46
|
+
- `[attr]` Matches elements with an `attr` property in its `package.json`.
|
|
47
|
+
- `[attr=value]` Matches elements with a property `attr` whose value is exactly `value`.
|
|
48
|
+
- `[attr^=value]` Matches elements with a property `attr` whose value starts with `value`.
|
|
49
|
+
- `[attr$=value]` Matches elements with a property `attr` whose value ends with `value`.
|
|
50
|
+
- `[attr~=value]` Matches elements with a property `attr` whose value is a whitespace-separated list of words, one of which is exactly `value`.
|
|
51
|
+
- `[attr|=value]` Matches elements with a property `attr` whose value is either `value` or starts with `value-`.
|
|
52
|
+
- `[attr*=value]` Matches elements with a property `attr`.
|
|
53
|
+
- `[attr=value i]` Case-insensitive flag, setting it will cause any comparison to be case-insensitive.
|
|
54
|
+
- `[attr=value s]` Case-sensitive flag, setting it will cause comparisons to be case-sensitive, this is the default behavior.
|
|
55
|
+
|
|
56
|
+
### Class selectors
|
|
57
|
+
|
|
58
|
+
- `.prod` Matches prod dependencies to your current project.
|
|
59
|
+
- `.dev` Matches packages that are only used as dev dependencies in your current project.
|
|
60
|
+
- `.optional` Matches packages that are optional to your current project.
|
|
61
|
+
- `.peer` Matches peer dependencies to your current project.
|
|
62
|
+
- `.workspace` Matches the current project worksacpes (listed in your `vlt-workspaces.json` file).
|
|
63
|
+
|
|
64
|
+
### Combinators
|
|
65
|
+
|
|
66
|
+
- `>` Child combinator, matches packages that are direct dependencies of the previously selected nodes.
|
|
67
|
+
- ` ` Descendant combinator, matches all packages that are direct & transitive dependencies of the previously selected nodes.
|
|
68
|
+
- `~` Sibling combinator, matches packages that are direct dependencies of all dependents of the previously selected nodes.
|
|
69
|
+
|
|
70
|
+
### ID Selectors
|
|
71
|
+
|
|
72
|
+
Identifiers are a shortcut to retrieving packages by name, unfortunately this shortcut only works for unscoped packages, with that in mind it's advised to rely on using **Attribute selectors** (showed above) instead.
|
|
73
|
+
|
|
74
|
+
e.g: `#foo` is the same as `[name=foo]`
|
|
75
|
+
|
|
76
|
+
### Pseudo class selectors
|
|
77
|
+
|
|
78
|
+
- `:attr(key, [attr=value])` The attribute pseudo-class allows for selecting packages based on nested properties of its `package.json` metadata. As an example, here is a query that filters only packages that declares an optional peer dependency named `foo`: `:attr(peerDependenciesMeta, foo, [optional=true])`
|
|
79
|
+
- `:empty` Matches packages that have no dependencies installed.
|
|
80
|
+
- `:has(<selector-list>)` Matches only packages that have valid results for the selector expression used. As an example, here is a query that matches all packages that have a peer dependency on `react`: `:has(.peer[name=react])`
|
|
81
|
+
- `:is(<forgiving-selector-list>)` Useful for writing large selectors in a more compact form, the `:is()` pseudo-class takes a selector list as its arguments and selects any element that can be selected by one of the selectors in that list. As an example, let's say I want to select packages named `a` & `b` that are direct dependencies of my project root: `:root > [name=a], :root > [name=b]` using the `:is()` pseudo-class, that same expression can be shortened to: `:root > :is([name=a], [name=b])`. Similar to the css pseudo-class of the same name, this selector has a forgiving behavior regarding its nested selector list ignoring any usage of non-existing ids, classes, combinators, operators and pseudo-selectors.
|
|
82
|
+
- `:not(<selector-list>)` Negation pseudo-class, select packages that do not match a list of selectors.
|
|
83
|
+
- `:private` Matches packages that have the property `private` set on their `package.json` file.
|
|
84
|
+
- `:type(registry|file|git|remote|workspace)` Matches packages based on their type, e.g, to retrieve all git dependencies: `:type(git)`.
|
|
85
|
+
|
|
86
|
+
### Pseudo Elements
|
|
87
|
+
|
|
88
|
+
- `:project` Returns both the root node (as defined below) along with any workspace declared in your project.
|
|
89
|
+
- `:root` Returns the root node, that represents the package defined at the top-level `package.json` of your project folder.
|
|
90
|
+
- `:scope` Returns the current scope of a given selector
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { NodeLike } from '@vltpkg/graph';
|
|
2
|
+
import { ParserState } from './types.js';
|
|
3
|
+
export type ComparatorFn = (attr: string, value?: string) => boolean;
|
|
4
|
+
/**
|
|
5
|
+
* Retrieve the {@link Manifest} values found at the given `properties`
|
|
6
|
+
* location for a given {@link Node}.
|
|
7
|
+
*/
|
|
8
|
+
export declare const getManifestPropertyValues: (node: NodeLike, properties: string[], attribute: string) => string[] | undefined;
|
|
9
|
+
export declare const filterAttributes: (state: ParserState, comparator: ComparatorFn | undefined, value: string, propertyName: string, insensitive: boolean, prefixProperties?: string[]) => ParserState;
|
|
10
|
+
export declare const attributeSelectorsMap: Map<string, ComparatorFn>;
|
|
11
|
+
/**
|
|
12
|
+
* Parse attributes selectors, e.g: `[name]`, `[name=value]`, etc
|
|
13
|
+
*/
|
|
14
|
+
export declare const attribute: (state: ParserState) => Promise<ParserState>;
|
|
15
|
+
//# sourceMappingURL=attribute.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribute.d.ts","sourceRoot":"","sources":["../../src/attribute.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAA;AAE7C,OAAO,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAA;AAEzD,MAAM,MAAM,YAAY,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,CAAC,EAAE,MAAM,KAAK,OAAO,CAAA;AAEpE;;;GAGG;AACH,eAAO,MAAM,yBAAyB,SAC9B,QAAQ,cACF,MAAM,EAAE,aACT,MAAM,KAChB,MAAM,EAAE,GAAG,SA+Db,CAAA;AAID,eAAO,MAAM,gBAAgB,UACpB,WAAW,cACN,YAAY,GAAG,SAAS,SAC7B,MAAM,gBACC,MAAM,eACP,OAAO,qBACF,MAAM,EAAE,KACzB,WA8CF,CAAA;AAcD,eAAO,MAAM,qBAAqB,2BAEjC,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,SAAS,UACb,WAAW,KACjB,OAAO,CAAC,WAAW,CAuBrB,CAAA"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { asAttributeNode } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Retrieve the {@link Manifest} values found at the given `properties`
|
|
5
|
+
* location for a given {@link Node}.
|
|
6
|
+
*/
|
|
7
|
+
export const getManifestPropertyValues = (node, properties, attribute) => {
|
|
8
|
+
if (!node.manifest)
|
|
9
|
+
return;
|
|
10
|
+
const traverse = new Set([node.manifest]);
|
|
11
|
+
const props = new Set();
|
|
12
|
+
for (const key of properties) {
|
|
13
|
+
for (const prop of traverse) {
|
|
14
|
+
/* c8 ignore start - should be impossible */
|
|
15
|
+
if (!prop) {
|
|
16
|
+
throw error('failed to find nested property in :attr', {
|
|
17
|
+
found: properties,
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
/* c8 ignore stop */
|
|
21
|
+
// expand the result list to include nested array values
|
|
22
|
+
if (Array.isArray(prop)) {
|
|
23
|
+
for (const p of prop) {
|
|
24
|
+
traverse.add(p);
|
|
25
|
+
}
|
|
26
|
+
continue;
|
|
27
|
+
}
|
|
28
|
+
// guard for inspecting keys of objects next
|
|
29
|
+
if (typeof prop === 'string' ||
|
|
30
|
+
typeof prop === 'number' ||
|
|
31
|
+
typeof prop === 'boolean') {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
// assign next value when found
|
|
35
|
+
if (key in prop) {
|
|
36
|
+
const nextValue = prop[key];
|
|
37
|
+
if (nextValue) {
|
|
38
|
+
if (key === attribute) {
|
|
39
|
+
props.add(nextValue);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
traverse.delete(prop);
|
|
43
|
+
traverse.add(nextValue);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
// if no value was found after trying a given key
|
|
50
|
+
// then there's nothing to be collected
|
|
51
|
+
if (!props.size)
|
|
52
|
+
return;
|
|
53
|
+
// expand the result to include array values
|
|
54
|
+
const collect = new Set();
|
|
55
|
+
for (const prop of props) {
|
|
56
|
+
if (Array.isArray(prop)) {
|
|
57
|
+
for (const p of prop) {
|
|
58
|
+
collect.add(p ? String(p) : '');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
collect.add(String(prop));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return [...collect];
|
|
66
|
+
};
|
|
67
|
+
// decorator style of function that will filter `ParserState` results
|
|
68
|
+
// based on a provided `comparator` function
|
|
69
|
+
export const filterAttributes = (state, comparator, value, propertyName, insensitive, prefixProperties = []) => {
|
|
70
|
+
const check = (attr) => comparator?.(insensitive ? String(attr).toLowerCase() : String(attr), insensitive ? value.toLowerCase() : value);
|
|
71
|
+
const deleteNode = (node) => {
|
|
72
|
+
for (const edge of node.edgesIn) {
|
|
73
|
+
state.partial.edges.delete(edge);
|
|
74
|
+
}
|
|
75
|
+
state.partial.nodes.delete(node);
|
|
76
|
+
};
|
|
77
|
+
for (const node of state.partial.nodes) {
|
|
78
|
+
const prefixes = prefixProperties.length ? prefixProperties : [propertyName];
|
|
79
|
+
const attrs = getManifestPropertyValues(node, prefixes, propertyName);
|
|
80
|
+
// if no attribute value was found, that means the attribute won't match
|
|
81
|
+
if (!attrs?.length) {
|
|
82
|
+
deleteNode(node);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
// if the node attribute value won't match, then remove the node
|
|
86
|
+
if (comparator && !attrs.some(check)) {
|
|
87
|
+
deleteNode(node);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (const edge of state.partial.edges) {
|
|
91
|
+
// edge.name is a special case in order
|
|
92
|
+
// to be able to match missing nodes by name
|
|
93
|
+
if (propertyName === 'name' && check(edge.name)) {
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
// remove any remaining dangling edge
|
|
97
|
+
if (!edge.to) {
|
|
98
|
+
state.partial.edges.delete(edge);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return state;
|
|
102
|
+
};
|
|
103
|
+
// ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors
|
|
104
|
+
const attributeSelectors = {
|
|
105
|
+
'=': (attr, value = '') => attr === value,
|
|
106
|
+
'^=': (attr, value = '') => attr.startsWith(value),
|
|
107
|
+
'$=': (attr, value = '') => attr.endsWith(value),
|
|
108
|
+
'~=': (attr, value = '') => new Set(attr.match(/\w+/g)).has(value),
|
|
109
|
+
'*=': (attr, value = '') => attr.includes(value),
|
|
110
|
+
'|=': (attr, value = '') => attr === value || attr.startsWith(`${value}-`),
|
|
111
|
+
undefined: (attr) => !!attr,
|
|
112
|
+
};
|
|
113
|
+
export const attributeSelectorsMap = new Map(Object.entries(attributeSelectors));
|
|
114
|
+
/**
|
|
115
|
+
* Parse attributes selectors, e.g: `[name]`, `[name=value]`, etc
|
|
116
|
+
*/
|
|
117
|
+
export const attribute = async (state) => {
|
|
118
|
+
const curr = asAttributeNode(state.current);
|
|
119
|
+
const operatorFn = attributeSelectorsMap.get(String(curr.operator));
|
|
120
|
+
if (!operatorFn) {
|
|
121
|
+
if (state.loose) {
|
|
122
|
+
return state;
|
|
123
|
+
}
|
|
124
|
+
throw new Error(`Unsupported attribute operator: ${curr.operator}`);
|
|
125
|
+
}
|
|
126
|
+
const value = curr.value || '';
|
|
127
|
+
const propertyName = curr.attribute;
|
|
128
|
+
const insensitive = !!curr.insensitive;
|
|
129
|
+
return filterAttributes(state, operatorFn, value, propertyName, insensitive);
|
|
130
|
+
};
|
|
131
|
+
//# sourceMappingURL=attribute.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attribute.js","sourceRoot":"","sources":["../../src/attribute.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAG3C,OAAO,EAAE,eAAe,EAAe,MAAM,YAAY,CAAA;AAIzD;;;GAGG;AACH,MAAM,CAAC,MAAM,yBAAyB,GAAG,CACvC,IAAc,EACd,UAAoB,EACpB,SAAiB,EACK,EAAE;IACxB,IAAI,CAAC,IAAI,CAAC,QAAQ;QAAE,OAAM;IAE1B,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAY,CAAC,IAAI,CAAC,QAAqB,CAAC,CAAC,CAAA;IACjE,MAAM,KAAK,GAAG,IAAI,GAAG,EAAa,CAAA;IAClC,KAAK,MAAM,GAAG,IAAI,UAAU,EAAE,CAAC;QAC7B,KAAK,MAAM,IAAI,IAAI,QAAQ,EAAE,CAAC;YAC5B,4CAA4C;YAC5C,IAAI,CAAC,IAAI,EAAE,CAA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KAAK,GAAG,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;IACxD,IAAI,EAAE,CAAC,IAAY,EAAE,KAAK,GAAG,EAAE,EAAE,EAAE,CACjC,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,UAAU,CAAC,GAAG,KAAK,GAAG,CAAC;IAChD,SAAS,EAAE,CAAC,IAAY,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI;CACpC,CAAA;AACD,MAAM,CAAC,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAC1C,MAAM,CAAC,OAAO,CAAC,kBAAkB,CAAC,CACnC,CAAA;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAC5B,KAAkB,EACI,EAAE;IACxB,MAAM,IAAI,GAAG,eAAe,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IAC3C,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,CAAC,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAA;IACnE,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YAChB,OAAO,KAAK,CAAA;QACd,CAAC;QAED,MAAM,IAAI,KAAK,CACb,mCAAmC,IAAI,CAAC,QAAQ,EAAE,CACnD,CAAA;IACH,CAAC;IAED,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,IAAI,EAAE,CAAA;IAC9B,MAAM,YAAY,GAAG,IAAI,CAAC,SAAS,CAAA;IACnC,MAAM,WAAW,GAAG,CAAC,CAAC,IAAI,CAAC,WAAW,CAAA;IACtC,OAAO,gBAAgB,CACrB,KAAK,EACL,UAAU,EACV,KAAK,EACL,YAAY,EACZ,WAAW,CACZ,CAAA;AACH,CAAC,CAAA","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport type { NodeLike } from '@vltpkg/graph'\nimport type { JSONField } from '@vltpkg/types'\nimport { asAttributeNode, ParserState } from './types.js'\n\nexport type ComparatorFn = (attr: string, value?: string) => boolean\n\n/**\n * Retrieve the {@link Manifest} values found at the given `properties`\n * location for a given {@link Node}.\n */\nexport const getManifestPropertyValues = (\n node: NodeLike,\n properties: string[],\n attribute: string,\n): string[] | undefined => {\n if (!node.manifest) return\n\n const traverse = new Set<JSONField>([node.manifest as JSONField])\n const props = new Set<JSONField>()\n for (const key of properties) {\n for (const prop of traverse) {\n /* c8 ignore start - should be impossible */\n if (!prop) {\n throw error('failed to find nested property in :attr', {\n found: properties,\n })\n }\n /* c8 ignore stop */\n\n // expand the result list to include nested array values\n if (Array.isArray(prop)) {\n for (const p of prop) {\n traverse.add(p)\n }\n continue\n }\n\n // guard for inspecting keys of objects next\n if (\n typeof prop === 'string' ||\n typeof prop === 'number' ||\n typeof prop === 'boolean'\n ) {\n continue\n }\n\n // assign next value when found\n if (key in prop) {\n const nextValue = prop[key]\n if (nextValue) {\n if (key === attribute) {\n props.add(nextValue)\n } else {\n traverse.delete(prop)\n traverse.add(nextValue)\n }\n }\n }\n }\n }\n // if no value was found after trying a given key\n // then there's nothing to be collected\n if (!props.size) return\n\n // expand the result to include array values\n const collect = new Set<string>()\n for (const prop of props) {\n if (Array.isArray(prop)) {\n for (const p of prop) {\n collect.add(p ? String(p) : '')\n }\n } else {\n collect.add(String(prop))\n }\n }\n\n return [...collect]\n}\n\n// decorator style of function that will filter `ParserState` results\n// based on a provided `comparator` function\nexport const filterAttributes = (\n state: ParserState,\n comparator: ComparatorFn | undefined,\n value: string,\n propertyName: string,\n insensitive: boolean,\n prefixProperties: string[] = [],\n): ParserState => {\n const check = (attr: JSONField) =>\n comparator?.(\n insensitive ? String(attr).toLowerCase() : String(attr),\n insensitive ? value.toLowerCase() : value,\n )\n const deleteNode = (node: NodeLike) => {\n for (const edge of node.edgesIn) {\n state.partial.edges.delete(edge)\n }\n state.partial.nodes.delete(node)\n }\n\n for (const node of state.partial.nodes) {\n const prefixes =\n prefixProperties.length ? prefixProperties : [propertyName]\n const attrs = getManifestPropertyValues(\n node,\n prefixes,\n propertyName,\n )\n\n // if no attribute value was found, that means the attribute won't match\n if (!attrs?.length) {\n deleteNode(node)\n continue\n }\n\n // if the node attribute value won't match, then remove the node\n if (comparator && !attrs.some(check)) {\n deleteNode(node)\n }\n }\n\n for (const edge of state.partial.edges) {\n // edge.name is a special case in order\n // to be able to match missing nodes by name\n if (propertyName === 'name' && check(edge.name)) {\n continue\n }\n // remove any remaining dangling edge\n if (!edge.to) {\n state.partial.edges.delete(edge)\n }\n }\n return state\n}\n\n// ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors\nconst attributeSelectors: Record<string, ComparatorFn> = {\n '=': (attr: string, value = '') => attr === value,\n '^=': (attr: string, value = '') => attr.startsWith(value),\n '$=': (attr: string, value = '') => attr.endsWith(value),\n '~=': (attr: string, value = '') =>\n new Set<string>(attr.match(/\\w+/g)).has(value),\n '*=': (attr: string, value = '') => attr.includes(value),\n '|=': (attr: string, value = '') =>\n attr === value || attr.startsWith(`${value}-`),\n undefined: (attr: string) => !!attr,\n}\nexport const attributeSelectorsMap = new Map<string, ComparatorFn>(\n Object.entries(attributeSelectors),\n)\n\n/**\n * Parse attributes selectors, e.g: `[name]`, `[name=value]`, etc\n */\nexport const attribute = async (\n state: ParserState,\n): Promise<ParserState> => {\n const curr = asAttributeNode(state.current)\n const operatorFn = attributeSelectorsMap.get(String(curr.operator))\n if (!operatorFn) {\n if (state.loose) {\n return state\n }\n\n throw new Error(\n `Unsupported attribute operator: ${curr.operator}`,\n )\n }\n\n const value = curr.value || ''\n const propertyName = curr.attribute\n const insensitive = !!curr.insensitive\n return filterAttributes(\n state,\n operatorFn,\n value,\n propertyName,\n insensitive,\n )\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["../../src/class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAyB,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AAoHpE;;GAEG;AACH,eAAO,MAAM,OAAO,UAAiB,WAAW,yBAW/C,CAAA"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { asClassNode } from './types.js';
|
|
2
|
+
const classSelectors = {
|
|
3
|
+
prod: async (state) => {
|
|
4
|
+
for (const edge of state.partial.edges) {
|
|
5
|
+
if (edge.type !== 'prod' || edge.from.dev) {
|
|
6
|
+
state.partial.edges.delete(edge);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
for (const node of state.partial.nodes) {
|
|
10
|
+
if (!node.edgesIn.size) {
|
|
11
|
+
state.partial.nodes.delete(node);
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const iterator = new Set(node.edgesIn);
|
|
15
|
+
for (const edge of iterator) {
|
|
16
|
+
if (!state.partial.edges.has(edge)) {
|
|
17
|
+
iterator.delete(edge);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (!iterator.size) {
|
|
21
|
+
state.partial.nodes.delete(node);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return state;
|
|
25
|
+
},
|
|
26
|
+
dev: async (state) => {
|
|
27
|
+
for (const edge of state.partial.edges) {
|
|
28
|
+
if (edge.type !== 'dev' && !edge.from.dev) {
|
|
29
|
+
state.partial.edges.delete(edge);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
for (const node of state.partial.nodes) {
|
|
33
|
+
if (!node.edgesIn.size) {
|
|
34
|
+
state.partial.nodes.delete(node);
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
const iterator = new Set(node.edgesIn);
|
|
38
|
+
for (const edge of iterator) {
|
|
39
|
+
if (!state.partial.edges.has(edge)) {
|
|
40
|
+
iterator.delete(edge);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
if (!iterator.size) {
|
|
44
|
+
state.partial.nodes.delete(node);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return state;
|
|
48
|
+
},
|
|
49
|
+
optional: async (state) => {
|
|
50
|
+
for (const edge of state.partial.edges) {
|
|
51
|
+
if (edge.type !== 'optional' && !edge.from.optional) {
|
|
52
|
+
state.partial.edges.delete(edge);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
for (const node of state.partial.nodes) {
|
|
56
|
+
if (!node.edgesIn.size) {
|
|
57
|
+
state.partial.nodes.delete(node);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
const iterator = new Set(node.edgesIn);
|
|
61
|
+
for (const edge of iterator) {
|
|
62
|
+
if (!state.partial.edges.has(edge)) {
|
|
63
|
+
iterator.delete(edge);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (!iterator.size) {
|
|
67
|
+
state.partial.nodes.delete(node);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return state;
|
|
71
|
+
},
|
|
72
|
+
peer: async (state) => {
|
|
73
|
+
for (const edge of state.partial.edges) {
|
|
74
|
+
if (edge.type !== 'peer') {
|
|
75
|
+
state.partial.edges.delete(edge);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
for (const node of state.partial.nodes) {
|
|
79
|
+
if (!node.edgesIn.size) {
|
|
80
|
+
state.partial.nodes.delete(node);
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
for (const e of node.edgesIn) {
|
|
84
|
+
if (!state.partial.edges.has(e)) {
|
|
85
|
+
state.partial.nodes.delete(node);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return state;
|
|
90
|
+
},
|
|
91
|
+
workspace: async (state) => {
|
|
92
|
+
for (const node of state.partial.nodes) {
|
|
93
|
+
if (!node.importer || node.mainImporter) {
|
|
94
|
+
state.partial.nodes.delete(node);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
for (const edge of state.partial.edges) {
|
|
98
|
+
// workspaces can't be missing
|
|
99
|
+
if (!edge.to) {
|
|
100
|
+
state.partial.edges.delete(edge);
|
|
101
|
+
// keep only edges that are linking to preivously kept nodes
|
|
102
|
+
}
|
|
103
|
+
else if (!state.partial.nodes.has(edge.to)) {
|
|
104
|
+
state.partial.edges.delete(edge);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return state;
|
|
108
|
+
},
|
|
109
|
+
// TBD: all things bundled
|
|
110
|
+
// bundled: () => false,
|
|
111
|
+
};
|
|
112
|
+
const classSelectorsMap = new Map(Object.entries(classSelectors));
|
|
113
|
+
/**
|
|
114
|
+
* Parse classes, e.g: `.prod`, `.dev`, `.optional`, etc
|
|
115
|
+
*/
|
|
116
|
+
export const classFn = async (state) => {
|
|
117
|
+
const curr = asClassNode(state.current);
|
|
118
|
+
const comparatorFn = curr.value && classSelectorsMap.get(curr.value);
|
|
119
|
+
if (!comparatorFn) {
|
|
120
|
+
if (state.loose) {
|
|
121
|
+
return state;
|
|
122
|
+
}
|
|
123
|
+
throw new Error(`Unsupported class: ${state.current.value}`);
|
|
124
|
+
}
|
|
125
|
+
return comparatorFn(state);
|
|
126
|
+
};
|
|
127
|
+
//# sourceMappingURL=class.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"class.js","sourceRoot":"","sources":["../../src/class.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,WAAW,EAAoB,MAAM,YAAY,CAAA;AAEpE,MAAM,cAAc,GAA6B;IAC/C,IAAI,EAAE,KAAK,EAAE,KAAkB,EAAE,EAAE;QACjC,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACvC,IAAI,IAAI,CAAC,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;gBAC1C,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAA;YAClC,CAAC;QACH,CAAC;QACD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,EA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, ParserFn> = {\n prod: async (state: ParserState) => {\n for (const edge of state.partial.edges) {\n if (edge.type !== 'prod' || edge.from.dev) {\n state.partial.edges.delete(edge)\n }\n }\n for (const node of state.partial.nodes) {\n if (!node.edgesIn.size) {\n state.partial.nodes.delete(node)\n continue\n }\n const iterator = new Set(node.edgesIn)\n for (const edge of iterator) {\n if (!state.partial.edges.has(edge)) {\n iterator.delete(edge)\n }\n }\n if (!iterator.size) {\n state.partial.nodes.delete(node)\n }\n }\n return state\n },\n dev: async (state: ParserState) => {\n for (const edge of state.partial.edges) {\n if (edge.type !== 'dev' && !edge.from.dev) {\n state.partial.edges.delete(edge)\n }\n }\n for (const node of state.partial.nodes) {\n if (!node.edgesIn.size) {\n state.partial.nodes.delete(node)\n continue\n }\n const iterator = new Set(node.edgesIn)\n for (const edge of iterator) {\n if (!state.partial.edges.has(edge)) {\n iterator.delete(edge)\n }\n }\n if (!iterator.size) {\n state.partial.nodes.delete(node)\n }\n }\n return state\n },\n optional: async (state: ParserState) => {\n for (const edge of state.partial.edges) {\n if (edge.type !== 'optional' && !edge.from.optional) {\n state.partial.edges.delete(edge)\n }\n }\n for (const node of state.partial.nodes) {\n if (!node.edgesIn.size) {\n state.partial.nodes.delete(node)\n continue\n }\n const iterator = new Set(node.edgesIn)\n for (const edge of iterator) {\n if (!state.partial.edges.has(edge)) {\n iterator.delete(edge)\n }\n }\n if (!iterator.size) {\n state.partial.nodes.delete(node)\n }\n }\n return state\n },\n peer: async (state: ParserState) => {\n for (const edge of state.partial.edges) {\n if (edge.type !== 'peer') {\n state.partial.edges.delete(edge)\n }\n }\n for (const node of state.partial.nodes) {\n if (!node.edgesIn.size) {\n state.partial.nodes.delete(node)\n continue\n }\n for (const e of node.edgesIn) {\n if (!state.partial.edges.has(e)) {\n state.partial.nodes.delete(node)\n }\n }\n }\n return state\n },\n workspace: async (state: ParserState) => {\n for (const node of state.partial.nodes) {\n if (!node.importer || node.mainImporter) {\n state.partial.nodes.delete(node)\n }\n }\n for (const edge of state.partial.edges) {\n // workspaces can't be missing\n if (!edge.to) {\n state.partial.edges.delete(edge)\n // keep only edges that are linking to preivously kept nodes\n } else if (!state.partial.nodes.has(edge.to)) {\n state.partial.edges.delete(edge)\n }\n }\n return state\n },\n // TBD: all things bundled\n // bundled: () => false,\n}\n\nconst classSelectorsMap = new Map<string, ParserFn>(\n Object.entries(classSelectors),\n)\n\n/**\n * Parse classes, e.g: `.prod`, `.dev`, `.optional`, etc\n */\nexport const classFn = async (state: ParserState) => {\n const curr = asClassNode(state.current)\n const comparatorFn = curr.value && classSelectorsMap.get(curr.value)\n if (!comparatorFn) {\n if (state.loose) {\n return state\n }\n\n throw new Error(`Unsupported class: ${state.current.value}`)\n }\n return comparatorFn(state)\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"combinator.d.ts","sourceRoot":"","sources":["../../src/combinator.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,WAAW,EAGjB,MAAM,YAAY,CAAA;AA6GnB;;GAEG;AACH,eAAO,MAAM,UAAU,UAAiB,WAAW,yBAYlD,CAAA"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { asCombinatorNode, } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Returns a new set of nodes, containing all direct dependencies
|
|
4
|
+
* of the current list of nodes used.
|
|
5
|
+
*
|
|
6
|
+
* ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator
|
|
7
|
+
*/
|
|
8
|
+
const childCombinator = async (state) => {
|
|
9
|
+
const traverse = new Set(state.partial.nodes);
|
|
10
|
+
state.partial.edges.clear();
|
|
11
|
+
state.partial.nodes.clear();
|
|
12
|
+
// visit direct children of the current list of nodes
|
|
13
|
+
// collecting refs to these children and the edges that
|
|
14
|
+
// connected them.
|
|
15
|
+
for (const node of traverse) {
|
|
16
|
+
for (const edge of node.edgesOut.values()) {
|
|
17
|
+
if (edge.to) {
|
|
18
|
+
state.partial.edges.add(edge);
|
|
19
|
+
state.partial.nodes.add(edge.to);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return state;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Returns a new set of nodes, containing nodes that are also children
|
|
27
|
+
* of all parent nodes to the current list of nodes used.
|
|
28
|
+
*
|
|
29
|
+
* Note: The subsequent-sibling comparator has a behavior that is
|
|
30
|
+
* somehow approximative of that of its css counterpart, given that
|
|
31
|
+
* in the context of dependency graphs the order of appearance is
|
|
32
|
+
* not necessarily controlled by the end user. The approach for
|
|
33
|
+
* this comparator is to match all siblings of a node.
|
|
34
|
+
*
|
|
35
|
+
* ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Subsequent-sibling_combinator
|
|
36
|
+
*/
|
|
37
|
+
const subsequentSiblingCombinator = async (state) => {
|
|
38
|
+
const traverse = new Set(state.partial.nodes);
|
|
39
|
+
state.partial.edges.clear();
|
|
40
|
+
state.partial.nodes.clear();
|
|
41
|
+
// visits direct parents of the current list of node and then
|
|
42
|
+
// visit their children, collecting refs to all children and edges
|
|
43
|
+
// that are not in the original list of nodes.
|
|
44
|
+
for (const node of traverse) {
|
|
45
|
+
for (const edge of node.edgesIn) {
|
|
46
|
+
const parents = edge.from.edgesOut.values();
|
|
47
|
+
for (const edge of parents) {
|
|
48
|
+
if (edge.to && edge.to !== node) {
|
|
49
|
+
state.partial.edges.add(edge);
|
|
50
|
+
state.partial.nodes.add(edge.to);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return state;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Returns a new set of nodes containing all nodes that are descendents
|
|
59
|
+
* to items in the current list of nodes.
|
|
60
|
+
*
|
|
61
|
+
* ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_combinator
|
|
62
|
+
*/
|
|
63
|
+
const descendentCombinator = async (state) => {
|
|
64
|
+
// spaces between tags selectors are a noop
|
|
65
|
+
if (state.prev?.type === 'tag' || state.next?.type === 'tag') {
|
|
66
|
+
return state;
|
|
67
|
+
}
|
|
68
|
+
const traverse = new Set(state.partial.nodes);
|
|
69
|
+
state.partial.edges.clear();
|
|
70
|
+
state.partial.nodes.clear();
|
|
71
|
+
// breadth-first traversal of the graph, starting from the current
|
|
72
|
+
// list of nodes, collecting all nodes and edges along the way
|
|
73
|
+
for (const node of traverse) {
|
|
74
|
+
const children = new Set();
|
|
75
|
+
for (const edge of node.edgesOut.values()) {
|
|
76
|
+
if (edge.to) {
|
|
77
|
+
children.add(edge.to);
|
|
78
|
+
state.partial.edges.add(edge);
|
|
79
|
+
state.partial.nodes.add(edge.to);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
for (const child of children) {
|
|
83
|
+
traverse.add(child);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return state;
|
|
87
|
+
};
|
|
88
|
+
const combinatorSelectors = {
|
|
89
|
+
'>': childCombinator,
|
|
90
|
+
'~': subsequentSiblingCombinator,
|
|
91
|
+
' ': descendentCombinator,
|
|
92
|
+
};
|
|
93
|
+
const combinatorSelectorsMap = new Map(Object.entries(combinatorSelectors));
|
|
94
|
+
/**
|
|
95
|
+
* Parse css-style combinators, e.g: `>`, `~` and ` `
|
|
96
|
+
*/
|
|
97
|
+
export const combinator = async (state) => {
|
|
98
|
+
const curr = asCombinatorNode(state.current);
|
|
99
|
+
const parserFn = curr.value && combinatorSelectorsMap.get(curr.value);
|
|
100
|
+
if (!parserFn) {
|
|
101
|
+
if (state.loose) {
|
|
102
|
+
return state;
|
|
103
|
+
}
|
|
104
|
+
throw new Error(`Unsupported combinator: ${state.current.value}`);
|
|
105
|
+
}
|
|
106
|
+
return parserFn(state);
|
|
107
|
+
};
|
|
108
|
+
//# sourceMappingURL=combinator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"combinator.js","sourceRoot":"","sources":["../../src/combinator.ts"],"names":[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AAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAA;gBAC7B,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,CAAA;YAClC,CAAC;QACH,CAAC;QACD,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;YAC7B,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QACrB,CAAC;IACH,CAAC;IAED,OAAO,KAAK,CAAA;AACd,CAAC,CAAA;AAED,MAAM,mBAAmB,GAAG;IAC1B,GAAG,EAAE,eAAe;IACpB,GAAG,EAAE,2BAA2B;IAChC,GAAG,EAAE,oBAAoB;CAC1B,CAAA;AAED,MAAM,sBAAsB,GAAG,IAAI,GAAG,CACpC,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,CACpC,CAAA;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,KAAK,EAAE,KAAkB,EAAE,EAAE;IACrD,MAAM,IAAI,GAAG,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IAC5C,MAAM,QAAQ,GACZ,IAAI,CAAC,KAAK,IAAI,sBAAsB,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;IACtD,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,IAAI,KAAK,CAAC,KAAK,EAAE,CAAC;YAChB,OAAO,KAAK,CAAA;QACd,CAAC;QAED,MAAM,IAAI,KAAK,CAAC,2BAA2B,KAAK,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC,CAAA;IACnE,CAAC;IACD,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAA;AACxB,CAAC,CAAA","sourcesContent":["import type { EdgeLike, NodeLike } from '@vltpkg/graph'\nimport {\n type ParserState,\n type ParserFn,\n asCombinatorNode,\n} from './types.js'\n\n/**\n * Returns a new set of nodes, containing all direct dependencies\n * of the current list of nodes used.\n *\n * ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Child_combinator\n */\nconst childCombinator = async (state: ParserState) => {\n const traverse = new Set(state.partial.nodes)\n state.partial.edges.clear()\n state.partial.nodes.clear()\n\n // visit direct children of the current list of nodes\n // collecting refs to these children and the edges that\n // connected them.\n for (const node of traverse) {\n for (const edge of node.edgesOut.values()) {\n if (edge.to) {\n state.partial.edges.add(edge)\n state.partial.nodes.add(edge.to)\n }\n }\n }\n\n return state\n}\n\n/**\n * Returns a new set of nodes, containing nodes that are also children\n * of all parent nodes to the current list of nodes used.\n *\n * Note: The subsequent-sibling comparator has a behavior that is\n * somehow approximative of that of its css counterpart, given that\n * in the context of dependency graphs the order of appearance is\n * not necessarily controlled by the end user. The approach for\n * this comparator is to match all siblings of a node.\n *\n * ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Subsequent-sibling_combinator\n */\nconst subsequentSiblingCombinator = async (state: ParserState) => {\n const traverse = new Set(state.partial.nodes)\n state.partial.edges.clear()\n state.partial.nodes.clear()\n\n // visits direct parents of the current list of node and then\n // visit their children, collecting refs to all children and edges\n // that are not in the original list of nodes.\n for (const node of traverse) {\n for (const edge of node.edgesIn) {\n const parents: IterableIterator<EdgeLike> =\n edge.from.edgesOut.values()\n for (const edge of parents) {\n if (edge.to && edge.to !== node) {\n state.partial.edges.add(edge)\n state.partial.nodes.add(edge.to)\n }\n }\n }\n }\n\n return state\n}\n\n/**\n * Returns a new set of nodes containing all nodes that are descendents\n * to items in the current list of nodes.\n *\n * ref: https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_combinator\n */\nconst descendentCombinator = async (state: ParserState) => {\n // spaces between tags selectors are a noop\n if (state.prev?.type === 'tag' || state.next?.type === 'tag') {\n return state\n }\n\n const traverse = new Set<NodeLike>(state.partial.nodes)\n state.partial.edges.clear()\n state.partial.nodes.clear()\n\n // breadth-first traversal of the graph, starting from the current\n // list of nodes, collecting all nodes and edges along the way\n for (const node of traverse) {\n const children = new Set<NodeLike>()\n for (const edge of node.edgesOut.values()) {\n if (edge.to) {\n children.add(edge.to)\n state.partial.edges.add(edge)\n state.partial.nodes.add(edge.to)\n }\n }\n for (const child of children) {\n traverse.add(child)\n }\n }\n\n return state\n}\n\nconst combinatorSelectors = {\n '>': childCombinator,\n '~': subsequentSiblingCombinator,\n ' ': descendentCombinator,\n}\n\nconst combinatorSelectorsMap = new Map<string, ParserFn>(\n Object.entries(combinatorSelectors),\n)\n\n/**\n * Parse css-style combinators, e.g: `>`, `~` and ` `\n */\nexport const combinator = async (state: ParserState) => {\n const curr = asCombinatorNode(state.current)\n const parserFn =\n curr.value && combinatorSelectorsMap.get(curr.value)\n if (!parserFn) {\n if (state.loose) {\n return state\n }\n\n throw new Error(`Unsupported combinator: ${state.current.value}`)\n }\n return parserFn(state)\n}\n"]}
|
package/dist/esm/id.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id.d.ts","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AACA,OAAO,EAAoB,KAAK,WAAW,EAAE,MAAM,YAAY,CAAA;AAM/D;;GAEG;AACH,eAAO,MAAM,EAAE,UAAiB,WAAW,yBAc1C,CAAA"}
|
package/dist/esm/id.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { error } from '@vltpkg/error-cause';
|
|
2
|
+
import { asIdentifierNode } from './types.js';
|
|
3
|
+
import { attributeSelectorsMap, filterAttributes, } from './attribute.js';
|
|
4
|
+
/**
|
|
5
|
+
* Parse ids, e.g: `#foo`
|
|
6
|
+
*/
|
|
7
|
+
export const id = async (state) => {
|
|
8
|
+
const { value } = asIdentifierNode(state.current);
|
|
9
|
+
const comparator = attributeSelectorsMap.get('=');
|
|
10
|
+
/* c8 ignore start - should not be possible */
|
|
11
|
+
if (!value) {
|
|
12
|
+
throw error('Missing identifier name');
|
|
13
|
+
}
|
|
14
|
+
if (!comparator) {
|
|
15
|
+
throw error('Could not find attribute selector comparator');
|
|
16
|
+
}
|
|
17
|
+
/* c8 ignore stop */
|
|
18
|
+
return filterAttributes(state, comparator, value, 'name', true);
|
|
19
|
+
};
|
|
20
|
+
//# sourceMappingURL=id.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"id.js","sourceRoot":"","sources":["../../src/id.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAC3C,OAAO,EAAE,gBAAgB,EAAoB,MAAM,YAAY,CAAA;AAC/D,OAAO,EACL,qBAAqB,EACrB,gBAAgB,GACjB,MAAM,gBAAgB,CAAA;AAEvB;;GAEG;AACH,MAAM,CAAC,MAAM,EAAE,GAAG,KAAK,EAAE,KAAkB,EAAE,EAAE;IAC7C,MAAM,EAAE,KAAK,EAAE,GAAG,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;IACjD,MAAM,UAAU,GAAG,qBAAqB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAEjD,8CAA8C;IAC9C,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,MAAM,KAAK,CAAC,yBAAyB,CAAC,CAAA;IACxC,CAAC;IACD,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,MAAM,KAAK,CAAC,8CAA8C,CAAC,CAAA;IAC7D,CAAC;IACD,oBAAoB;IAEpB,OAAO,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,CAAA;AACjE,CAAC,CAAA","sourcesContent":["import { error } from '@vltpkg/error-cause'\nimport { asIdentifierNode, type ParserState } from './types.js'\nimport {\n attributeSelectorsMap,\n filterAttributes,\n} from './attribute.js'\n\n/**\n * Parse ids, e.g: `#foo`\n */\nexport const id = async (state: ParserState) => {\n const { value } = asIdentifierNode(state.current)\n const comparator = attributeSelectorsMap.get('=')\n\n /* c8 ignore start - should not be possible */\n if (!value) {\n throw error('Missing identifier name')\n }\n if (!comparator) {\n throw error('Could not find attribute selector comparator')\n }\n /* c8 ignore stop */\n\n return filterAttributes(state, comparator, value, 'name', true)\n}\n"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { GraphLike } from '@vltpkg/graph';
|
|
2
|
+
import { type ParserState, QueryResponse } from './types.js';
|
|
3
|
+
export * from './types.js';
|
|
4
|
+
export declare const walk: (state: ParserState) => Promise<ParserState>;
|
|
5
|
+
export type QueryOptions = {
|
|
6
|
+
graph: GraphLike;
|
|
7
|
+
};
|
|
8
|
+
export declare class Query {
|
|
9
|
+
#private;
|
|
10
|
+
constructor({ graph }: QueryOptions);
|
|
11
|
+
search(query: string): Promise<QueryResponse>;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAY,SAAS,EAAY,MAAM,eAAe,CAAA;AAOlE,OAAO,EAEL,KAAK,WAAW,EAKhB,aAAa,EACd,MAAM,YAAY,CAAA;AAEnB,cAAc,YAAY,CAAA;AAiC1B,eAAO,MAAM,IAAI,UACR,WAAW,KACjB,OAAO,CAAC,WAAW,CAiDrB,CAAA;AAED,MAAM,MAAM,YAAY,GAAG;IACzB,KAAK,EAAE,SAAS,CAAA;CACjB,CAAA;AAED,qBAAa,KAAK;;gBAIJ,EAAE,KAAK,EAAE,EAAE,YAAY;IAK7B,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;CA0CpD"}
|