@markuplint/svelte-parser 3.4.0 → 3.5.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.
package/lib/attr.d.ts CHANGED
@@ -1,10 +1,5 @@
1
1
  import type { SvelteDirective } from './svelte-parser';
2
2
  import type { MLASTAttr } from '@markuplint/ml-ast';
3
- export declare function attr(
4
- attr: SvelteDirective,
5
- rawHTML: string,
6
- ):
7
- | MLASTAttr
8
- | {
9
- __spreadAttr: true;
10
- };
3
+ export declare function attr(attr: SvelteDirective, rawHTML: string): MLASTAttr | {
4
+ __spreadAttr: true;
5
+ };
package/lib/attr.js CHANGED
@@ -6,7 +6,9 @@ const html_parser_1 = require("@markuplint/html-parser");
6
6
  const parser_utils_1 = require("@markuplint/parser-utils");
7
7
  const directive_tokenizer_1 = tslib_1.__importDefault(require("./directive-tokenizer"));
8
8
  const specificBindDirective = ['bind:group', 'bind:this'];
9
- function attr(attr, rawHTML) {
9
+ function attr(
10
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
+ attr, rawHTML) {
10
12
  const isShorthand = attr.value && Array.isArray(attr.value)
11
13
  ? attr.value.some((val) => val.type === 'AttributeShorthand')
12
14
  : false;
@@ -1,8 +1,2 @@
1
1
  import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
2
- export default function directiveTokenizer(
3
- raw: string,
4
- rawValue: string,
5
- line: number,
6
- col: number,
7
- startOffset: number,
8
- ): MLASTHTMLAttr;
2
+ export default function directiveTokenizer(raw: string, rawValue: string, line: number, col: number, startOffset: number): MLASTHTMLAttr;
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { parse } from './parse';
2
- export declare const endTag = 'xml';
2
+ export declare const endTag = "xml";
package/lib/nodeize.d.ts CHANGED
@@ -1,9 +1,3 @@
1
1
  import type { SvelteNode } from './svelte-parser';
2
2
  import type { MLASTNode, MLASTParentNode, ParserOptions } from '@markuplint/ml-ast';
3
- export declare function nodeize(
4
- originNode: SvelteNode,
5
- prevNode: MLASTNode | null,
6
- parentNode: MLASTParentNode | null,
7
- rawHtml: string,
8
- options?: ParserOptions,
9
- ): MLASTNode | MLASTNode[] | null;
3
+ export declare function nodeize(originNode: SvelteNode, prevNode: MLASTNode | null, parentNode: MLASTParentNode | null, rawHtml: string, options?: ParserOptions): MLASTNode | MLASTNode[] | null;
package/lib/nodeize.js CHANGED
@@ -4,8 +4,15 @@ exports.nodeize = void 0;
4
4
  const html_parser_1 = require("@markuplint/html-parser");
5
5
  const parser_utils_1 = require("@markuplint/parser-utils");
6
6
  const attr_1 = require("./attr");
7
+ const parse_ctrl_block_1 = require("./parse-ctrl-block");
7
8
  const traverse_1 = require("./traverse");
8
- function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
9
+ function nodeize(
10
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
11
+ originNode,
12
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
13
+ prevNode,
14
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
15
+ parentNode, rawHtml, options) {
9
16
  var _a, _b, _c, _d;
10
17
  const nextNode = null;
11
18
  const { startOffset, endOffset, startLine, endLine, startCol, endCol, raw } = (0, parser_utils_1.sliceFragment)(rawHtml, originNode.start, originNode.end);
@@ -125,10 +132,11 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
125
132
  return startTag;
126
133
  }
127
134
  case 'IfBlock': {
128
- return solveCtrlBlock('if', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
135
+ const ifBlocks = (0, parse_ctrl_block_1.parseCtrlBlock)('if', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
136
+ return ifBlocks;
129
137
  }
130
138
  case 'EachBlock': {
131
- return solveCtrlBlock('each', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
139
+ return (0, parse_ctrl_block_1.parseCtrlBlock)('each', originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options);
132
140
  }
133
141
  case 'AwaitBlock': {
134
142
  const pendingNode = originNode.pending;
@@ -248,81 +256,3 @@ function nodeize(originNode, prevNode, parentNode, rawHtml, options) {
248
256
  }
249
257
  }
250
258
  exports.nodeize = nodeize;
251
- function solveCtrlBlock(ctrlName, originNode, raw, rawHtml, startOffset, parentNode, prevNode, nextNode, options) {
252
- var _a, _b, _c, _d;
253
- const children = originNode.children || [];
254
- const reEndTag = new RegExp(`{/${ctrlName}}$`, 'i');
255
- const startTagEndOffset = children.length
256
- ? (_b = (_a = children[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : 0
257
- : raw.replace(reEndTag, '').length + startOffset;
258
- const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, startTagEndOffset);
259
- let endTag = null;
260
- const endTagRawMatched = raw.match(reEndTag);
261
- if (endTagRawMatched) {
262
- const endTagRaw = endTagRawMatched[0];
263
- const endTagStartOffset = originNode.end - endTagRaw.length;
264
- const endTagEndOffset = originNode.end;
265
- const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
266
- endTag = {
267
- uuid: (0, parser_utils_1.uuid)(),
268
- raw: endTagRaw,
269
- startOffset: endTagStartOffset,
270
- endOffset: endTagEndOffset,
271
- startLine: endTagLocation.startLine,
272
- endLine: endTagLocation.endLine,
273
- startCol: endTagLocation.startCol,
274
- endCol: endTagLocation.endCol,
275
- nodeName: originNode.type,
276
- type: 'psblock',
277
- parentNode,
278
- prevNode,
279
- nextNode,
280
- isFragment: false,
281
- isGhost: false,
282
- };
283
- }
284
- let elseTag = null;
285
- if (originNode.else) {
286
- const elseNode = originNode.else;
287
- const elseTagStartOffset = children.length
288
- ? (_d = (_c = children[children.length - 1]) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : 0
289
- : startTagLocation.endOffset;
290
- const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
291
- elseTag = {
292
- uuid: (0, parser_utils_1.uuid)(),
293
- ...elseTagLocation,
294
- nodeName: elseNode.type,
295
- type: 'psblock',
296
- parentNode,
297
- prevNode,
298
- nextNode,
299
- isFragment: false,
300
- isGhost: false,
301
- };
302
- if (elseNode.children) {
303
- elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
304
- }
305
- }
306
- const tag = {
307
- uuid: (0, parser_utils_1.uuid)(),
308
- ...startTagLocation,
309
- nodeName: originNode.type,
310
- type: 'psblock',
311
- parentNode,
312
- prevNode,
313
- nextNode,
314
- isFragment: false,
315
- isGhost: false,
316
- };
317
- if (originNode.children) {
318
- tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
319
- }
320
- const tags = [tag];
321
- if (elseTag) {
322
- tags.push(elseTag);
323
- }
324
- if (endTag) {
325
- tags.push(endTag);
326
- }
327
- return tags;
328
- }
@@ -0,0 +1,3 @@
1
+ import type { SvelteNode } from './svelte-parser';
2
+ import type { MLASTNode, MLASTParentNode, MLASTPreprocessorSpecificBlock, ParserOptions } from '@markuplint/ml-ast';
3
+ export declare function parseCtrlBlock(ctrlName: 'if' | 'each', originNode: SvelteNode, raw: string, rawHtml: string, startOffset: number, parentNode: MLASTParentNode | null, prevNode: MLASTNode | null, nextNode: MLASTNode | null, options?: ParserOptions): MLASTPreprocessorSpecificBlock[];
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseCtrlBlock = void 0;
4
+ const parser_utils_1 = require("@markuplint/parser-utils");
5
+ const traverse_1 = require("./traverse");
6
+ function parseCtrlBlock(ctrlName,
7
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
8
+ originNode, raw, rawHtml, startOffset,
9
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
10
+ parentNode,
11
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
12
+ prevNode,
13
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
14
+ nextNode, options) {
15
+ var _a, _b, _c, _d;
16
+ if (ctrlName === 'if') {
17
+ return parseIfBlock(originNode, raw, rawHtml, startOffset, originNode.start, parentNode, prevNode, nextNode, options);
18
+ }
19
+ const children = originNode.children || [];
20
+ const reEndTag = new RegExp('{/each}$', 'i');
21
+ const startTagEndOffset = children.length
22
+ ? (_b = (_a = children[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : 0
23
+ : raw.replace(reEndTag, '').length + startOffset;
24
+ const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, startOffset, startTagEndOffset);
25
+ const tag = {
26
+ uuid: (0, parser_utils_1.uuid)(),
27
+ ...startTagLocation,
28
+ nodeName: originNode.type,
29
+ type: 'psblock',
30
+ parentNode,
31
+ prevNode,
32
+ nextNode,
33
+ isFragment: false,
34
+ isGhost: false,
35
+ };
36
+ if (originNode.children) {
37
+ tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
38
+ }
39
+ let elseTag = null;
40
+ if (originNode.else) {
41
+ const elseNode = originNode.else;
42
+ const elseTagStartOffset = children.length
43
+ ? (_d = (_c = children[children.length - 1]) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : 0
44
+ : startTagLocation.endOffset;
45
+ const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
46
+ elseTag = {
47
+ uuid: (0, parser_utils_1.uuid)(),
48
+ ...elseTagLocation,
49
+ nodeName: elseNode.type,
50
+ type: 'psblock',
51
+ parentNode,
52
+ prevNode,
53
+ nextNode,
54
+ isFragment: false,
55
+ isGhost: false,
56
+ };
57
+ if (elseNode.children) {
58
+ elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
59
+ }
60
+ }
61
+ let endTag = null;
62
+ const endTagRawMatched = raw.match(reEndTag);
63
+ if (endTagRawMatched) {
64
+ const endTagRaw = endTagRawMatched[0];
65
+ const endTagStartOffset = originNode.end - endTagRaw.length;
66
+ const endTagEndOffset = originNode.end;
67
+ const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
68
+ endTag = {
69
+ uuid: (0, parser_utils_1.uuid)(),
70
+ raw: endTagRaw,
71
+ startOffset: endTagStartOffset,
72
+ endOffset: endTagEndOffset,
73
+ startLine: endTagLocation.startLine,
74
+ endLine: endTagLocation.endLine,
75
+ startCol: endTagLocation.startCol,
76
+ endCol: endTagLocation.endCol,
77
+ nodeName: originNode.type,
78
+ type: 'psblock',
79
+ parentNode,
80
+ prevNode,
81
+ nextNode,
82
+ isFragment: false,
83
+ isGhost: false,
84
+ };
85
+ }
86
+ const tags = [tag];
87
+ if (elseTag) {
88
+ tags.push(elseTag);
89
+ }
90
+ if (endTag) {
91
+ tags.push(endTag);
92
+ }
93
+ return tags;
94
+ }
95
+ exports.parseCtrlBlock = parseCtrlBlock;
96
+ function parseIfBlock(
97
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
98
+ originNode, raw, rawHtml, statementStartOffset, tokenStartOffset,
99
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
100
+ parentNode,
101
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
102
+ prevNode,
103
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
104
+ nextNode, options) {
105
+ var _a, _b, _c, _d, _e, _f, _g, _h;
106
+ const children = originNode.children || [];
107
+ const startTagEndOffset = (_b = (_a = children[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : tokenStartOffset;
108
+ const startTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, tokenStartOffset, startTagEndOffset);
109
+ const tag = {
110
+ uuid: (0, parser_utils_1.uuid)(),
111
+ ...startTagLocation,
112
+ nodeName: originNode.elseif ? 'ElseIfBlock' : originNode.type,
113
+ type: 'psblock',
114
+ parentNode,
115
+ prevNode,
116
+ nextNode,
117
+ isFragment: false,
118
+ isGhost: false,
119
+ };
120
+ if (originNode.children) {
121
+ tag.childNodes = (0, traverse_1.traverse)(originNode.children, tag, rawHtml, options);
122
+ }
123
+ const elseOrElseIfTags = [];
124
+ if (originNode.else) {
125
+ const elseNode = originNode.else;
126
+ const elseTagStartOffset = children.length
127
+ ? (_d = (_c = children[children.length - 1]) === null || _c === void 0 ? void 0 : _c.end) !== null && _d !== void 0 ? _d : 0
128
+ : startTagLocation.endOffset;
129
+ const elseTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, elseTagStartOffset, elseNode.start);
130
+ if (elseNode.children) {
131
+ if (elseNode.children.length === 1 && ((_e = elseNode.children[0]) === null || _e === void 0 ? void 0 : _e.type) === 'IfBlock') {
132
+ const elseIfTags = parseIfBlock(elseNode.children[0], elseTagLocation.raw, rawHtml, statementStartOffset, (_h = (_g = (_f = originNode.children) === null || _f === void 0 ? void 0 : _f[0]) === null || _g === void 0 ? void 0 : _g.end) !== null && _h !== void 0 ? _h : startTagLocation.endOffset, parentNode, null, null, options);
133
+ elseOrElseIfTags.push(...elseIfTags);
134
+ }
135
+ else {
136
+ const elseTag = {
137
+ uuid: (0, parser_utils_1.uuid)(),
138
+ ...elseTagLocation,
139
+ nodeName: elseNode.type,
140
+ type: 'psblock',
141
+ parentNode,
142
+ prevNode,
143
+ nextNode,
144
+ isFragment: false,
145
+ isGhost: false,
146
+ };
147
+ elseTag.childNodes = (0, traverse_1.traverse)(elseNode.children, elseTag, rawHtml, options);
148
+ elseOrElseIfTags.push(elseTag);
149
+ }
150
+ }
151
+ }
152
+ if (originNode.elseif) {
153
+ return [tag, ...elseOrElseIfTags];
154
+ }
155
+ const endTagRawMatched = raw.match('{/if}');
156
+ if (!endTagRawMatched) {
157
+ throw new Error('Missing the end token `{/if}`');
158
+ }
159
+ const endTagRaw = endTagRawMatched[0];
160
+ const endTagStartOffset = originNode.end - endTagRaw.length;
161
+ const endTagEndOffset = originNode.end;
162
+ const endTagLocation = (0, parser_utils_1.sliceFragment)(rawHtml, endTagStartOffset, endTagEndOffset);
163
+ const endTag = {
164
+ uuid: (0, parser_utils_1.uuid)(),
165
+ raw: endTagRaw,
166
+ startOffset: endTagStartOffset,
167
+ endOffset: endTagEndOffset,
168
+ startLine: endTagLocation.startLine,
169
+ endLine: endTagLocation.endLine,
170
+ startCol: endTagLocation.startCol,
171
+ endCol: endTagLocation.endCol,
172
+ nodeName: originNode.type,
173
+ type: 'psblock',
174
+ parentNode,
175
+ prevNode,
176
+ nextNode,
177
+ isFragment: false,
178
+ isGhost: false,
179
+ };
180
+ return [tag, ...elseOrElseIfTags, endTag];
181
+ }
package/lib/parse.js CHANGED
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parse = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const html_parser_1 = require("@markuplint/html-parser");
6
5
  const parser_utils_1 = require("@markuplint/parser-utils");
7
6
  const svelte_parser_1 = tslib_1.__importDefault(require("./svelte-parser"));
8
7
  const traverse_1 = require("./traverse");
@@ -43,7 +42,8 @@ const parse = (rawCode, options) => {
43
42
  parseError: err instanceof Error ? err.message : new Error(`${err}`).message,
44
43
  };
45
44
  }
46
- const nodeList = (0, parser_utils_1.restoreNode)((0, html_parser_1.flattenNodes)((0, traverse_1.traverse)(ast, null, blocks.replaced, options), blocks.replaced), blocks);
45
+ const nodes = (0, traverse_1.traverse)(ast, null, blocks.replaced, options);
46
+ const nodeList = (0, parser_utils_1.restoreNode)((0, parser_utils_1.flattenNodes)(nodes, blocks.replaced), blocks);
47
47
  return {
48
48
  nodeList,
49
49
  isFragment: true,
package/lib/traverse.d.ts CHANGED
@@ -1,8 +1,3 @@
1
1
  import type { SvelteNode } from './svelte-parser';
2
2
  import type { MLASTNode, MLASTParentNode, ParserOptions } from '@markuplint/ml-ast';
3
- export declare function traverse(
4
- astNodes: SvelteNode[],
5
- parentNode: MLASTParentNode | null | undefined,
6
- rawHtml: string,
7
- options?: ParserOptions,
8
- ): MLASTNode[];
3
+ export declare function traverse(astNodes: readonly SvelteNode[], parentNode: MLASTParentNode | null | undefined, rawHtml: string, options?: ParserOptions): MLASTNode[];
package/lib/traverse.js CHANGED
@@ -2,7 +2,9 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.traverse = void 0;
4
4
  const nodeize_1 = require("./nodeize");
5
- function traverse(astNodes, parentNode = null, rawHtml, options) {
5
+ function traverse(
6
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
7
+ astNodes, parentNode = null, rawHtml, options) {
6
8
  const nodeList = [];
7
9
  let prevNode = null;
8
10
  for (const astNode of astNodes) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/svelte-parser",
3
- "version": "3.4.0",
3
+ "version": "3.5.0",
4
4
  "description": "Svelte parser for markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -16,11 +16,11 @@
16
16
  "clean": "tsc --build --clean"
17
17
  },
18
18
  "dependencies": {
19
- "@markuplint/html-parser": "3.4.0",
20
- "@markuplint/ml-ast": "3.0.0",
21
- "@markuplint/parser-utils": "3.4.0",
22
- "svelte": "^3.55.0",
19
+ "@markuplint/html-parser": "3.5.0",
20
+ "@markuplint/ml-ast": "3.1.0",
21
+ "@markuplint/parser-utils": "3.5.0",
22
+ "svelte": "^3.57.0",
23
23
  "tslib": "^2.4.1"
24
24
  },
25
- "gitHead": "a83e0f5f214a9bbcc0286b9e269074ddca6189e7"
25
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
26
26
  }