@markuplint/parser-utils 3.6.1 → 3.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 +1 @@
1
- export declare function getSpaceBefore(offset: number, rawCode: string): import("@markuplint/ml-ast").MLToken;
1
+ export declare function getSpaceBefore(offset: number, rawCode: string): import('@markuplint/ml-ast').MLToken;
@@ -1,24 +1,27 @@
1
1
  import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
2
2
  type ParseAttrOptions = {
3
- readonly booleanish?: boolean;
4
- readonly valueDelimiters?: readonly ValueDelimiter[];
5
- readonly equal?: string;
3
+ readonly booleanish?: boolean;
4
+ readonly valueDelimiters?: readonly ValueDelimiter[];
5
+ readonly equal?: string;
6
6
  };
7
7
  type ValueDelimiter = {
8
- readonly start: string;
9
- readonly end: string;
8
+ readonly start: string;
9
+ readonly end: string;
10
10
  };
11
11
  export declare const defaultValueDelimiters: readonly ValueDelimiter[];
12
12
  export declare function parseAttr(raw: string, offset: number, html: string, options?: ParseAttrOptions): MLASTHTMLAttr;
13
- export declare function tokenize(raw: string, options?: ParseAttrOptions): {
14
- beforeName: string;
15
- name: string;
16
- afterName: string;
17
- equal: string;
18
- beforeValue: string;
19
- startQuote: string;
20
- value: string;
21
- endQuote: string;
22
- afterAttr: string;
13
+ export declare function tokenize(
14
+ raw: string,
15
+ options?: ParseAttrOptions,
16
+ ): {
17
+ beforeName: string;
18
+ name: string;
19
+ afterName: string;
20
+ equal: string;
21
+ beforeValue: string;
22
+ startQuote: string;
23
+ value: string;
24
+ endQuote: string;
25
+ afterAttr: string;
23
26
  };
24
27
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/parser-utils",
3
- "version": "3.6.1",
3
+ "version": "3.7.0",
4
4
  "description": "Utility module for markuplint parser plugin",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -18,18 +18,16 @@
18
18
  "build": "tsc",
19
19
  "clean": "tsc --build --clean"
20
20
  },
21
- "devDependencies": {
22
- "@types/uuid": "^9.0.0",
23
- "type-fest": "^3.7.0"
24
- },
25
21
  "dependencies": {
26
22
  "@markuplint/ml-ast": "3.1.0",
27
- "@markuplint/types": "3.5.1",
23
+ "@markuplint/types": "3.6.0",
24
+ "@types/uuid": "^9.0.0",
28
25
  "tslib": "^2.4.1",
26
+ "type-fest": "^3.8.0",
29
27
  "uuid": "^9.0.0"
30
28
  },
31
29
  "peerDependencies": {
32
30
  "@markuplint/ml-core": "3.x"
33
31
  },
34
- "gitHead": "3cdf5a088b2da03773d5d4461d0e65ec32290a00"
32
+ "gitHead": "adc6e432cccba7cfad0dc8bf9f92e5aaf1107359"
35
33
  }
@@ -0,0 +1,17 @@
1
+ const { tokenizer } = require('../lib/create-token');
2
+
3
+ describe('tokenizer', () => {
4
+ it('empty', () => {
5
+ expect(tokenizer('', 1, 1, 0)).toEqual(
6
+ expect.objectContaining({
7
+ raw: '',
8
+ startLine: 1,
9
+ startCol: 1,
10
+ startOffset: 0,
11
+ endLine: 1,
12
+ endCol: 1,
13
+ endOffset: 0,
14
+ }),
15
+ );
16
+ });
17
+ });
@@ -0,0 +1,31 @@
1
+ const { isDocumentFragment, createTree } = require('@markuplint/html-parser');
2
+
3
+ const { flattenNodes } = require('../lib/flatten-nodes');
4
+
5
+ function toTree(rawCode) {
6
+ const isFragment = isDocumentFragment(rawCode);
7
+ return createTree(rawCode, isFragment, 0, 0, 0);
8
+ }
9
+
10
+ describe('flattenNodes', () => {
11
+ it('a node', () => {
12
+ const raw = '<div></div>';
13
+ const tree = toTree(raw);
14
+ const list = flattenNodes(tree, raw);
15
+ expect(list.length).toStrictEqual(2);
16
+ });
17
+
18
+ it('one depth', () => {
19
+ const raw = '<div><span></span></div>';
20
+ const tree = toTree(raw);
21
+ const list = flattenNodes(tree, raw);
22
+ expect(list.length).toStrictEqual(4);
23
+ });
24
+
25
+ it('two depth', () => {
26
+ const raw = '<div><span><span></span></span></div>';
27
+ const tree = toTree(raw);
28
+ const list = flattenNodes(tree, raw);
29
+ expect(list.length).toStrictEqual(6);
30
+ });
31
+ });
@@ -0,0 +1,38 @@
1
+ const { getEndCol, getEndLine } = require('../lib/get-location');
2
+
3
+ describe('getEndLine', () => {
4
+ it('empty', () => {
5
+ expect(getEndLine('', 1)).toBe(1);
6
+ expect(getEndLine('', 3)).toBe(3);
7
+ });
8
+
9
+ it('basic', () => {
10
+ expect(getEndLine('\n\n', 2)).toBe(4);
11
+ expect(getEndLine('\n\n', 4)).toBe(6);
12
+ expect(getEndLine('\n\n \n \n foo', 1)).toBe(5);
13
+ expect(getEndLine('\n\n \n \n foo', 6)).toBe(10);
14
+ });
15
+ });
16
+
17
+ describe('getEndCol', () => {
18
+ it('empty', () => {
19
+ expect(getEndCol('', 1)).toBe(1);
20
+ expect(getEndCol('', 3)).toBe(3);
21
+ });
22
+
23
+ it('basic', () => {
24
+ expect(getEndCol(' ', 2)).toBe(4);
25
+ expect(getEndCol(' ', 4)).toBe(6);
26
+ expect(getEndCol('foo bar', 1)).toBe(8);
27
+ expect(getEndCol('foo bar', 6)).toBe(13);
28
+ });
29
+
30
+ it('with line break', () => {
31
+ expect(getEndCol('foo bar\n', 4)).toBe(1);
32
+ expect(getEndCol('foo bar\n', 4)).toBe(1);
33
+ expect(getEndCol('foo bar\n ', 4)).toBe(3);
34
+ expect(getEndCol('foo bar\n ', 4)).toBe(3);
35
+ expect(getEndCol('foo bar\nfoo bar', 1)).toBe(8);
36
+ expect(getEndCol('foo bar\nfoo bar', 6)).toBe(8);
37
+ });
38
+ });
@@ -0,0 +1,274 @@
1
+ const { parse } = require('@markuplint/html-parser');
2
+
3
+ const { nodeListToDebugMaps } = require('../lib/debugger');
4
+ const { ignoreBlock, restoreNode } = require('../lib/ignore-block');
5
+
6
+ const tags = [
7
+ {
8
+ type: 'ejs-tag',
9
+ start: /<%/,
10
+ end: /%>/,
11
+ },
12
+ ];
13
+
14
+ describe('ignoreBlock', () => {
15
+ it('basic', () => {
16
+ const result = ignoreBlock('<div><%= test %></div>', tags);
17
+ expect(result).toStrictEqual({
18
+ source: '<div><%= test %></div>',
19
+ replaced: '<div><!></div>',
20
+ stack: [
21
+ {
22
+ type: 'ejs-tag',
23
+ index: 5,
24
+ startTag: '<%',
25
+ taggedCode: '= test ',
26
+ endTag: '%>',
27
+ },
28
+ ],
29
+ maskChar: '',
30
+ });
31
+ });
32
+
33
+ it('2 tags', () => {
34
+ const result = ignoreBlock('<div><%= test %></div><div><%= test2 %></div>', tags);
35
+ expect(result).toStrictEqual({
36
+ source: '<div><%= test %></div><div><%= test2 %></div>',
37
+ replaced: '<div><!></div><div><!></div>',
38
+ stack: [
39
+ {
40
+ type: 'ejs-tag',
41
+ index: 5,
42
+ startTag: '<%',
43
+ taggedCode: '= test ',
44
+ endTag: '%>',
45
+ },
46
+ {
47
+ type: 'ejs-tag',
48
+ index: 27,
49
+ startTag: '<%',
50
+ taggedCode: '= test2 ',
51
+ endTag: '%>',
52
+ },
53
+ ],
54
+ maskChar: '',
55
+ });
56
+ });
57
+
58
+ it('without closing tag', () => {
59
+ const result = ignoreBlock('<div><%= test', tags);
60
+ expect(result).toStrictEqual({
61
+ source: '<div><%= test',
62
+ replaced: '<div><!>',
63
+ stack: [
64
+ {
65
+ type: 'ejs-tag',
66
+ index: 5,
67
+ startTag: '<%',
68
+ taggedCode: '= test',
69
+ endTag: null,
70
+ },
71
+ ],
72
+ maskChar: '',
73
+ });
74
+ });
75
+
76
+ it('with line break', () => {
77
+ const result = ignoreBlock('<div><% if () {\n\t\n} %></div>', tags);
78
+ expect(result).toStrictEqual({
79
+ source: '<div><% if () {\n\t\n} %></div>',
80
+ replaced: '<div><!\n\n></div>',
81
+ stack: [
82
+ {
83
+ type: 'ejs-tag',
84
+ index: 5,
85
+ startTag: '<%',
86
+ taggedCode: ' if () {\n\t\n} ',
87
+ endTag: '%>',
88
+ },
89
+ ],
90
+ maskChar: '',
91
+ });
92
+ });
93
+
94
+ it('multiple tags', () => {
95
+ const result = ignoreBlock('<% 1 %>2<%= 3 %>4<%_ 5 _%>6<%- 7 -%>8<%% 9 %>', [
96
+ {
97
+ type: 'ejs-whitespace-slurping',
98
+ start: /<%_/,
99
+ end: /%>/,
100
+ },
101
+ {
102
+ type: 'ejs-output-value',
103
+ start: /<%=/,
104
+ end: /%>/,
105
+ },
106
+ {
107
+ type: 'ejs-output-unescaped',
108
+ start: /<%-/,
109
+ end: /%>/,
110
+ },
111
+ {
112
+ type: 'ejs-comment',
113
+ start: /<%#/,
114
+ end: /%>/,
115
+ },
116
+ {
117
+ type: 'ejs-scriptlet',
118
+ start: /<%(?!%)/,
119
+ end: /%>/,
120
+ },
121
+ ]);
122
+
123
+ expect(result).toStrictEqual({
124
+ source: '<% 1 %>2<%= 3 %>4<%_ 5 _%>6<%- 7 -%>8<%% 9 %>',
125
+ replaced: '<!>2<!>4<!>6<!>8<%% 9 %>',
126
+ stack: [
127
+ {
128
+ type: 'ejs-scriptlet',
129
+ index: 0,
130
+ startTag: '<%',
131
+ taggedCode: ' 1 ',
132
+ endTag: '%>',
133
+ },
134
+ {
135
+ type: 'ejs-output-value',
136
+ index: 8,
137
+ startTag: '<%=',
138
+ taggedCode: ' 3 ',
139
+ endTag: '%>',
140
+ },
141
+ {
142
+ type: 'ejs-whitespace-slurping',
143
+ index: 17,
144
+ startTag: '<%_',
145
+ taggedCode: ' 5 _',
146
+ endTag: '%>',
147
+ },
148
+ {
149
+ type: 'ejs-output-unescaped',
150
+ index: 27,
151
+ startTag: '<%-',
152
+ taggedCode: ' 7 -',
153
+ endTag: '%>',
154
+ },
155
+ ],
156
+ maskChar: '',
157
+ });
158
+ });
159
+ });
160
+
161
+ describe('restoreNode', () => {
162
+ it('basic', () => {
163
+ const code = '<div attr="<% attr %>"><% content %></div>';
164
+ const masked = ignoreBlock(code, tags);
165
+ const ast = parse(masked.replaced);
166
+ const restoredAst = restoreNode(ast.nodeList, masked);
167
+ const nodeMap = nodeListToDebugMaps(restoredAst, true);
168
+ // TODO: Remove the masks from Element.raw and Attribute.raw
169
+ expect(nodeMap).toStrictEqual([
170
+ '[1:1]>[1:24](0,23)div: <div␣attr="">',
171
+ '[1:6]>[1:23](5,22)attr: attr=""',
172
+ ' [1:5]>[1:6](4,5)bN: ␣',
173
+ ' [1:6]>[1:10](5,9)name: attr',
174
+ ' [1:10]>[1:10](9,9)bE: ',
175
+ ' [1:10]>[1:11](9,10)equal: =',
176
+ ' [1:11]>[1:11](10,10)aE: ',
177
+ ' [1:11]>[1:12](10,11)sQ: "',
178
+ ' [1:12]>[1:22](11,21)value: <%␣attr␣%>',
179
+ ' [1:22]>[1:23](21,22)eQ: "',
180
+ ' isDirective: false',
181
+ ' isDynamicValue: true',
182
+ '[1:24]>[1:37](23,36)#ps:ejs-tag: <%␣content␣%>',
183
+ '[1:37]>[1:43](36,42)div: </div>',
184
+ ]);
185
+ });
186
+
187
+ it('tag', () => {
188
+ const code = '<title><% content %></title>';
189
+ const masked = ignoreBlock(code, tags);
190
+ const ast = parse(masked.replaced);
191
+ const restoredAst = restoreNode(ast.nodeList, masked);
192
+ const nodeMap = nodeListToDebugMaps(restoredAst);
193
+ expect(nodeMap).toStrictEqual([
194
+ '[1:1]>[1:8](0,7)title: <title>',
195
+ '[1:8]>[1:21](7,20)#ps:ejs-tag: <%␣content␣%>',
196
+ '[1:21]>[1:29](20,28)title: </title>',
197
+ ]);
198
+ });
199
+
200
+ it('attr', () => {
201
+ const code = '<div attr="<% attr %><% attr2 %>"></div>';
202
+ const masked = ignoreBlock(code, tags);
203
+ const ast = parse(masked.replaced);
204
+ const restoredAst = restoreNode(ast.nodeList, masked);
205
+ expect(restoredAst[0].attributes[0].value.raw).toBe('<% attr %><% attr2 %>');
206
+ });
207
+
208
+ it('attr', () => {
209
+ const code = '<div attr="<% attr %> <% attr2 %>"></div>';
210
+ const masked = ignoreBlock(code, tags);
211
+ const ast = parse(masked.replaced);
212
+ const restoredAst = restoreNode(ast.nodeList, masked);
213
+ expect(restoredAst[0].attributes[0].value.raw).toBe('<% attr %> <% attr2 %>');
214
+ });
215
+
216
+ it('attr', () => {
217
+ const code = '<div attr="<% attr %>A<% attr2 %>"></div>';
218
+ const masked = ignoreBlock(code, tags);
219
+ const ast = parse(masked.replaced);
220
+ const restoredAst = restoreNode(ast.nodeList, masked);
221
+ expect(restoredAst[0].attributes[0].value.raw).toBe('<% attr %>A<% attr2 %>');
222
+ });
223
+
224
+ it('before space', () => {
225
+ const code = '<div attr=" <% attr %>"></div>';
226
+ const masked = ignoreBlock(code, tags);
227
+ const ast = parse(masked.replaced);
228
+ const restoredAst = restoreNode(ast.nodeList, masked);
229
+ expect(restoredAst[0].attributes[0].value.raw).toBe(' <% attr %>');
230
+ });
231
+
232
+ it('after space', () => {
233
+ const code = '<div attr=" <% attr %> "></div>';
234
+ const masked = ignoreBlock(code, tags);
235
+ const ast = parse(masked.replaced);
236
+ const restoredAst = restoreNode(ast.nodeList, masked);
237
+ expect(restoredAst[0].attributes[0].value.raw).toBe(' <% attr %> ');
238
+ });
239
+
240
+ it('before char', () => {
241
+ const code = '<div attr="A<% attr %>"></div>';
242
+ const masked = ignoreBlock(code, tags);
243
+ const ast = parse(masked.replaced);
244
+ const restoredAst = restoreNode(ast.nodeList, masked);
245
+ expect(restoredAst[0].attributes[0].value.raw).toBe('A<% attr %>');
246
+ });
247
+
248
+ it('after char', () => {
249
+ const code = '<div attr="A<% attr %>B"></div>';
250
+ const masked = ignoreBlock(code, tags);
251
+ const ast = parse(masked.replaced);
252
+ const restoredAst = restoreNode(ast.nodeList, masked);
253
+ expect(restoredAst[0].attributes[0].value.raw).toBe('A<% attr %>B');
254
+ });
255
+
256
+ it('unexpected parsing', () => {
257
+ const code = '<div attr="<% attr %> "></div>';
258
+ const masked = ignoreBlock(code, tags);
259
+ const ast = parse(masked.replaced);
260
+ const restoredAst = restoreNode(ast.nodeList, masked);
261
+ expect(restoredAst).toStrictEqual([]);
262
+ });
263
+ });
264
+
265
+ describe('Issues', () => {
266
+ test('#607', () => {
267
+ const code = '<div><% %><img/></div>';
268
+ const masked = ignoreBlock(code, tags);
269
+ const ast = parse(masked.replaced);
270
+ const restoredAst = restoreNode(ast.nodeList, masked);
271
+ expect(restoredAst[2].parentNode?.uuid).toBe(restoredAst[0].uuid);
272
+ expect(restoredAst[2].prevNode?.uuid).toBe(restoredAst[1].uuid);
273
+ });
274
+ });
@@ -0,0 +1,20 @@
1
+ const { ignoreFrontMatter } = require('../lib/ignore-front-matter');
2
+
3
+ describe('ignoreFrontMatter', () => {
4
+ it('basic', () => {
5
+ expect(ignoreFrontMatter('---')).toStrictEqual('---');
6
+ expect(ignoreFrontMatter('---\np: v')).toStrictEqual('---\np: v');
7
+ expect(ignoreFrontMatter('---\np: v\n---')).toStrictEqual('---\np: v\n---');
8
+ expect(ignoreFrontMatter('---\np: v\n---\n')).toStrictEqual(' \n \n \n');
9
+ });
10
+
11
+ it('basic', () => {
12
+ expect(
13
+ ignoreFrontMatter(`
14
+ ---
15
+ prop: value
16
+ ---
17
+ <html></html>`),
18
+ ).toStrictEqual('\n \n \n \n<html></html>');
19
+ });
20
+ });
@@ -0,0 +1,68 @@
1
+ const { tokenize, defaultValueDelimiters } = require('../lib/parse-attr');
2
+
3
+ describe('tokenize', () => {
4
+ test('name', () => {
5
+ expect(tokenize('a').name).toBe('a');
6
+ expect(tokenize('abc').name).toBe('abc');
7
+ expect(tokenize('abc:').name).toBe('abc:');
8
+ expect(tokenize(':abc').name).toBe(':abc');
9
+ expect(tokenize('xxx:abc').name).toBe('xxx:abc');
10
+ expect(tokenize('@abc').name).toBe('@abc');
11
+ });
12
+
13
+ test('value', () => {
14
+ expect(tokenize('a=b').value).toBe('b');
15
+ expect(tokenize('abc=xyz').value).toBe('xyz');
16
+ expect(tokenize('abc:="xyz"').value).toBe('xyz');
17
+ });
18
+
19
+ test('complex', () => {
20
+ expect(tokenize(' @a:x.y\n= x + y ')).toStrictEqual({
21
+ beforeName: ' ',
22
+ name: '@a:x.y',
23
+ afterName: '\n',
24
+ equal: '=',
25
+ beforeValue: '',
26
+ startQuote: '',
27
+ value: ' x + y ',
28
+ endQuote: '',
29
+ afterAttr: '',
30
+ });
31
+
32
+ expect(tokenize(' @a:x.y\n= " x\' + y " ')).toStrictEqual({
33
+ beforeName: ' ',
34
+ name: '@a:x.y',
35
+ afterName: '\n',
36
+ equal: '=',
37
+ beforeValue: ' ',
38
+ startQuote: '"',
39
+ value: " x' + y ",
40
+ endQuote: '"',
41
+ afterAttr: ' ',
42
+ });
43
+ });
44
+
45
+ test('jsx', () => {
46
+ expect(
47
+ tokenize(' className={classList.map((c) => `${c.toLowerCase()}`).join(",")} ', {
48
+ valueDelimiters: [
49
+ ...defaultValueDelimiters,
50
+ {
51
+ start: '{',
52
+ end: '}',
53
+ },
54
+ ],
55
+ }),
56
+ ).toStrictEqual({
57
+ beforeName: ' ',
58
+ name: 'className',
59
+ afterName: '',
60
+ equal: '=',
61
+ beforeValue: '',
62
+ startQuote: '{',
63
+ value: 'classList.map((c) => `${c.toLowerCase()}`).join(",")',
64
+ endQuote: '}',
65
+ afterAttr: ' ',
66
+ });
67
+ });
68
+ });