@vltpkg/dss-parser 1.0.0-rc.23 → 1.0.0-rc.25

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.
@@ -0,0 +1,15 @@
1
+ import type { Root } from 'postcss-selector-parser';
2
+ export * from './types.ts';
3
+ /**
4
+ * Escapes forward slashes in specific patterns matching @scoped/name paths
5
+ * This will allow usage of unescaped forward slashes necessary for scoped
6
+ * package names in the id selector.
7
+ */
8
+ export declare const escapeScopedNamesSlashes: (query: string) => string;
9
+ export declare const escapeDots: (query: string) => string;
10
+ export declare const unescapeDots: (query: string) => string;
11
+ /**
12
+ * Parses a CSS selector string into an AST
13
+ * Handles escaping of forward slashes in specific patterns
14
+ */
15
+ export declare const parse: (query: string) => Root;
package/dist/index.js ADDED
@@ -0,0 +1,92 @@
1
+ import postcssSelectorParser from 'postcss-selector-parser';
2
+ import { asSelectorNode, isCombinatorNode, isPseudoNode, isTagNode, } from "./types.js";
3
+ export * from "./types.js";
4
+ /**
5
+ * Escapes forward slashes in specific patterns matching @scoped/name paths
6
+ * This will allow usage of unescaped forward slashes necessary for scoped
7
+ * package names in the id selector.
8
+ */
9
+ export const escapeScopedNamesSlashes = (query) => query.replace(/(#@(\w|-|\.)+)\//gm, (_, scope) => `${scope}\\/`);
10
+ export const escapeDots = (query) => query.replaceAll('.', '\\.');
11
+ export const unescapeDots = (query) => query.replaceAll('\\.', '.');
12
+ const pseudoCleanUpNeeded = new Set([
13
+ ':published',
14
+ ':score',
15
+ ':malware',
16
+ ':severity',
17
+ ':sev',
18
+ ':squat',
19
+ ':semver',
20
+ ':v',
21
+ ]);
22
+ const hasParamsToEscape = (node) => pseudoCleanUpNeeded.has(node.value);
23
+ /**
24
+ * Parses a CSS selector string into an AST
25
+ * Handles escaping of forward slashes in specific patterns
26
+ */
27
+ export const parse = (query) => {
28
+ const escapedQuery = escapeDots(escapeScopedNamesSlashes(query));
29
+ const transformAst = (root) => {
30
+ root.walk((node) => {
31
+ // clean up the escaped dots
32
+ if (node.value && typeof node.value === 'string') {
33
+ node.value = unescapeDots(node.value);
34
+ }
35
+ if (isPseudoNode(node) && hasParamsToEscape(node)) {
36
+ // these are pseudo nodes that should only take strings as
37
+ // parameters, so in this preparse step we clean up anything
38
+ // that was recognized as a postcss node and transform that
39
+ // into something that can be most likely parsed as a string
40
+ for (const n of node.nodes) {
41
+ // the parameters have a selector node that wraps them up
42
+ const selector = asSelectorNode(n);
43
+ selector.nodes.forEach((currentNode, index, arr) => {
44
+ // get the next node, we'll update it later
45
+ const nextNode = arr[index + 1];
46
+ // if the current node is a combinator node, we'll need to
47
+ // escape it, we do so by removing the node entirely and
48
+ // updating the contents of the next node with its value
49
+ if (isCombinatorNode(currentNode) &&
50
+ isTagNode(nextNode)) {
51
+ nextNode.value = `${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}${nextNode.value}`;
52
+ // make sure to also update the source position
53
+ // references, those are used by the syntax highlighter
54
+ if (nextNode.source?.start?.line &&
55
+ currentNode.source?.start?.line) {
56
+ nextNode.source.start.line =
57
+ currentNode.source.start.line;
58
+ }
59
+ if (nextNode.source?.start?.column &&
60
+ currentNode.source?.start?.column) {
61
+ nextNode.source.start.column =
62
+ currentNode.source.start.column;
63
+ }
64
+ // removes the current node from the selector node
65
+ arr.splice(index, 1);
66
+ }
67
+ });
68
+ // after removing combinator nodes, if we end up with multiple
69
+ // tags in the selector node, we need to smush them together
70
+ selector.nodes.reduce((acc, currentNode) => {
71
+ if (currentNode === acc)
72
+ return acc;
73
+ acc.value = `${acc.value}${currentNode.spaces.before}${currentNode.value}${currentNode.spaces.after}`;
74
+ // make sure to also update the source position refs
75
+ if (currentNode.source?.end?.line &&
76
+ acc.source?.end?.line) {
77
+ acc.source.end.line = currentNode.source.end.line;
78
+ }
79
+ if (currentNode.source?.end?.column &&
80
+ acc.source?.end?.column) {
81
+ acc.source.end.column = currentNode.source.end.column;
82
+ }
83
+ return acc;
84
+ }, selector.first);
85
+ // the selector wrapper node should have a single node
86
+ selector.nodes.length = 1;
87
+ }
88
+ }
89
+ });
90
+ };
91
+ return postcssSelectorParser(transformAst).astSync(escapedQuery);
92
+ };
@@ -0,0 +1,25 @@
1
+ import type { Tag, String, Selector, Root, Pseudo, Nesting, Identifier, Comment, Combinator, ClassName, Attribute, Universal, tag, id, combinator, string, attribute, pseudo } from 'postcss-selector-parser';
2
+ export type PostcssNode = Tag | String | Selector | Root | Pseudo | Nesting | Identifier | Comment | Combinator | ClassName | Attribute | Universal;
3
+ export type PostCSSLeaf = ReturnType<typeof tag> | ReturnType<typeof id> | ReturnType<typeof attribute> | ReturnType<typeof combinator> | ReturnType<typeof pseudo> | ReturnType<typeof string>;
4
+ export type PostcssNodeWithChildren = Selector | Root | Pseudo;
5
+ export type ParsedSelectorToken = PostcssNode & {
6
+ token: string;
7
+ };
8
+ export declare const isPostcssNodeWithChildren: (node: any) => node is PostcssNodeWithChildren;
9
+ export declare const asPostcssNodeWithChildren: (node?: PostcssNode) => PostcssNodeWithChildren;
10
+ export declare const isAttributeNode: (node: unknown) => node is Attribute;
11
+ export declare const asAttributeNode: (node?: PostcssNode) => Attribute;
12
+ export declare const isCombinatorNode: (node: unknown) => node is Combinator;
13
+ export declare const asCombinatorNode: (node?: PostcssNode) => Combinator;
14
+ export declare const isIdentifierNode: (node: any) => node is Identifier;
15
+ export declare const asIdentifierNode: (node?: PostcssNode) => Identifier;
16
+ export declare const isSelectorNode: (node: any) => node is Selector;
17
+ export declare const asSelectorNode: (node?: PostcssNode) => Selector;
18
+ export declare const isPseudoNode: (node: unknown) => node is Pseudo;
19
+ export declare const asPseudoNode: (node?: PostcssNode) => Pseudo;
20
+ export declare const isTagNode: (node: unknown) => node is Tag;
21
+ export declare const asTagNode: (node?: PostcssNode) => Tag;
22
+ export declare const isStringNode: (node: unknown) => node is String;
23
+ export declare const asStringNode: (node?: PostcssNode) => String;
24
+ export declare const isCommentNode: (node: unknown) => node is Comment;
25
+ export declare const asCommentNode: (node?: PostcssNode) => Comment;
package/dist/types.js ADDED
@@ -0,0 +1,118 @@
1
+ import { error } from '@vltpkg/error-cause';
2
+ export const isPostcssNodeWithChildren = (node) => 'type' in node && 'nodes' in node;
3
+ export const asPostcssNodeWithChildren = (node) => {
4
+ if (!node) {
5
+ throw error('Expected a query node');
6
+ }
7
+ if (!isPostcssNodeWithChildren(node)) {
8
+ throw error('Not a query selector node with children', {
9
+ found: node,
10
+ });
11
+ }
12
+ return node;
13
+ };
14
+ const isObj = (o) => !!o && typeof o === 'object';
15
+ export const isAttributeNode = (node) => isObj(node) && !!node.attribute && node.type === 'attribute';
16
+ export const asAttributeNode = (node) => {
17
+ if (!node) {
18
+ throw error('Expected a query node');
19
+ }
20
+ if (!isAttributeNode(node)) {
21
+ throw error('Mismatching query node', {
22
+ wanted: 'attribute',
23
+ found: node.type,
24
+ });
25
+ }
26
+ return node;
27
+ };
28
+ export const isCombinatorNode = (node) => isObj(node) && !!node.value && node.type === 'combinator';
29
+ export const asCombinatorNode = (node) => {
30
+ if (!node) {
31
+ throw error('Expected a query node');
32
+ }
33
+ if (!isCombinatorNode(node)) {
34
+ throw error('Mismatching query node', {
35
+ wanted: 'combinator',
36
+ found: node.type,
37
+ });
38
+ }
39
+ return node;
40
+ };
41
+ export const isIdentifierNode = (node) => isObj(node) && !!node.value && node.type === 'id';
42
+ export const asIdentifierNode = (node) => {
43
+ if (!node) {
44
+ throw error('Expected a query node');
45
+ }
46
+ if (!isIdentifierNode(node)) {
47
+ throw error('Mismatching query node', {
48
+ wanted: 'id',
49
+ found: node.type,
50
+ });
51
+ }
52
+ return node;
53
+ };
54
+ export const isSelectorNode = (node) => isPostcssNodeWithChildren(node) && node.type === 'selector';
55
+ export const asSelectorNode = (node) => {
56
+ if (!node) {
57
+ throw error('Expected a query node');
58
+ }
59
+ if (!isSelectorNode(node)) {
60
+ throw error('Mismatching query node', {
61
+ wanted: 'selector',
62
+ found: node.type,
63
+ });
64
+ }
65
+ return node;
66
+ };
67
+ export const isPseudoNode = (node) => isObj(node) && !!node.value && node.type === 'pseudo';
68
+ export const asPseudoNode = (node) => {
69
+ if (!node) {
70
+ throw error('Expected a query node');
71
+ }
72
+ if (!isPseudoNode(node)) {
73
+ throw error('Mismatching query node', {
74
+ wanted: 'pseudo',
75
+ found: node.type,
76
+ });
77
+ }
78
+ return node;
79
+ };
80
+ export const isTagNode = (node) => isObj(node) && !!node.value && node.type === 'tag';
81
+ export const asTagNode = (node) => {
82
+ if (!node) {
83
+ throw error('Expected a query node');
84
+ }
85
+ if (!isTagNode(node)) {
86
+ throw error('Mismatching query node', {
87
+ wanted: 'tag',
88
+ found: node.type,
89
+ });
90
+ }
91
+ return node;
92
+ };
93
+ export const isStringNode = (node) => isObj(node) && !!node.value && node.type === 'string';
94
+ export const asStringNode = (node) => {
95
+ if (!node) {
96
+ throw error('Expected a query node');
97
+ }
98
+ if (!isStringNode(node)) {
99
+ throw error('Mismatching query node', {
100
+ wanted: 'string',
101
+ found: node.type,
102
+ });
103
+ }
104
+ return node;
105
+ };
106
+ export const isCommentNode = (node) => isObj(node) && !!node.value && node.type === 'comment';
107
+ export const asCommentNode = (node) => {
108
+ if (!node) {
109
+ throw error('Expected a query node');
110
+ }
111
+ if (!isCommentNode(node)) {
112
+ throw error('Mismatching query node', {
113
+ wanted: 'comment',
114
+ found: node.type,
115
+ });
116
+ }
117
+ return node;
118
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@vltpkg/dss-parser",
3
3
  "description": "The Dependency Selector Syntax (DSS) parser",
4
- "version": "1.0.0-rc.23",
4
+ "version": "1.0.0-rc.25",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "git+https://github.com/vltpkg/vltpkg.git",
@@ -12,7 +12,7 @@
12
12
  "email": "support@vlt.sh"
13
13
  },
14
14
  "dependencies": {
15
- "@vltpkg/error-cause": "1.0.0-rc.23",
15
+ "@vltpkg/error-cause": "1.0.0-rc.25",
16
16
  "postcss-selector-parser": "^7.1.1"
17
17
  },
18
18
  "devDependencies": {