@markuplint/ml-core 4.6.1 → 4.7.0

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.
@@ -1,10 +1,11 @@
1
1
  import type { MLDocument } from './document.js';
2
2
  import type { MLElement } from './element.js';
3
3
  import type { MarkuplintPreprocessorBlockType } from './types.js';
4
- import type { MLASTPreprocessorSpecificBlock } from '@markuplint/ml-ast';
4
+ import type { MLASTPreprocessorSpecificBlock, MLASTPreprocessorSpecificBlockConditionalType } from '@markuplint/ml-ast';
5
5
  import type { PlainData, RuleConfigValue } from '@markuplint/ml-config';
6
6
  import { MLNode } from './node.js';
7
7
  export declare class MLBlock<T extends RuleConfigValue, O extends PlainData = undefined> extends MLNode<T, O, MLASTPreprocessorSpecificBlock> {
8
+ readonly conditionalType: MLASTPreprocessorSpecificBlockConditionalType;
8
9
  readonly isTransparent: boolean;
9
10
  constructor(astNode: MLASTPreprocessorSpecificBlock, document: MLDocument<T, O>);
10
11
  /**
@@ -7,6 +7,7 @@ export class MLBlock extends MLNode {
7
7
  super(astNode, document);
8
8
  // TODO:
9
9
  this.isTransparent = true;
10
+ this.conditionalType = astNode.conditionalType;
10
11
  }
11
12
  /**
12
13
  * Returns a string appropriate for the type of node as `MLBlock`
@@ -2341,6 +2341,9 @@ export class MLElement extends MLParentNode {
2341
2341
  hasMutableChildren(attr = false) {
2342
2342
  for (const child of this.childNodes) {
2343
2343
  if (child.is(child.MARKUPLINT_PREPROCESSOR_BLOCK)) {
2344
+ if (child.conditionalType) {
2345
+ continue;
2346
+ }
2344
2347
  return true;
2345
2348
  }
2346
2349
  if (child.is(child.ELEMENT_NODE)) {
@@ -353,6 +353,20 @@ export declare abstract class MLNode<T extends RuleConfigValue, O extends PlainD
353
353
  * @see https://dom.spec.whatwg.org/#ref-for-dom-node-comparedocumentposition%E2%91%A0
354
354
  */
355
355
  compareDocumentPosition(other: Node): number;
356
+ /**
357
+ * Returns an array of NodeLists representing the conditional child nodes of the current node.
358
+ * Conditional child nodes are nodes that are nested within
359
+ * **preprocessor blocks** such as `if`, `each`, or `switch`.
360
+ * Each NodeList represents a branch of conditional child nodes.
361
+ *
362
+ * Note: NodeList doesn't include whitespace nodes.
363
+ *
364
+ * @returns An array of NodeLists representing the conditional child nodes.
365
+ *
366
+ * @experemental
367
+ * @implements `@markuplint/ml-core` API: `MLNode`
368
+ */
369
+ conditionalChildNodes(): NodeListOf<MLChildNode<T, O>>[];
356
370
  /**
357
371
  * Returns true if other is an inclusive descendant of node; otherwise false.
358
372
  *
@@ -9,7 +9,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10
10
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
11
  };
12
- var _MLNode_childNodes, _MLNode_ownerDocument, _MLNode_prevToken;
12
+ var _MLNode_childNodes, _MLNode_ownerDocument, _MLNode_prevToken, _MLNode_conditionalChildNodes;
13
+ import { branchesToPatterns } from '@markuplint/shared';
13
14
  import { MLToken } from '../token/token.js';
14
15
  import { isChildNode } from './child-node.js';
15
16
  import { toNodeList } from './node-list.js';
@@ -132,6 +133,10 @@ export class MLNode extends MLToken {
132
133
  * Cached `prevToken` property
133
134
  */
134
135
  _MLNode_prevToken.set(this, void 0);
136
+ /**
137
+ * Cached `conditionalChildNodes` mothod
138
+ */
139
+ _MLNode_conditionalChildNodes.set(this, void 0);
135
140
  /**
136
141
  *
137
142
  */
@@ -550,6 +555,85 @@ export class MLNode extends MLToken {
550
555
  other) {
551
556
  throw new UnexpectedCallError('Not supported "compareDocumentPosition" method');
552
557
  }
558
+ /**
559
+ * Returns an array of NodeLists representing the conditional child nodes of the current node.
560
+ * Conditional child nodes are nodes that are nested within
561
+ * **preprocessor blocks** such as `if`, `each`, or `switch`.
562
+ * Each NodeList represents a branch of conditional child nodes.
563
+ *
564
+ * Note: NodeList doesn't include whitespace nodes.
565
+ *
566
+ * @returns An array of NodeLists representing the conditional child nodes.
567
+ *
568
+ * @experemental
569
+ * @implements `@markuplint/ml-core` API: `MLNode`
570
+ */
571
+ conditionalChildNodes() {
572
+ if (__classPrivateFieldGet(this, _MLNode_conditionalChildNodes, "f") != null) {
573
+ return __classPrivateFieldGet(this, _MLNode_conditionalChildNodes, "f");
574
+ }
575
+ const branches = [];
576
+ let mode = null;
577
+ let openConditional = false;
578
+ let subBranches = [];
579
+ for (const child of this.childNodes) {
580
+ if (child.is(child.MARKUPLINT_PREPROCESSOR_BLOCK)) {
581
+ switch (child.conditionalType) {
582
+ case 'if':
583
+ case 'if:elseif': {
584
+ mode = 'if';
585
+ break;
586
+ }
587
+ case 'each': {
588
+ mode = 'each';
589
+ break;
590
+ }
591
+ case 'switch:case': {
592
+ mode = 'switch';
593
+ break;
594
+ }
595
+ /* No default mode */
596
+ case 'if:else':
597
+ case 'each:empty':
598
+ case 'switch:default':
599
+ case 'await':
600
+ case 'await:catch':
601
+ case 'await:then': {
602
+ break;
603
+ }
604
+ default: {
605
+ continue;
606
+ }
607
+ }
608
+ const conditionalChildNodes = child.conditionalChildNodes();
609
+ subBranches.push(...conditionalChildNodes.flatMap(nodeList => [...nodeList]));
610
+ openConditional = true;
611
+ continue;
612
+ }
613
+ if (openConditional) {
614
+ if (['if', 'each', 'switch'].includes(mode)) {
615
+ subBranches.push(null);
616
+ }
617
+ branches.push(subBranches);
618
+ mode = null;
619
+ openConditional = false;
620
+ subBranches = [];
621
+ }
622
+ if (child.is(child.TEXT_NODE) && child.isWhitespace()) {
623
+ continue;
624
+ }
625
+ branches.push(child);
626
+ }
627
+ if (subBranches.length > 0) {
628
+ if (['if', 'each', 'switch'].includes(mode)) {
629
+ subBranches.push(null);
630
+ }
631
+ branches.push(subBranches);
632
+ }
633
+ const cache = branchesToPatterns(branches).map(array => toNodeList(array));
634
+ __classPrivateFieldSet(this, _MLNode_conditionalChildNodes, cache, "f");
635
+ return cache;
636
+ }
553
637
  /**
554
638
  * Returns true if other is an inclusive descendant of node; otherwise false.
555
639
  *
@@ -763,4 +847,4 @@ export class MLNode extends MLToken {
763
847
  __classPrivateFieldSet(this, _MLNode_childNodes, childNodes ?? __classPrivateFieldGet(this, _MLNode_childNodes, "f"), "f");
764
848
  }
765
849
  }
766
- _MLNode_childNodes = new WeakMap(), _MLNode_ownerDocument = new WeakMap(), _MLNode_prevToken = new WeakMap();
850
+ _MLNode_childNodes = new WeakMap(), _MLNode_ownerDocument = new WeakMap(), _MLNode_prevToken = new WeakMap(), _MLNode_conditionalChildNodes = new WeakMap();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/ml-core",
3
- "version": "4.6.1",
3
+ "version": "4.7.0",
4
4
  "description": "The core module of markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -29,18 +29,19 @@
29
29
  },
30
30
  "dependencies": {
31
31
  "@markuplint/config-presets": "4.4.0",
32
- "@markuplint/html-parser": "4.5.1",
33
- "@markuplint/html-spec": "4.6.1",
32
+ "@markuplint/html-parser": "4.6.0",
33
+ "@markuplint/html-spec": "4.7.0",
34
34
  "@markuplint/i18n": "4.4.0",
35
- "@markuplint/ml-ast": "4.2.0",
36
- "@markuplint/ml-config": "4.5.1",
37
- "@markuplint/ml-spec": "4.4.1",
38
- "@markuplint/parser-utils": "4.5.1",
39
- "@markuplint/selector": "4.5.1",
35
+ "@markuplint/ml-ast": "4.3.0",
36
+ "@markuplint/ml-config": "4.6.0",
37
+ "@markuplint/ml-spec": "4.5.0",
38
+ "@markuplint/parser-utils": "4.6.0",
39
+ "@markuplint/selector": "4.6.0",
40
+ "@markuplint/shared": "4.3.0",
40
41
  "@types/debug": "4.1.12",
41
42
  "debug": "4.3.4",
42
43
  "is-plain-object": "5.0.0",
43
- "type-fest": "4.15.0"
44
+ "type-fest": "4.18.1"
44
45
  },
45
- "gitHead": "b029c86a6b3a9ea8189d2e5535e3023aaea753fd"
46
+ "gitHead": "b8d7bae9bdcdad63ff79abe21b88be12abde3633"
46
47
  }