@markuplint/pug-parser 3.3.1 → 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.
@@ -1,7 +1,9 @@
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(attr) {
4
+ function attrTokenizer(
5
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6
+ attr) {
5
7
  if (attr.raw[0] === '#' || attr.raw[0] === '.') {
6
8
  let value = '';
7
9
  if (typeof attr.val === 'string') {
@@ -21,7 +23,7 @@ function attrTokenizer(attr) {
21
23
  startCol: attr.column,
22
24
  endCol: attr.endColumn,
23
25
  potentialName: attr.name,
24
- potentialValue: value,
26
+ potentialValue: value !== null && value !== void 0 ? value : '',
25
27
  valueType: 'string',
26
28
  isDuplicatable: attr.raw[0] === '.',
27
29
  nodeName: '#pug-special-attr',
@@ -53,11 +55,11 @@ function attrTokenizer(attr) {
53
55
  const equalAndBeforeSpaceAfterSpace = withoutName.slice(0, valueOffset);
54
56
  const [, before, equal, after] = equalAndBeforeSpaceAfterSpace.match(/^(\s*)(=)(\s*)$/) || ['', '', '', ''];
55
57
  const [, quote, coreValue] = attr.val.match(/(['"`]?)([^\1]+)(\1)/) || ['', '', ''];
56
- spacesBeforeEqualChars = before;
57
- equalChars = equal;
58
- spacesAfterEqualChars = after;
59
- quoteChars = quote;
60
- valueChars = coreValue;
58
+ spacesBeforeEqualChars = before !== null && before !== void 0 ? before : '';
59
+ equalChars = equal !== null && equal !== void 0 ? equal : '';
60
+ spacesAfterEqualChars = after !== null && after !== void 0 ? after : '';
61
+ quoteChars = quote !== null && quote !== void 0 ? quote : '';
62
+ valueChars = coreValue !== null && coreValue !== void 0 ? coreValue : '';
61
63
  if (quote === '`' || quote === '') {
62
64
  isDynamicValue = true;
63
65
  }
package/lib/index.d.ts CHANGED
@@ -1,2 +1,2 @@
1
1
  export { parse } from './parse';
2
- export declare const endTag = 'never';
2
+ export declare const endTag = "never";
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(nodeTree) {
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, html_parser_1.removeDeprecatedNode)(nodeOrders);
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(originNode, prevNode, parentNode) {
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;
@@ -204,7 +212,9 @@ class Parser {
204
212
  }
205
213
  }
206
214
  }
207
- traverse(astNodes, parentNode = null) {
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) {
@@ -214,7 +224,11 @@ class Parser {
214
224
  }
215
225
  let node;
216
226
  if (Array.isArray(nodes)) {
217
- node = nodes[nodes.length - 1];
227
+ const lastNode = nodes[nodes.length - 1];
228
+ if (!lastNode) {
229
+ continue;
230
+ }
231
+ node = lastNode;
218
232
  }
219
233
  else {
220
234
  node = nodes;
@@ -1,21 +1,7 @@
1
1
  export declare function pugParse(pug: string): ASTBlock;
2
2
  export type ASTBlock = PugAST<ASTNode>;
3
- export type ASTNode =
4
- | ASTTagNode
5
- | ASTTextNode
6
- | ASTCodeNode
7
- | ASTComment
8
- | ASTDoctype
9
- | ASTIncludeNode
10
- | ASTMixinNode
11
- | ASTMixinSlotNode
12
- | ASTFilterNode
13
- | ASTEachNode
14
- | ASTConditionalNode
15
- | ASTCaseNode
16
- | ASTCaseWhenNode;
17
- export type ASTTagNode = Omit<PugASTTagNode<ASTAttr, ASTBlock>, 'attributeBlocks' | 'selfClosing' | 'isInline'> &
18
- AdditionalASTData;
3
+ export type ASTNode = ASTTagNode | ASTTextNode | ASTCodeNode | ASTComment | ASTDoctype | ASTIncludeNode | ASTMixinNode | ASTMixinSlotNode | ASTFilterNode | ASTEachNode | ASTConditionalNode | ASTCaseNode | ASTCaseWhenNode;
4
+ export type ASTTagNode = Omit<PugASTTagNode<ASTAttr, ASTBlock>, 'attributeBlocks' | 'selfClosing' | 'isInline'> & AdditionalASTData;
19
5
  export type ASTTextNode = Omit<PugASTTextNode, 'val'> & AdditionalASTData;
20
6
  export type ASTCodeNode = PugASTCodeNode & AdditionalASTData;
21
7
  export type ASTComment = PugASTCommentNode & AdditionalASTData;
@@ -26,135 +12,135 @@ export type ASTMixinSlotNode = PugASTMixinSlotNode & AdditionalASTData;
26
12
  export type ASTFilterNode = PugASTFilterNode<ASTAttr, ASTBlock> & AdditionalASTData;
27
13
  export type ASTEachNode = PugASTEachNode<ASTBlock> & AdditionalASTData;
28
14
  export type ASTConditionalNode = Omit<PugASTConditionalNode<ASTBlock>, 'consequent' | 'alternate'> & {
29
- block: ASTBlock;
15
+ block: ASTBlock;
30
16
  } & AdditionalASTData;
31
17
  export type ASTCaseNode = PugASTCaseNode<ASTBlock> & AdditionalASTData;
32
18
  export type ASTCaseWhenNode = PugASTCaseWhenNode<ASTBlock> & AdditionalASTData;
33
19
  export type ASTAttr = PugASTAttr & AdditionalASTData;
34
20
  type AdditionalASTData = {
35
- raw: string;
36
- offset: number;
37
- endOffset: number;
38
- endLine: number;
39
- endColumn: number;
21
+ raw: string;
22
+ offset: number;
23
+ endOffset: number;
24
+ endLine: number;
25
+ endColumn: number;
40
26
  };
41
27
  interface PugAST<N> {
42
- type: 'Block';
43
- nodes: N[];
44
- line: number;
28
+ type: 'Block';
29
+ nodes: N[];
30
+ line: number;
45
31
  }
46
32
  type PugASTTagNode<A, B> = {
47
- type: 'Tag';
48
- name: string;
49
- selfClosing: boolean;
50
- attrs: A[];
51
- attributeBlocks: never[];
52
- isInline: boolean;
53
- line: number;
54
- column: number;
55
- block: B;
33
+ type: 'Tag';
34
+ name: string;
35
+ selfClosing: boolean;
36
+ attrs: A[];
37
+ attributeBlocks: never[];
38
+ isInline: boolean;
39
+ line: number;
40
+ column: number;
41
+ block: B;
56
42
  };
57
43
  type PugASTTextNode = {
58
- type: 'Text';
59
- val: string;
60
- isHtml?: true;
61
- line: number;
62
- column: number;
44
+ type: 'Text';
45
+ val: string;
46
+ isHtml?: true;
47
+ line: number;
48
+ column: number;
63
49
  };
64
50
  type PugASTCodeNode = {
65
- type: 'Code';
66
- val: string;
67
- buffer: boolean;
68
- mustEscape: boolean;
69
- isInline: boolean;
70
- line: number;
71
- column: number;
51
+ type: 'Code';
52
+ val: string;
53
+ buffer: boolean;
54
+ mustEscape: boolean;
55
+ isInline: boolean;
56
+ line: number;
57
+ column: number;
72
58
  };
73
59
  type PugASTCommentNode = {
74
- type: 'Comment';
75
- val: string;
76
- buffer: boolean;
77
- line: number;
78
- column: number;
60
+ type: 'Comment';
61
+ val: string;
62
+ buffer: boolean;
63
+ line: number;
64
+ column: number;
79
65
  };
80
66
  type PugASTDoctypeNode = {
81
- type: 'Doctype';
82
- val: string;
83
- line: number;
84
- column: number;
67
+ type: 'Doctype';
68
+ val: string;
69
+ line: number;
70
+ column: number;
85
71
  };
86
72
  type PugASTIncludeNode<B> = {
87
- type: 'Include';
88
- file: {
89
- type: 'FileReference';
90
- path: string;
91
- line: number;
92
- column: number;
93
- };
94
- block: B;
95
- line: number;
96
- column: number;
73
+ type: 'Include';
74
+ file: {
75
+ type: 'FileReference';
76
+ path: string;
77
+ line: number;
78
+ column: number;
79
+ };
80
+ block: B;
81
+ line: number;
82
+ column: number;
97
83
  };
98
84
  type PugASTEachNode<B> = {
99
- type: 'Each';
100
- obj: string;
101
- val: string;
102
- key: string | null;
103
- block: B;
104
- line: number;
105
- column: number;
85
+ type: 'Each';
86
+ obj: string;
87
+ val: string;
88
+ key: string | null;
89
+ block: B;
90
+ line: number;
91
+ column: number;
106
92
  };
107
93
  type PugASTMixinNode<A, B> = {
108
- type: 'Mixin';
109
- name: string;
110
- args: string;
111
- call: boolean;
112
- block: B | null;
113
- attrs?: A[];
114
- attributeBlocks: never[];
115
- line: number;
116
- column: number;
94
+ type: 'Mixin';
95
+ name: string;
96
+ args: string;
97
+ call: boolean;
98
+ block: B | null;
99
+ attrs?: A[];
100
+ attributeBlocks: never[];
101
+ line: number;
102
+ column: number;
117
103
  };
118
104
  type PugASTMixinSlotNode = {
119
- type: 'MixinBlock';
120
- line: number;
121
- column: number;
105
+ type: 'MixinBlock';
106
+ line: number;
107
+ column: number;
122
108
  };
123
109
  type PugASTFilterNode<A, B> = {
124
- type: 'Filter';
125
- name: string;
126
- block: B;
127
- attrs: A[];
128
- line: number;
129
- column: number;
110
+ type: 'Filter';
111
+ name: string;
112
+ block: B;
113
+ attrs: A[];
114
+ line: number;
115
+ column: number;
130
116
  };
131
117
  type PugASTConditionalNode<B> = {
132
- type: 'Conditional';
133
- test: string;
134
- consequent: B;
135
- alternate?: B | PugASTConditionalNode<B>;
136
- line: number;
137
- column: number;
118
+ type: 'Conditional';
119
+ test: string;
120
+ consequent: B;
121
+ alternate?: B | PugASTConditionalNode<B>;
122
+ line: number;
123
+ column: number;
138
124
  };
139
125
  type PugASTCaseNode<B> = {
140
- type: 'Case';
141
- expr: string;
142
- block: B;
143
- line: number;
144
- column: number;
126
+ type: 'Case';
127
+ expr: string;
128
+ block: B;
129
+ line: number;
130
+ column: number;
145
131
  };
146
132
  type PugASTCaseWhenNode<B> = {
147
- type: 'When';
148
- expr: string;
149
- block: B;
150
- line: number;
151
- column: number;
133
+ type: 'When';
134
+ expr: string;
135
+ block: B;
136
+ line: number;
137
+ column: number;
152
138
  };
153
139
  type PugASTAttr = {
154
- name: string;
155
- val: string | true;
156
- mustEscape: boolean;
157
- line: number;
158
- column: number;
140
+ name: string;
141
+ val: string | true;
142
+ mustEscape: boolean;
143
+ line: number;
144
+ column: number;
159
145
  };
160
146
  export {};
@@ -26,7 +26,9 @@ function getOffsetsFromLines(pug) {
26
26
  });
27
27
  return result;
28
28
  }
29
- function mergeTextNode(nodes, pug) {
29
+ function mergeTextNode(
30
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
31
+ nodes, pug) {
30
32
  const baseNodes = [];
31
33
  for (const node of nodes) {
32
34
  const prevNode = baseNodes[baseNodes.length - 1] || null;
@@ -47,13 +49,18 @@ function mergeTextNode(nodes, pug) {
47
49
  * @param tokens Lexed token list
48
50
  * @param pug Raw code
49
51
  */
50
- function optimizeAST(originalAST, tokens, pug) {
52
+ function optimizeAST(
53
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
54
+ originalAST,
55
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
56
+ tokens, pug) {
57
+ var _a;
51
58
  const nodes = [];
52
59
  for (const node of originalAST.nodes) {
53
60
  const line = node.line;
54
61
  const column = node.column;
55
62
  const offsets = getOffsetsFromLines(pug);
56
- const lineOffset = Math.max(offsets[line - 2], 0) || 0;
63
+ const lineOffset = Math.max((_a = offsets[line - 2]) !== null && _a !== void 0 ? _a : 0, 0) || 0;
57
64
  const offset = lineOffset + column - 1;
58
65
  const { endLine, endColumn, endOffset } = getLocationFromToken(offset, line, column, tokens);
59
66
  const raw = pug.slice(offset, endOffset);
@@ -303,7 +310,12 @@ function optimizeAST(originalAST, tokens, pug) {
303
310
  line: originalAST.line,
304
311
  };
305
312
  }
306
- function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
313
+ function optimizeASTOfConditionalNode(
314
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
315
+ node,
316
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
317
+ tokens, pug, offsets, depth) {
318
+ var _a, _b;
307
319
  const altNodes = [];
308
320
  let tokenOfCurrentNode = null;
309
321
  for (const token of tokens) {
@@ -314,7 +326,7 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
314
326
  }
315
327
  if (tokenOfCurrentNode) {
316
328
  // console.log(JSON.stringify(node, null, 2));
317
- const lineOffset = Math.max(offsets[node.line - 2], 0) || 0;
329
+ const lineOffset = Math.max((_a = offsets[node.line - 2]) !== null && _a !== void 0 ? _a : 0, 0) || 0;
318
330
  const offset = lineOffset + node.column - 1;
319
331
  const length = tokenOfCurrentNode.loc.end.column - tokenOfCurrentNode.loc.start.column;
320
332
  const endOffset = offset + length;
@@ -346,7 +358,7 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
346
358
  if (!tokenOfCurrentNode) {
347
359
  return [];
348
360
  }
349
- const lineOffset = Math.max(offsets[tokenOfCurrentNode.loc.start.line - 2], 0) || 0;
361
+ const lineOffset = Math.max((_b = offsets[tokenOfCurrentNode.loc.start.line - 2]) !== null && _b !== void 0 ? _b : 0, 0) || 0;
350
362
  const offset = lineOffset + tokenOfCurrentNode.loc.start.column - 1;
351
363
  const length = tokenOfCurrentNode.loc.end.column - tokenOfCurrentNode.loc.start.column;
352
364
  const endOffset = offset + length;
@@ -374,7 +386,9 @@ function optimizeASTOfConditionalNode(node, tokens, pug, offsets, depth) {
374
386
  }
375
387
  return altNodes;
376
388
  }
377
- function getLocationFromToken(offset, line, column, tokens, tokenType = '') {
389
+ function getLocationFromToken(offset, line, column,
390
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
391
+ tokens, tokenType = '') {
378
392
  let tokenOfCurrentNode = null;
379
393
  for (const token of tokens) {
380
394
  if ((tokenType
@@ -399,7 +413,11 @@ function getLocationFromToken(offset, line, column, tokens, tokenType = '') {
399
413
  length,
400
414
  };
401
415
  }
402
- function getAttrs(originalAttrs, tokens, offsets, pug) {
416
+ function getAttrs(
417
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
418
+ originalAttrs,
419
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
420
+ tokens, offsets, pug) {
403
421
  const attrs = [];
404
422
  for (const attr of originalAttrs) {
405
423
  const attrLineOffset = offsets[attr.line - 2] || 0;
@@ -432,7 +450,10 @@ function getAttrs(originalAttrs, tokens, offsets, pug) {
432
450
  }
433
451
  return attrs;
434
452
  }
435
- function getEndAttributeLocation(nodeName, offset, line, column, tokens, offsets) {
453
+ function getEndAttributeLocation(nodeName, offset, line, column,
454
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
455
+ tokens, offsets) {
456
+ var _a;
436
457
  let beforeNewlineToken = null;
437
458
  for (const token of tokens) {
438
459
  // Searching token after the tag node.
@@ -443,7 +464,7 @@ function getEndAttributeLocation(nodeName, offset, line, column, tokens, offsets
443
464
  token.type !== 'end-attributes' &&
444
465
  token.type !== 'id' &&
445
466
  token.type !== 'class') {
446
- const endAttrLineOffset = Math.max(offsets[beforeNewlineToken.loc.end.line - 2], 0) || 0;
467
+ const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0) || 0;
447
468
  const endAttrOffset = endAttrLineOffset + beforeNewlineToken.loc.end.column - 1;
448
469
  return {
449
470
  endOffset: endAttrOffset,
@@ -459,7 +480,11 @@ function getEndAttributeLocation(nodeName, offset, line, column, tokens, offsets
459
480
  endColumn: column + nodeName.length,
460
481
  };
461
482
  }
462
- function getPipelessText(node, pug, tokens) {
483
+ function getPipelessText(
484
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
485
+ node, pug,
486
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
487
+ tokens) {
463
488
  let startPipelessText = null;
464
489
  let endPipelessText = null;
465
490
  for (const token of tokens) {
@@ -491,7 +516,10 @@ function getPipelessText(node, pug, tokens) {
491
516
  }
492
517
  return null;
493
518
  }
494
- function getRawTextAndLocationEnd(val, offset, line, column, tokens, offsets, pug) {
519
+ function getRawTextAndLocationEnd(val, offset, line, column,
520
+ // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
521
+ tokens, offsets, pug) {
522
+ var _a;
495
523
  let beforeNewlineToken = null;
496
524
  for (const token of tokens) {
497
525
  // Searching token after the text.
@@ -501,7 +529,7 @@ function getRawTextAndLocationEnd(val, offset, line, column, tokens, offsets, pu
501
529
  token.type !== 'text-html' &&
502
530
  token.type !== 'indent' &&
503
531
  token.type !== 'outdent') {
504
- const endAttrLineOffset = Math.max(offsets[beforeNewlineToken.loc.end.line - 2], 0) || 0;
532
+ const endAttrLineOffset = Math.max((_a = offsets[beforeNewlineToken.loc.end.line - 2]) !== null && _a !== void 0 ? _a : 0, 0) || 0;
505
533
  const endAttrOffset = endAttrLineOffset + beforeNewlineToken.loc.end.column - 1;
506
534
  return {
507
535
  endOffset: endAttrOffset,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/pug-parser",
3
- "version": "3.3.1",
3
+ "version": "3.5.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.3.1",
20
- "@markuplint/ml-ast": "3.0.0",
21
- "@markuplint/parser-utils": "3.3.0",
19
+ "@markuplint/html-parser": "3.5.0",
20
+ "@markuplint/ml-ast": "3.1.0",
21
+ "@markuplint/parser-utils": "3.5.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": "f19e7de726cf01d53342bc5513c30da75d28da63"
26
+ "gitHead": "0c47b2c2722f6823a17f36edbab98486275f8ab4"
27
27
  }