@markuplint/parser-utils 4.0.0-dev.10 → 4.0.0-dev.12

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.
Files changed (47) hide show
  1. package/LICENSE +1 -1
  2. package/lib/attr-tokenizer.d.ts +16 -4
  3. package/lib/attr-tokenizer.js +164 -70
  4. package/lib/const.d.ts +2 -1
  5. package/lib/const.js +2 -1
  6. package/lib/debugger.d.ts +3 -2
  7. package/lib/debugger.js +35 -19
  8. package/lib/enums.d.ts +16 -0
  9. package/lib/enums.js +18 -0
  10. package/lib/get-location.d.ts +4 -13
  11. package/lib/get-location.js +10 -21
  12. package/lib/ignore-block.d.ts +3 -2
  13. package/lib/ignore-block.js +62 -124
  14. package/lib/ignore-front-matter.d.ts +4 -1
  15. package/lib/ignore-front-matter.js +12 -3
  16. package/lib/index.d.ts +3 -16
  17. package/lib/index.js +3 -16
  18. package/lib/parser-error.d.ts +1 -0
  19. package/lib/parser-error.js +1 -0
  20. package/lib/parser.d.ts +108 -0
  21. package/lib/parser.js +1076 -0
  22. package/lib/script-parser.d.ts +1 -1
  23. package/lib/script-parser.js +1 -1
  24. package/lib/sort-nodes.d.ts +2 -0
  25. package/lib/sort-nodes.js +18 -0
  26. package/lib/types.d.ts +34 -0
  27. package/package.json +9 -5
  28. package/lib/attr-parser.d.ts +0 -25
  29. package/lib/attr-parser.js +0 -188
  30. package/lib/create-token.d.ts +0 -4
  31. package/lib/create-token.js +0 -29
  32. package/lib/flatten-nodes.d.ts +0 -2
  33. package/lib/flatten-nodes.js +0 -247
  34. package/lib/get-space-before.d.ts +0 -1
  35. package/lib/get-space-before.js +0 -8
  36. package/lib/parse-attr.d.ts +0 -24
  37. package/lib/parse-attr.js +0 -144
  38. package/lib/remove-deprecated-node.d.ts +0 -7
  39. package/lib/remove-deprecated-node.js +0 -39
  40. package/lib/siblings-correction.d.ts +0 -9
  41. package/lib/siblings-correction.js +0 -21
  42. package/lib/tag-parser.d.ts +0 -10
  43. package/lib/tag-parser.js +0 -152
  44. package/lib/tag-splitter.d.ts +0 -7
  45. package/lib/tag-splitter.js +0 -96
  46. package/lib/walker.d.ts +0 -2
  47. package/lib/walker.js +0 -18
package/lib/parse-attr.js DELETED
@@ -1,144 +0,0 @@
1
- import { createTokenFromRawCode, tokenizer, uuid } from './create-token.js';
2
- export const defaultValueDelimiters = [
3
- {
4
- start: "'",
5
- end: "'",
6
- },
7
- {
8
- start: '"',
9
- end: '"',
10
- },
11
- ];
12
- const defaultEqual = '=';
13
- const spaceRegex = /^\s$/;
14
- export function parseAttr(raw, offset, html, options) {
15
- const tokens = tokenize(raw, options);
16
- tokens.beforeName;
17
- const attrToken = createTokenFromRawCode(raw, offset, html);
18
- const spacesBeforeName = tokenizer(tokens.beforeName, attrToken.startLine, attrToken.startCol, attrToken.startOffset);
19
- const name = tokenizer(tokens.name, spacesBeforeName.endLine, spacesBeforeName.endCol, spacesBeforeName.endOffset);
20
- const spacesBeforeEqual = tokenizer(tokens.afterName, name.endLine, name.endCol, name.endOffset);
21
- const equal = tokenizer(tokens.equal, spacesBeforeEqual.endLine, spacesBeforeEqual.endCol, spacesBeforeEqual.endOffset);
22
- const spacesAfterEqual = tokenizer(tokens.beforeValue, equal.endLine, equal.endCol, equal.endOffset);
23
- const startQuote = tokenizer(tokens.startQuote, spacesAfterEqual.endLine, spacesAfterEqual.endCol, spacesAfterEqual.endOffset);
24
- const value = tokenizer(tokens.value, startQuote.endLine, startQuote.endCol, startQuote.endOffset);
25
- const endQuote = tokenizer(tokens.endQuote, value.endLine, value.endCol, value.endOffset);
26
- const attr = {
27
- type: 'html-attr',
28
- uuid: uuid(),
29
- raw: attrToken.raw,
30
- startOffset: attrToken.startOffset,
31
- endOffset: attrToken.endOffset,
32
- startLine: attrToken.startLine,
33
- endLine: attrToken.endLine,
34
- startCol: attrToken.startCol,
35
- endCol: attrToken.endCol,
36
- spacesBeforeName,
37
- name,
38
- spacesBeforeEqual,
39
- equal,
40
- spacesAfterEqual,
41
- startQuote,
42
- value,
43
- endQuote,
44
- isDuplicatable: false,
45
- nodeName: name.raw,
46
- parentNode: null,
47
- nextNode: null,
48
- prevNode: null,
49
- isFragment: false,
50
- isGhost: false,
51
- };
52
- return attr;
53
- }
54
- export function tokenize(raw, options) {
55
- const valueDelimiters = options?.valueDelimiters ?? defaultValueDelimiters;
56
- const equalDelimiter = options?.equal ?? defaultEqual;
57
- let state = 'b-name';
58
- const charactors = [...raw];
59
- let beforeName = '';
60
- let name = '';
61
- let afterName = '';
62
- let equal = '';
63
- let valueDelimiter = null;
64
- let beforeValue = '';
65
- let startQuote = '';
66
- let value = '';
67
- let endQuote = '';
68
- let afterAttr = '';
69
- while (charactors.length > 0) {
70
- const charactor = charactors.shift();
71
- if (state === 'b-name') {
72
- if (spaceRegex.test(charactor)) {
73
- beforeName += charactor;
74
- continue;
75
- }
76
- name += charactor;
77
- state = 'name';
78
- continue;
79
- }
80
- if (state === 'name') {
81
- if (equalDelimiter === charactor) {
82
- equal = equalDelimiter;
83
- state = 'value-start';
84
- continue;
85
- }
86
- if (spaceRegex.test(charactor)) {
87
- afterName += charactor;
88
- state = 'a-name';
89
- continue;
90
- }
91
- name += charactor;
92
- continue;
93
- }
94
- if (state === 'a-name') {
95
- if (equalDelimiter === charactor) {
96
- equal = equalDelimiter;
97
- state = 'value-start';
98
- continue;
99
- }
100
- if (spaceRegex.test(charactor)) {
101
- afterName += charactor;
102
- continue;
103
- }
104
- break;
105
- }
106
- if (state === 'value-start') {
107
- if (spaceRegex.test(charactor)) {
108
- beforeValue += charactor;
109
- continue;
110
- }
111
- valueDelimiter = valueDelimiters.find(d => d.start === charactor) ?? null;
112
- if (valueDelimiter) {
113
- startQuote += valueDelimiter.start;
114
- }
115
- else {
116
- value += beforeValue + charactor;
117
- beforeValue = '';
118
- }
119
- state = 'value';
120
- continue;
121
- }
122
- if (state !== 'value') {
123
- throw new Error('ParseError: unknown parse state in the attribute');
124
- }
125
- value += charactor;
126
- }
127
- if (valueDelimiter) {
128
- const endQuoteIndex = value.lastIndexOf(valueDelimiter.end);
129
- endQuote = value.slice(endQuoteIndex, endQuoteIndex + 1);
130
- afterAttr = value.slice(endQuoteIndex + 1);
131
- value = value.slice(0, endQuoteIndex);
132
- }
133
- return {
134
- beforeName,
135
- name,
136
- afterName,
137
- equal,
138
- beforeValue,
139
- startQuote,
140
- value,
141
- endQuote,
142
- afterAttr,
143
- };
144
- }
@@ -1,7 +0,0 @@
1
- import type { MLASTNode } from '@markuplint/ml-ast';
2
- /**
3
- *
4
- * @disruptive
5
- * @param nodeOrders [Disruptive change]
6
- */
7
- export declare function removeDeprecatedNode(nodeOrders: MLASTNode[]): void;
@@ -1,39 +0,0 @@
1
- /**
2
- *
3
- * @disruptive
4
- * @param nodeOrders [Disruptive change]
5
- */
6
- export function removeDeprecatedNode(
7
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
- nodeOrders) {
9
- /**
10
- * sorting
11
- */
12
- nodeOrders.sort((a, b) => {
13
- if (a.isGhost || b.isGhost) {
14
- return 0;
15
- }
16
- return a.startOffset - b.startOffset;
17
- });
18
- /**
19
- * remove duplicated node
20
- */
21
- const stack = {};
22
- const removeIndexes = [];
23
- for (const [i, node] of nodeOrders.entries()) {
24
- if (node.isGhost) {
25
- continue;
26
- }
27
- const id = `${node.startLine}:${node.startCol}:${node.endLine}:${node.endCol}`;
28
- if (stack[id] != null) {
29
- removeIndexes.push(i);
30
- }
31
- stack[id] = i;
32
- }
33
- let r = nodeOrders.length;
34
- while (r-- > 0) {
35
- if (removeIndexes.includes(r)) {
36
- nodeOrders.splice(r, 1);
37
- }
38
- }
39
- }
@@ -1,9 +0,0 @@
1
- import type { MLASTNode } from '@markuplint/ml-ast';
2
- /**
3
- * Correct the references to prevNode and nextNode in the order listed.
4
- *
5
- * @param nodeList
6
- * @affects nodeList[].prevNode
7
- * @affects nodeList[].nextNode
8
- */
9
- export declare function siblingsCorrection(nodeList: MLASTNode[]): void;
@@ -1,21 +0,0 @@
1
- /**
2
- * Correct the references to prevNode and nextNode in the order listed.
3
- *
4
- * @param nodeList
5
- * @affects nodeList[].prevNode
6
- * @affects nodeList[].nextNode
7
- */
8
- export function siblingsCorrection(
9
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
10
- nodeList) {
11
- for (let i = 0; i < nodeList.length; i++) {
12
- const prevNode = nodeList[i - 1] ?? null;
13
- const node = nodeList[i];
14
- if (!node) {
15
- continue;
16
- }
17
- const nextNode = nodeList[i + 1] ?? null;
18
- node.prevNode = prevNode;
19
- node.nextNode = nextNode;
20
- }
21
- }
@@ -1,10 +0,0 @@
1
- import type { MLASTAttr } from '@markuplint/ml-ast';
2
- export declare function tagParser(raw: string, startLine: number, startCol: number, startOffset: number, offsetOffset?: number, offsetLine?: number, offsetColumn?: number, spaces?: ReadonlyArray<string>): {
3
- beforeOpenTag: string;
4
- tagName: string;
5
- attrs: MLASTAttr[];
6
- afterAttrSpaces: import("@markuplint/ml-ast").MLToken;
7
- selfClosingSolidus: import("@markuplint/ml-ast").MLToken;
8
- isOpenTag: boolean;
9
- leftover: string;
10
- };
package/lib/tag-parser.js DELETED
@@ -1,152 +0,0 @@
1
- import { attrTokenizer } from './attr-tokenizer.js';
2
- import { defaultSpaces } from './const.js';
3
- import { tokenizer } from './create-token.js';
4
- var TagState;
5
- (function (TagState) {
6
- TagState[TagState["BeforeOpenTag"] = 0] = "BeforeOpenTag";
7
- TagState[TagState["FirstCharOfTagName"] = 1] = "FirstCharOfTagName";
8
- TagState[TagState["TagName"] = 2] = "TagName";
9
- TagState[TagState["Attrs"] = 3] = "Attrs";
10
- TagState[TagState["AfterAttrs"] = 4] = "AfterAttrs";
11
- TagState[TagState["AfterOpenTag"] = 5] = "AfterOpenTag";
12
- })(TagState || (TagState = {}));
13
- export function tagParser(raw, startLine, startCol, startOffset, offsetOffset = 0, offsetLine = 0, offsetColumn = 0, spaces = defaultSpaces) {
14
- let offset = startOffset + offsetOffset;
15
- let line = startLine + offsetLine;
16
- let col = startCol + (startLine === 1 ? offsetColumn : 0);
17
- let state = TagState.BeforeOpenTag;
18
- let beforeOpenTagChars = '';
19
- let tagName = '';
20
- let afterAttrsSpaceChars = '';
21
- let selfClosingSolidusChar = '';
22
- let isOpenTag = true;
23
- const attrs = [];
24
- const chars = [...raw];
25
- while (chars.length > 0) {
26
- if (state === TagState.AfterOpenTag) {
27
- break;
28
- }
29
- const char = chars.shift();
30
- stateSwitch: switch (state) {
31
- case TagState.BeforeOpenTag: {
32
- if (char === '<') {
33
- const beforeOpenTag = tokenizer(beforeOpenTagChars, line, col, offset);
34
- line = beforeOpenTag.endLine;
35
- col = beforeOpenTag.endCol;
36
- offset = beforeOpenTag.endOffset;
37
- // Add `<` length
38
- col += 1;
39
- offset += 1;
40
- state = TagState.FirstCharOfTagName;
41
- break;
42
- }
43
- beforeOpenTagChars += char;
44
- break;
45
- }
46
- case TagState.FirstCharOfTagName: {
47
- if (/[a-z]/i.test(char)) {
48
- tagName += char;
49
- state = TagState.TagName;
50
- break;
51
- }
52
- if (char === '/') {
53
- isOpenTag = false;
54
- break;
55
- }
56
- chars.unshift(char);
57
- state = TagState.AfterOpenTag;
58
- break;
59
- }
60
- case TagState.TagName: {
61
- if (spaces.includes(char)) {
62
- chars.unshift(char);
63
- if (!isOpenTag) {
64
- // Add `/` of `</`(close tag) length
65
- offset += 1;
66
- col += 1;
67
- }
68
- offset += tagName.length;
69
- col += tagName.length;
70
- state = TagState.Attrs;
71
- break;
72
- }
73
- if (char === '/') {
74
- chars.unshift(char);
75
- state = TagState.AfterAttrs;
76
- break;
77
- }
78
- if (char === '>') {
79
- state = TagState.AfterOpenTag;
80
- break;
81
- }
82
- tagName += char;
83
- break;
84
- }
85
- case TagState.Attrs: {
86
- let leftover = char + chars.join('');
87
- while (leftover.trim()) {
88
- if (leftover.trim().startsWith('/') || leftover.trim().startsWith('>')) {
89
- chars.length = 0;
90
- chars.push(...leftover);
91
- state = TagState.AfterAttrs;
92
- break stateSwitch;
93
- }
94
- const attr = attrTokenizer(leftover, line, col, offset);
95
- line = attr.endLine;
96
- col = attr.endCol;
97
- offset = attr.endOffset;
98
- if (leftover === attr.__leftover) {
99
- throw new SyntaxError(`Invalid attribute syntax: ${leftover}`);
100
- }
101
- leftover = attr.__leftover ?? '';
102
- delete attr.__leftover;
103
- attrs.push(attr);
104
- }
105
- break;
106
- }
107
- case TagState.AfterAttrs: {
108
- if (char === '>') {
109
- state = TagState.AfterOpenTag;
110
- break;
111
- }
112
- if (spaces.includes(char)) {
113
- afterAttrsSpaceChars += char;
114
- break;
115
- }
116
- if (char === '/') {
117
- selfClosingSolidusChar = char;
118
- break;
119
- }
120
- throw new SyntaxError(`Invalid tag syntax: "${raw}"`);
121
- }
122
- }
123
- }
124
- const leftover = chars.join('');
125
- if ((!leftover && state === TagState.TagName) || tagName === '') {
126
- throw new SyntaxError(`Invalid tag syntax: "${raw}"`);
127
- }
128
- // console.log({
129
- // state,
130
- // leftover,
131
- // afterAttrsSpaceChars,
132
- // selfClosingSolidusChar,
133
- // attrs: attrs.map(a => a.raw),
134
- // });
135
- const afterAttrSpaces = tokenizer(afterAttrsSpaceChars, line, col, offset);
136
- line = afterAttrSpaces.endLine;
137
- col = afterAttrSpaces.endCol;
138
- offset = afterAttrSpaces.endOffset;
139
- const selfClosingSolidus = tokenizer(selfClosingSolidusChar, line, col, offset);
140
- line = selfClosingSolidus.endLine;
141
- col = selfClosingSolidus.endCol;
142
- offset = selfClosingSolidus.endOffset;
143
- return {
144
- beforeOpenTag: beforeOpenTagChars,
145
- tagName,
146
- attrs,
147
- afterAttrSpaces,
148
- selfClosingSolidus,
149
- isOpenTag,
150
- leftover,
151
- };
152
- }
@@ -1,7 +0,0 @@
1
- export interface N {
2
- type: 'text' | 'starttag' | 'endtag' | 'comment' | 'boguscomment';
3
- raw: string;
4
- line: number;
5
- col: number;
6
- }
7
- export declare function tagSplitter(raw: string, line: number, col: number): N[];
@@ -1,96 +0,0 @@
1
- import { reSplitterTag, reTagName } from './const.js';
2
- import { getEndCol, getEndLine } from '@markuplint/parser-utils';
3
- export function tagSplitter(raw, line, col) {
4
- return withLocation(tagSplitterAsString(raw), line, col);
5
- }
6
- function tagSplitterAsString(raw) {
7
- const tagMatches = raw.match(reSplitterTag);
8
- if (!tagMatches) {
9
- return [raw];
10
- }
11
- const tokens = [...tagMatches];
12
- tokens.unshift(); // remove all match
13
- const nodes = [];
14
- let rest = raw;
15
- for (const token of tokens) {
16
- const index = rest.indexOf(token);
17
- let length = token.length;
18
- if (index > 0) {
19
- const text = rest.slice(0, index);
20
- nodes.push(text);
21
- length += text.length;
22
- }
23
- nodes.push(token);
24
- rest = rest.slice(length);
25
- }
26
- if (rest) {
27
- nodes.push(rest);
28
- }
29
- return nodes;
30
- }
31
- function withLocation(nodes, line, col) {
32
- const result = [];
33
- for (const node of nodes) {
34
- if (node[0] === '<') {
35
- const label = node.slice(1).slice(0, -1);
36
- if (reTagName.test(label)) {
37
- result.push({
38
- type: 'starttag',
39
- raw: node,
40
- line,
41
- col,
42
- });
43
- }
44
- else {
45
- switch (label[0]) {
46
- case '/': {
47
- result.push({
48
- type: 'endtag',
49
- raw: node,
50
- line,
51
- col,
52
- });
53
- break;
54
- }
55
- case '!': {
56
- result.push({
57
- type: 'comment',
58
- raw: node,
59
- line,
60
- col,
61
- });
62
- break;
63
- }
64
- case '?': {
65
- result.push({
66
- type: 'boguscomment',
67
- raw: node,
68
- line,
69
- col,
70
- });
71
- break;
72
- }
73
- default: {
74
- result.push({
75
- type: 'text',
76
- raw: node,
77
- line,
78
- col,
79
- });
80
- }
81
- }
82
- }
83
- }
84
- else {
85
- result.push({
86
- type: 'text',
87
- raw: node,
88
- line,
89
- col,
90
- });
91
- }
92
- line = getEndLine(node, line);
93
- col = getEndCol(node, col);
94
- }
95
- return result;
96
- }
package/lib/walker.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { MLASTNode, Walker } from '@markuplint/ml-ast';
2
- export declare function walk(nodeList: readonly MLASTNode[], walker: Walker, depth?: number): void;
package/lib/walker.js DELETED
@@ -1,18 +0,0 @@
1
- export function walk(
2
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
3
- nodeList, walker, depth = 0) {
4
- for (const node of nodeList) {
5
- walker(node, depth);
6
- if ('childNodes' in node) {
7
- if (node.type === 'endtag') {
8
- continue;
9
- }
10
- if (node.childNodes && node.childNodes.length > 0) {
11
- walk(node.childNodes, walker, depth + 1);
12
- }
13
- if ('pearNode' in node && node.pearNode) {
14
- walker(node.pearNode, depth);
15
- }
16
- }
17
- }
18
- }