@markuplint/pug-parser 3.4.0 → 3.6.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-tokenizer.js +7 -4
- package/lib/parse.js +17 -7
- package/lib/pug-parser/index.js +43 -20
- package/package.json +5 -5
package/lib/attr-tokenizer.js
CHANGED
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const parser_utils_1 = require("@markuplint/parser-utils");
|
|
4
|
-
function attrTokenizer(
|
|
4
|
+
function attrTokenizer(
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
6
|
+
attr) {
|
|
7
|
+
var _a, _b, _c;
|
|
5
8
|
if (attr.raw[0] === '#' || attr.raw[0] === '.') {
|
|
6
9
|
let value = '';
|
|
7
10
|
if (typeof attr.val === 'string') {
|
|
8
|
-
[, , value] = attr.val.match(/(['"`]?)([^\1]+)(\1)/)
|
|
11
|
+
[, , value] = (_a = attr.val.match(/(['"`]?)([^\1]+)(\1)/)) !== null && _a !== void 0 ? _a : ['', '', ''];
|
|
9
12
|
}
|
|
10
13
|
else {
|
|
11
14
|
value = `${attr.val}`;
|
|
@@ -51,8 +54,8 @@ function attrTokenizer(attr) {
|
|
|
51
54
|
const withoutName = attr.raw.slice(attr.name.length);
|
|
52
55
|
const valueOffset = withoutName.indexOf(attr.val);
|
|
53
56
|
const equalAndBeforeSpaceAfterSpace = withoutName.slice(0, valueOffset);
|
|
54
|
-
const [, before, equal, after] = equalAndBeforeSpaceAfterSpace.match(/^(\s*)(=)(\s*)$/)
|
|
55
|
-
const [, quote, coreValue] = attr.val.match(/(['"`]?)([^\1]+)(\1)/)
|
|
57
|
+
const [, before, equal, after] = (_b = equalAndBeforeSpaceAfterSpace.match(/^(\s*)(=)(\s*)$/)) !== null && _b !== void 0 ? _b : ['', '', '', ''];
|
|
58
|
+
const [, quote, coreValue] = (_c = attr.val.match(/(['"`]?)([^\1]+)(\1)/)) !== null && _c !== void 0 ? _c : ['', '', ''];
|
|
56
59
|
spacesBeforeEqualChars = before !== null && before !== void 0 ? before : '';
|
|
57
60
|
equalChars = equal !== null && equal !== void 0 ? equal : '';
|
|
58
61
|
spacesAfterEqualChars = after !== null && after !== void 0 ? after : '';
|
package/lib/parse.js
CHANGED
|
@@ -46,19 +46,27 @@ class Parser {
|
|
|
46
46
|
tslib_1.__classPrivateFieldSet(this, _Parser_ast, (0, pug_parser_1.pugParse)(raw), "f");
|
|
47
47
|
// console.log(JSON.stringify(this.#ast, null, 2));
|
|
48
48
|
}
|
|
49
|
-
flattenNodes(
|
|
49
|
+
flattenNodes(
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
51
|
+
nodeTree) {
|
|
50
52
|
const nodeOrders = [];
|
|
51
53
|
(0, parser_utils_1.walk)(nodeTree, node => {
|
|
52
54
|
nodeOrders.push(node);
|
|
53
55
|
});
|
|
54
|
-
(0,
|
|
56
|
+
(0, parser_utils_1.removeDeprecatedNode)(nodeOrders);
|
|
55
57
|
return nodeOrders;
|
|
56
58
|
}
|
|
57
59
|
getNodeList() {
|
|
58
60
|
const nodeTree = this.traverse(tslib_1.__classPrivateFieldGet(this, _Parser_ast, "f").nodes, null);
|
|
59
61
|
return this.flattenNodes(nodeTree);
|
|
60
62
|
}
|
|
61
|
-
nodeize(
|
|
63
|
+
nodeize(
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
65
|
+
originNode,
|
|
66
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
67
|
+
prevNode,
|
|
68
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
69
|
+
parentNode) {
|
|
62
70
|
var _a;
|
|
63
71
|
const nextNode = null;
|
|
64
72
|
const startOffset = originNode.offset;
|
|
@@ -73,7 +81,7 @@ class Parser {
|
|
|
73
81
|
return {
|
|
74
82
|
uuid: (0, parser_utils_1.uuid)(),
|
|
75
83
|
raw: originNode.raw,
|
|
76
|
-
name: originNode.val
|
|
84
|
+
name: (_a = originNode.val) !== null && _a !== void 0 ? _a : '',
|
|
77
85
|
// TODO:
|
|
78
86
|
publicId: '',
|
|
79
87
|
// TODO:
|
|
@@ -174,7 +182,7 @@ class Parser {
|
|
|
174
182
|
tagOpenChar: '',
|
|
175
183
|
tagCloseChar: '',
|
|
176
184
|
};
|
|
177
|
-
if (originNode.block.nodes.length) {
|
|
185
|
+
if (originNode.block.nodes.length > 0) {
|
|
178
186
|
tag.childNodes = this.traverse(originNode.block.nodes, tag);
|
|
179
187
|
}
|
|
180
188
|
return tag;
|
|
@@ -197,14 +205,16 @@ class Parser {
|
|
|
197
205
|
isFragment: true,
|
|
198
206
|
isGhost: false,
|
|
199
207
|
};
|
|
200
|
-
if ('block' in originNode &&
|
|
208
|
+
if ('block' in originNode && originNode.block && originNode.block.nodes.length > 0) {
|
|
201
209
|
tag.childNodes = this.traverse(originNode.block.nodes, tag);
|
|
202
210
|
}
|
|
203
211
|
return tag;
|
|
204
212
|
}
|
|
205
213
|
}
|
|
206
214
|
}
|
|
207
|
-
traverse(
|
|
215
|
+
traverse(
|
|
216
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
217
|
+
astNodes, parentNode = null) {
|
|
208
218
|
const nodeList = [];
|
|
209
219
|
let prevNode = null;
|
|
210
220
|
for (const astNode of astNodes) {
|
package/lib/pug-parser/index.js
CHANGED
|
@@ -26,10 +26,13 @@ function getOffsetsFromLines(pug) {
|
|
|
26
26
|
});
|
|
27
27
|
return result;
|
|
28
28
|
}
|
|
29
|
-
function mergeTextNode(
|
|
29
|
+
function mergeTextNode(
|
|
30
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
31
|
+
nodes, pug) {
|
|
32
|
+
var _a;
|
|
30
33
|
const baseNodes = [];
|
|
31
34
|
for (const node of nodes) {
|
|
32
|
-
const prevNode = baseNodes[baseNodes.length - 1]
|
|
35
|
+
const prevNode = (_a = baseNodes[baseNodes.length - 1]) !== null && _a !== void 0 ? _a : null;
|
|
33
36
|
if (prevNode && prevNode.type === 'Text' && node.type === 'Text') {
|
|
34
37
|
prevNode.raw = pug.slice(prevNode.offset, node.endOffset);
|
|
35
38
|
prevNode.endColumn = node.endColumn;
|
|
@@ -47,14 +50,18 @@ function mergeTextNode(nodes, pug) {
|
|
|
47
50
|
* @param tokens Lexed token list
|
|
48
51
|
* @param pug Raw code
|
|
49
52
|
*/
|
|
50
|
-
function optimizeAST(
|
|
53
|
+
function optimizeAST(
|
|
54
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
55
|
+
originalAST,
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
57
|
+
tokens, pug) {
|
|
51
58
|
var _a;
|
|
52
59
|
const nodes = [];
|
|
53
60
|
for (const node of originalAST.nodes) {
|
|
54
61
|
const line = node.line;
|
|
55
62
|
const column = node.column;
|
|
56
63
|
const offsets = getOffsetsFromLines(pug);
|
|
57
|
-
const lineOffset = Math.max((_a = offsets[line - 2]) !== null && _a !== void 0 ? _a : 0, 0)
|
|
64
|
+
const lineOffset = Math.max((_a = offsets[line - 2]) !== null && _a !== void 0 ? _a : 0, 0);
|
|
58
65
|
const offset = lineOffset + column - 1;
|
|
59
66
|
const { endLine, endColumn, endOffset } = getLocationFromToken(offset, line, column, tokens);
|
|
60
67
|
const raw = pug.slice(offset, endOffset);
|
|
@@ -304,7 +311,11 @@ function optimizeAST(originalAST, tokens, pug) {
|
|
|
304
311
|
line: originalAST.line,
|
|
305
312
|
};
|
|
306
313
|
}
|
|
307
|
-
function optimizeASTOfConditionalNode(
|
|
314
|
+
function optimizeASTOfConditionalNode(
|
|
315
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
316
|
+
node,
|
|
317
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
318
|
+
tokens, pug, offsets, depth) {
|
|
308
319
|
var _a, _b;
|
|
309
320
|
const altNodes = [];
|
|
310
321
|
let tokenOfCurrentNode = null;
|
|
@@ -316,7 +327,7 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
|
|
|
316
327
|
}
|
|
317
328
|
if (tokenOfCurrentNode) {
|
|
318
329
|
// console.log(JSON.stringify(node, null, 2));
|
|
319
|
-
const lineOffset = Math.max((_a = offsets[node.line - 2]) !== null && _a !== void 0 ? _a : 0, 0)
|
|
330
|
+
const lineOffset = Math.max((_a = offsets[node.line - 2]) !== null && _a !== void 0 ? _a : 0, 0);
|
|
320
331
|
const offset = lineOffset + node.column - 1;
|
|
321
332
|
const length = tokenOfCurrentNode.loc.end.column - tokenOfCurrentNode.loc.start.column;
|
|
322
333
|
const endOffset = offset + length;
|
|
@@ -348,7 +359,7 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
|
|
|
348
359
|
if (!tokenOfCurrentNode) {
|
|
349
360
|
return [];
|
|
350
361
|
}
|
|
351
|
-
const lineOffset = Math.max((_b = offsets[tokenOfCurrentNode.loc.start.line - 2]) !== null && _b !== void 0 ? _b : 0, 0)
|
|
362
|
+
const lineOffset = Math.max((_b = offsets[tokenOfCurrentNode.loc.start.line - 2]) !== null && _b !== void 0 ? _b : 0, 0);
|
|
352
363
|
const offset = lineOffset + tokenOfCurrentNode.loc.start.column - 1;
|
|
353
364
|
const length = tokenOfCurrentNode.loc.end.column - tokenOfCurrentNode.loc.start.column;
|
|
354
365
|
const endOffset = offset + length;
|
|
@@ -376,14 +387,13 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
|
|
|
376
387
|
}
|
|
377
388
|
return altNodes;
|
|
378
389
|
}
|
|
379
|
-
function getLocationFromToken(offset, line, column,
|
|
390
|
+
function getLocationFromToken(offset, line, column,
|
|
391
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
392
|
+
tokens, tokenType) {
|
|
393
|
+
const tokenTypes = typeof tokenType === 'string' ? (tokenType !== '' ? [tokenType] : null) : tokenType !== null && tokenType !== void 0 ? tokenType : null;
|
|
380
394
|
let tokenOfCurrentNode = null;
|
|
381
395
|
for (const token of tokens) {
|
|
382
|
-
if ((
|
|
383
|
-
? Array.isArray(tokenType)
|
|
384
|
-
? tokenType.includes(token.type)
|
|
385
|
-
: tokenType === token.type
|
|
386
|
-
: true) &&
|
|
396
|
+
if ((tokenTypes == null || tokenTypes.includes(token.type)) &&
|
|
387
397
|
token.loc.start.line === line &&
|
|
388
398
|
token.loc.start.column === column) {
|
|
389
399
|
tokenOfCurrentNode = token;
|
|
@@ -401,10 +411,15 @@ function getLocationFromToken(offset, line, column, tokens, tokenType = '') {
|
|
|
401
411
|
length,
|
|
402
412
|
};
|
|
403
413
|
}
|
|
404
|
-
function getAttrs(
|
|
414
|
+
function getAttrs(
|
|
415
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
416
|
+
originalAttrs,
|
|
417
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
418
|
+
tokens, offsets, pug) {
|
|
419
|
+
var _a;
|
|
405
420
|
const attrs = [];
|
|
406
421
|
for (const attr of originalAttrs) {
|
|
407
|
-
const attrLineOffset = offsets[attr.line - 2]
|
|
422
|
+
const attrLineOffset = (_a = offsets[attr.line - 2]) !== null && _a !== void 0 ? _a : 0;
|
|
408
423
|
const attrOffset = attrLineOffset + attr.column - 1;
|
|
409
424
|
let tokenOfCurrentAttr = null;
|
|
410
425
|
for (const token of tokens) {
|
|
@@ -434,7 +449,9 @@ function getAttrs(originalAttrs, tokens, offsets, pug) {
|
|
|
434
449
|
}
|
|
435
450
|
return attrs;
|
|
436
451
|
}
|
|
437
|
-
function getEndAttributeLocation(nodeName, offset, line, column,
|
|
452
|
+
function getEndAttributeLocation(nodeName, offset, line, column,
|
|
453
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
454
|
+
tokens, offsets) {
|
|
438
455
|
var _a;
|
|
439
456
|
let beforeNewlineToken = null;
|
|
440
457
|
for (const token of tokens) {
|
|
@@ -446,7 +463,7 @@ function getEndAttributeLocation(nodeName, offset, line, column, tokens, offsets
|
|
|
446
463
|
token.type !== 'end-attributes' &&
|
|
447
464
|
token.type !== 'id' &&
|
|
448
465
|
token.type !== 'class') {
|
|
449
|
-
const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0)
|
|
466
|
+
const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0);
|
|
450
467
|
const endAttrOffset = endAttrLineOffset + beforeNewlineToken.loc.end.column - 1;
|
|
451
468
|
return {
|
|
452
469
|
endOffset: endAttrOffset,
|
|
@@ -462,7 +479,11 @@ function getEndAttributeLocation(nodeName, offset, line, column, tokens, offsets
|
|
|
462
479
|
endColumn: column + nodeName.length,
|
|
463
480
|
};
|
|
464
481
|
}
|
|
465
|
-
function getPipelessText(
|
|
482
|
+
function getPipelessText(
|
|
483
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
484
|
+
node, pug,
|
|
485
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
486
|
+
tokens) {
|
|
466
487
|
let startPipelessText = null;
|
|
467
488
|
let endPipelessText = null;
|
|
468
489
|
for (const token of tokens) {
|
|
@@ -494,7 +515,9 @@ function getPipelessText(node, pug, tokens) {
|
|
|
494
515
|
}
|
|
495
516
|
return null;
|
|
496
517
|
}
|
|
497
|
-
function getRawTextAndLocationEnd(val, offset, line, column,
|
|
518
|
+
function getRawTextAndLocationEnd(val, offset, line, column,
|
|
519
|
+
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
520
|
+
tokens, offsets, pug) {
|
|
498
521
|
var _a;
|
|
499
522
|
let beforeNewlineToken = null;
|
|
500
523
|
for (const token of tokens) {
|
|
@@ -505,7 +528,7 @@ function getRawTextAndLocationEnd(val, offset, line, column, tokens, offsets, pu
|
|
|
505
528
|
token.type !== 'text-html' &&
|
|
506
529
|
token.type !== 'indent' &&
|
|
507
530
|
token.type !== 'outdent') {
|
|
508
|
-
const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0)
|
|
531
|
+
const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0);
|
|
509
532
|
const endAttrOffset = endAttrLineOffset + beforeNewlineToken.loc.end.column - 1;
|
|
510
533
|
return {
|
|
511
534
|
endOffset: endAttrOffset,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/pug-parser",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.6.0",
|
|
4
4
|
"description": "Pug parser for markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
"clean": "tsc --build --clean"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"@markuplint/html-parser": "3.
|
|
20
|
-
"@markuplint/ml-ast": "3.
|
|
21
|
-
"@markuplint/parser-utils": "3.
|
|
19
|
+
"@markuplint/html-parser": "3.6.0",
|
|
20
|
+
"@markuplint/ml-ast": "3.1.0",
|
|
21
|
+
"@markuplint/parser-utils": "3.6.0",
|
|
22
22
|
"pug-lexer": "^5.0.1",
|
|
23
23
|
"pug-parser": "^6.0.0",
|
|
24
24
|
"tslib": "^2.4.1"
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "715dd53d3b1064a9bcf616c1533921cad9e3b187"
|
|
27
27
|
}
|