@markuplint/pug-parser 3.12.0 → 4.0.0-alpha.10

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/package.json CHANGED
@@ -1,12 +1,17 @@
1
1
  {
2
2
  "name": "@markuplint/pug-parser",
3
- "version": "3.12.0",
3
+ "version": "4.0.0-alpha.10",
4
4
  "description": "Pug parser for markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
7
7
  "license": "MIT",
8
8
  "private": false,
9
- "main": "lib/index.js",
9
+ "type": "module",
10
+ "exports": {
11
+ ".": {
12
+ "import": "./lib/index.js"
13
+ }
14
+ },
10
15
  "types": "lib/index.d.ts",
11
16
  "publishConfig": {
12
17
  "access": "public"
@@ -16,12 +21,11 @@
16
21
  "clean": "tsc --build --clean"
17
22
  },
18
23
  "dependencies": {
19
- "@markuplint/html-parser": "3.13.0",
20
- "@markuplint/ml-ast": "3.2.0",
21
- "@markuplint/parser-utils": "3.13.0",
24
+ "@markuplint/html-parser": "4.0.0-alpha.10",
25
+ "@markuplint/ml-ast": "4.0.0-alpha.10",
26
+ "@markuplint/parser-utils": "4.0.0-alpha.10",
22
27
  "pug-lexer": "^5.0.1",
23
- "pug-parser": "^6.0.0",
24
- "tslib": "^2.6.2"
28
+ "pug-parser": "^6.0.0"
25
29
  },
26
- "gitHead": "b37b749d7ac0f9e6cbd022ee7031bc020c6677d3"
30
+ "gitHead": "b41153ea665aa8f091daf6114a06047f4ccb8350"
27
31
  }
@@ -1,3 +0,0 @@
1
- import type { ASTAttr } from './pug-parser';
2
- import type { MLASTAttr } from '@markuplint/ml-ast';
3
- export default function attrTokenizer(attr: ASTAttr): MLASTAttr;
@@ -1,141 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const parser_utils_1 = require("@markuplint/parser-utils");
4
- function attrTokenizer(
5
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
6
- attr) {
7
- var _a, _b, _c;
8
- if (attr.raw[0] === '#' || attr.raw[0] === '.') {
9
- let value = '';
10
- if (typeof attr.val === 'string') {
11
- [, , value] = (_a = attr.val.match(/(['"`]?)([^\1]+)(\1)/)) !== null && _a !== void 0 ? _a : ['', '', ''];
12
- }
13
- else {
14
- value = `${attr.val}`;
15
- }
16
- return {
17
- type: 'ps-attr',
18
- uuid: (0, parser_utils_1.uuid)(),
19
- raw: attr.raw,
20
- startOffset: attr.offset,
21
- endOffset: attr.endOffset,
22
- startLine: attr.line,
23
- endLine: attr.endLine,
24
- startCol: attr.column,
25
- endCol: attr.endColumn,
26
- potentialName: attr.name,
27
- potentialValue: value !== null && value !== void 0 ? value : '',
28
- valueType: 'string',
29
- isDuplicatable: attr.raw[0] === '.',
30
- nodeName: '#pug-special-attr',
31
- parentNode: null,
32
- nextNode: null,
33
- prevNode: null,
34
- isFragment: false,
35
- isGhost: false,
36
- };
37
- }
38
- const spacesBeforeAttrString = '';
39
- const nameChars = attr.name;
40
- let spacesBeforeEqualChars;
41
- let equalChars;
42
- let spacesAfterEqualChars;
43
- let quoteChars;
44
- let valueChars;
45
- let isDynamicValue = undefined;
46
- if (attr.val === true) {
47
- spacesBeforeEqualChars = '';
48
- equalChars = '';
49
- spacesAfterEqualChars = '';
50
- quoteChars = '';
51
- valueChars = '';
52
- }
53
- else {
54
- const withoutName = attr.raw.slice(attr.name.length);
55
- const valueOffset = withoutName.indexOf(attr.val);
56
- const equalAndBeforeSpaceAfterSpace = withoutName.slice(0, valueOffset);
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 : ['', '', ''];
59
- spacesBeforeEqualChars = before !== null && before !== void 0 ? before : '';
60
- equalChars = equal !== null && equal !== void 0 ? equal : '';
61
- spacesAfterEqualChars = after !== null && after !== void 0 ? after : '';
62
- quoteChars = quote !== null && quote !== void 0 ? quote : '';
63
- valueChars = coreValue !== null && coreValue !== void 0 ? coreValue : '';
64
- if (quote === '`' || quote === '') {
65
- isDynamicValue = true;
66
- }
67
- }
68
- const invalid = !!(valueChars && quoteChars === null && /["'=<>`]/.test(valueChars)) ||
69
- !!(equalChars && quoteChars === null && valueChars === null);
70
- if (invalid) {
71
- throw new Error('Parse error: It has invalid attribute');
72
- }
73
- let offset = attr.offset;
74
- let line = attr.line;
75
- let col = attr.column;
76
- const attrToken = (0, parser_utils_1.tokenizer)(attr.raw, line, col, offset);
77
- const spacesBeforeName = (0, parser_utils_1.tokenizer)(spacesBeforeAttrString, line, col, offset);
78
- line = spacesBeforeName.endLine;
79
- col = spacesBeforeName.endCol;
80
- offset = spacesBeforeName.endOffset;
81
- const name = (0, parser_utils_1.tokenizer)(nameChars, line, col, offset);
82
- line = name.endLine;
83
- col = name.endCol;
84
- offset = name.endOffset;
85
- const spacesBeforeEqual = (0, parser_utils_1.tokenizer)(spacesBeforeEqualChars, line, col, offset);
86
- line = spacesBeforeEqual.endLine;
87
- col = spacesBeforeEqual.endCol;
88
- offset = spacesBeforeEqual.endOffset;
89
- const equal = (0, parser_utils_1.tokenizer)(equalChars, line, col, offset);
90
- line = equal.endLine;
91
- col = equal.endCol;
92
- offset = equal.endOffset;
93
- const spacesAfterEqual = (0, parser_utils_1.tokenizer)(spacesAfterEqualChars, line, col, offset);
94
- line = spacesAfterEqual.endLine;
95
- col = spacesAfterEqual.endCol;
96
- offset = spacesAfterEqual.endOffset;
97
- const startQuote = (0, parser_utils_1.tokenizer)(quoteChars, line, col, offset);
98
- line = startQuote.endLine;
99
- col = startQuote.endCol;
100
- offset = startQuote.endOffset;
101
- const value = (0, parser_utils_1.tokenizer)(valueChars, line, col, offset);
102
- line = value.endLine;
103
- col = value.endCol;
104
- offset = value.endOffset;
105
- const endQuote = (0, parser_utils_1.tokenizer)(quoteChars, line, col, offset);
106
- line = endQuote.endLine;
107
- col = endQuote.endCol;
108
- offset = endQuote.endOffset;
109
- let isDuplicatable = false;
110
- if (name.raw.toLowerCase() === 'class') {
111
- isDuplicatable = true;
112
- }
113
- return {
114
- type: 'html-attr',
115
- uuid: (0, parser_utils_1.uuid)(),
116
- raw: attrToken.raw,
117
- startOffset: attrToken.startOffset,
118
- endOffset: attrToken.endOffset,
119
- startLine: attrToken.startLine,
120
- endLine: attrToken.endLine,
121
- startCol: attrToken.startCol,
122
- endCol: attrToken.endCol,
123
- spacesBeforeName,
124
- name,
125
- spacesBeforeEqual,
126
- equal,
127
- spacesAfterEqual,
128
- startQuote,
129
- value,
130
- endQuote,
131
- isDynamicValue,
132
- isDuplicatable,
133
- nodeName: name.raw,
134
- parentNode: null,
135
- nextNode: null,
136
- prevNode: null,
137
- isFragment: false,
138
- isGhost: false,
139
- };
140
- }
141
- exports.default = attrTokenizer;
package/lib/parse.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import type { Parse } from '@markuplint/ml-ast';
2
- export declare const parse: Parse;
package/lib/parse.js DELETED
@@ -1,250 +0,0 @@
1
- "use strict";
2
- var _Parser_ast;
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.parse = void 0;
5
- const tslib_1 = require("tslib");
6
- const html_parser_1 = require("@markuplint/html-parser");
7
- const parser_utils_1 = require("@markuplint/parser-utils");
8
- const attr_tokenizer_1 = tslib_1.__importDefault(require("./attr-tokenizer"));
9
- const pug_parser_1 = require("./pug-parser");
10
- const parse = (rawCode, options) => {
11
- let unknownParseError;
12
- let nodeList;
13
- if (options === null || options === void 0 ? void 0 : options.ignoreFrontMatter) {
14
- rawCode = (0, parser_utils_1.ignoreFrontMatter)(rawCode);
15
- }
16
- try {
17
- const parser = new Parser(rawCode);
18
- nodeList = parser.getNodeList();
19
- }
20
- catch (err) {
21
- nodeList = [];
22
- if (err instanceof Error && 'msg' in err && 'line' in err && 'column' in err && 'src' in err) {
23
- throw new parser_utils_1.ParserError(
24
- // @ts-ignore
25
- err.msg, {
26
- // @ts-ignore
27
- line: err.line,
28
- // @ts-ignore
29
- col: err.column,
30
- // @ts-ignore
31
- raw: err.src,
32
- });
33
- }
34
- unknownParseError = err instanceof Error ? err.message : new Error(`${err}`).message;
35
- }
36
- return {
37
- nodeList,
38
- isFragment: (0, html_parser_1.isDocumentFragment)(rawCode),
39
- unknownParseError: unknownParseError,
40
- };
41
- };
42
- exports.parse = parse;
43
- class Parser {
44
- constructor(raw) {
45
- _Parser_ast.set(this, void 0);
46
- tslib_1.__classPrivateFieldSet(this, _Parser_ast, (0, pug_parser_1.pugParse)(raw), "f");
47
- // console.log(JSON.stringify(this.#ast, null, 2));
48
- }
49
- flattenNodes(
50
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
51
- nodeTree) {
52
- const nodeOrders = [];
53
- (0, parser_utils_1.walk)(nodeTree, node => {
54
- nodeOrders.push(node);
55
- });
56
- (0, parser_utils_1.removeDeprecatedNode)(nodeOrders);
57
- return nodeOrders;
58
- }
59
- getNodeList() {
60
- const nodeTree = this.traverse(tslib_1.__classPrivateFieldGet(this, _Parser_ast, "f").nodes, null);
61
- return this.flattenNodes(nodeTree);
62
- }
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) {
70
- var _a;
71
- const nextNode = null;
72
- const startOffset = originNode.offset;
73
- const endOffset = originNode.endOffset;
74
- const startLine = originNode.line;
75
- const endLine = originNode.endLine;
76
- const startCol = originNode.column;
77
- const endCol = originNode.endColumn;
78
- const parentNamespace = parentNode && 'namespace' in parentNode ? parentNode.namespace : 'http://www.w3.org/1999/xhtml';
79
- switch (originNode.type) {
80
- case 'Doctype': {
81
- return {
82
- uuid: (0, parser_utils_1.uuid)(),
83
- raw: originNode.raw,
84
- name: (_a = originNode.val) !== null && _a !== void 0 ? _a : '',
85
- // TODO:
86
- publicId: '',
87
- // TODO:
88
- systemId: '',
89
- startOffset,
90
- endOffset,
91
- startLine,
92
- endLine,
93
- startCol,
94
- endCol,
95
- nodeName: '#doctype',
96
- type: 'doctype',
97
- parentNode,
98
- prevNode,
99
- _addPrevNode: 102,
100
- nextNode,
101
- isFragment: false,
102
- isGhost: false,
103
- };
104
- }
105
- case 'Text': {
106
- if (parentNode && /^script$|^style$/i.test(parentNode.nodeName)) {
107
- return {
108
- uuid: (0, parser_utils_1.uuid)(),
109
- raw: originNode.raw,
110
- startOffset,
111
- endOffset,
112
- startLine,
113
- endLine,
114
- startCol,
115
- endCol,
116
- nodeName: '#text',
117
- type: 'text',
118
- parentNode,
119
- prevNode,
120
- nextNode,
121
- isFragment: false,
122
- isGhost: false,
123
- };
124
- }
125
- const htmlDoc = (0, html_parser_1.parse)(originNode.raw, {
126
- offsetOffset: originNode.offset,
127
- offsetLine: originNode.line - 1,
128
- offsetColumn: originNode.column - 1,
129
- });
130
- const nodes = htmlDoc.nodeList.filter(node => {
131
- return node.parentNode == null && node.type !== 'endtag';
132
- });
133
- return nodes;
134
- }
135
- case 'Comment': {
136
- return {
137
- uuid: (0, parser_utils_1.uuid)(),
138
- raw: originNode.raw,
139
- startOffset,
140
- endOffset,
141
- startLine,
142
- endLine,
143
- startCol,
144
- endCol,
145
- nodeName: '#comment',
146
- type: 'comment',
147
- parentNode,
148
- prevNode,
149
- nextNode,
150
- isFragment: false,
151
- isGhost: false,
152
- };
153
- }
154
- case 'Tag': {
155
- const namespace = (0, html_parser_1.getNamespace)(originNode.name, parentNamespace);
156
- const tag = {
157
- uuid: (0, parser_utils_1.uuid)(),
158
- raw: originNode.raw,
159
- startOffset,
160
- endOffset,
161
- startLine,
162
- endLine,
163
- startCol,
164
- endCol,
165
- nodeName: originNode.name,
166
- type: 'starttag',
167
- namespace,
168
- elementType: (0, parser_utils_1.detectElementType)(originNode.name),
169
- attributes: originNode.attrs.map(attr => (0, attr_tokenizer_1.default)(attr)),
170
- hasSpreadAttr: false,
171
- parentNode,
172
- prevNode,
173
- nextNode,
174
- pearNode: null,
175
- selfClosingSolidus: (0, parser_utils_1.tokenizer)('', originNode.line, originNode.column, originNode.offset),
176
- endSpace: (0, parser_utils_1.tokenizer)('', originNode.line, originNode.column, originNode.offset),
177
- isFragment: false,
178
- isGhost: false,
179
- tagOpenChar: '',
180
- tagCloseChar: '',
181
- };
182
- if (originNode.block.nodes.length > 0) {
183
- tag.childNodes = this.traverse(originNode.block.nodes, tag);
184
- }
185
- return tag;
186
- }
187
- default: {
188
- const tag = {
189
- uuid: (0, parser_utils_1.uuid)(),
190
- raw: originNode.raw,
191
- startOffset,
192
- endOffset,
193
- startLine,
194
- endLine,
195
- startCol,
196
- endCol,
197
- type: 'psblock',
198
- nodeName: originNode.type,
199
- parentNode,
200
- prevNode,
201
- nextNode,
202
- isFragment: true,
203
- isGhost: false,
204
- };
205
- if ('block' in originNode && originNode.block && originNode.block.nodes.length > 0) {
206
- tag.childNodes = this.traverse(originNode.block.nodes, tag);
207
- }
208
- return tag;
209
- }
210
- }
211
- }
212
- traverse(
213
- // eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
214
- astNodes, parentNode = null) {
215
- const nodeList = [];
216
- let prevNode = null;
217
- for (const astNode of astNodes) {
218
- const nodes = this.nodeize(astNode, prevNode, parentNode);
219
- if (!nodes || (Array.isArray(nodes) && nodes.length === 0)) {
220
- continue;
221
- }
222
- let node;
223
- if (Array.isArray(nodes)) {
224
- const lastNode = nodes[nodes.length - 1];
225
- if (!lastNode) {
226
- continue;
227
- }
228
- node = lastNode;
229
- }
230
- else {
231
- node = nodes;
232
- }
233
- if (prevNode) {
234
- if (node.type !== 'endtag') {
235
- prevNode.nextNode = node;
236
- }
237
- node.prevNode = prevNode;
238
- }
239
- prevNode = node;
240
- if (Array.isArray(nodes)) {
241
- nodeList.push(...nodes);
242
- }
243
- else {
244
- nodeList.push(nodes);
245
- }
246
- }
247
- return nodeList;
248
- }
249
- }
250
- _Parser_ast = new WeakMap();