@markuplint/pug-parser 4.0.0-alpha.3 → 4.0.0-alpha.5
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/LICENSE +1 -1
- package/lib/attr-tokenizer.d.ts +1 -1
- package/lib/attr-tokenizer.js +14 -17
- package/lib/parse.js +25 -18
- package/lib/pug-parser/index.js +3 -3
- package/lib/utils/get-offset-from-line-and-col.js +1 -1
- package/package.json +6 -7
package/LICENSE
CHANGED
package/lib/attr-tokenizer.d.ts
CHANGED
package/lib/attr-tokenizer.js
CHANGED
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
import { tokenizer, uuid } from '@markuplint/parser-utils';
|
|
2
|
-
export
|
|
1
|
+
import { tokenizer, uuid, scriptParser, removeQuote } from '@markuplint/parser-utils';
|
|
2
|
+
export function attrTokenizer(
|
|
3
3
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
4
4
|
attr) {
|
|
5
5
|
if (attr.raw[0] === '#' || attr.raw[0] === '.') {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
[, , value] = attr.val.match(/(['"`]?)([^\1]+)(\1)/) ?? ['', '', ''];
|
|
9
|
-
}
|
|
10
|
-
else {
|
|
11
|
-
value = `${attr.val}`;
|
|
12
|
-
}
|
|
6
|
+
const value = `${attr.val}`;
|
|
7
|
+
const potentialValue = removeQuote(value);
|
|
13
8
|
return {
|
|
14
9
|
type: 'ps-attr',
|
|
15
10
|
uuid: uuid(),
|
|
@@ -21,7 +16,7 @@ attr) {
|
|
|
21
16
|
startCol: attr.column,
|
|
22
17
|
endCol: attr.endColumn,
|
|
23
18
|
potentialName: attr.name,
|
|
24
|
-
potentialValue
|
|
19
|
+
potentialValue,
|
|
25
20
|
valueType: 'string',
|
|
26
21
|
isDuplicatable: attr.raw[0] === '.',
|
|
27
22
|
nodeName: '#pug-special-attr',
|
|
@@ -37,32 +32,33 @@ attr) {
|
|
|
37
32
|
let spacesBeforeEqualChars;
|
|
38
33
|
let equalChars;
|
|
39
34
|
let spacesAfterEqualChars;
|
|
40
|
-
|
|
35
|
+
const quoteChars = '';
|
|
41
36
|
let valueChars;
|
|
37
|
+
let potentialValue;
|
|
42
38
|
let isDynamicValue = undefined;
|
|
43
39
|
if (attr.val === true) {
|
|
44
40
|
spacesBeforeEqualChars = '';
|
|
45
41
|
equalChars = '';
|
|
46
42
|
spacesAfterEqualChars = '';
|
|
47
|
-
quoteChars = '';
|
|
48
43
|
valueChars = '';
|
|
44
|
+
potentialValue = '';
|
|
49
45
|
}
|
|
50
46
|
else {
|
|
51
47
|
const withoutName = attr.raw.slice(attr.name.length);
|
|
52
48
|
const valueOffset = withoutName.indexOf(attr.val);
|
|
53
49
|
const equalAndBeforeSpaceAfterSpace = withoutName.slice(0, valueOffset);
|
|
54
50
|
const [, before, equal, after] = equalAndBeforeSpaceAfterSpace.match(/^(\s*)(=)(\s*)$/) ?? ['', '', '', ''];
|
|
55
|
-
const
|
|
51
|
+
const valueTokens = scriptParser(attr.val);
|
|
56
52
|
spacesBeforeEqualChars = before ?? '';
|
|
57
53
|
equalChars = equal ?? '';
|
|
58
54
|
spacesAfterEqualChars = after ?? '';
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
if (
|
|
55
|
+
valueChars = attr.val;
|
|
56
|
+
potentialValue = removeQuote(attr.val);
|
|
57
|
+
if (valueTokens.length > 1 || valueTokens[0].type !== 'String') {
|
|
62
58
|
isDynamicValue = true;
|
|
63
59
|
}
|
|
64
60
|
}
|
|
65
|
-
const invalid = !!(valueChars && quoteChars === null && /["'
|
|
61
|
+
const invalid = !!(valueChars && quoteChars === null && /["'<=>`]/.test(valueChars)) ||
|
|
66
62
|
!!(equalChars && quoteChars === null && valueChars === null);
|
|
67
63
|
if (invalid) {
|
|
68
64
|
throw new Error('Parse error: It has invalid attribute');
|
|
@@ -127,6 +123,7 @@ attr) {
|
|
|
127
123
|
endQuote,
|
|
128
124
|
isDynamicValue,
|
|
129
125
|
isDuplicatable,
|
|
126
|
+
potentialValue,
|
|
130
127
|
nodeName: name.raw,
|
|
131
128
|
parentNode: null,
|
|
132
129
|
nextNode: null,
|
package/lib/parse.js
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
2
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
3
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
4
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
5
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
6
|
+
};
|
|
7
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
8
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
9
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
|
+
};
|
|
1
12
|
var _Parser_ast;
|
|
2
|
-
import {
|
|
3
|
-
import { getNamespace, parse as htmlParser, isDocumentFragment } from '@markuplint/html-parser';
|
|
13
|
+
import { getNamespace, parse as htmlParse, isDocumentFragment } from '@markuplint/html-parser';
|
|
4
14
|
import { detectElementType, ignoreFrontMatter, ParserError, tokenizer, uuid, walk, removeDeprecatedNode, } from '@markuplint/parser-utils';
|
|
5
|
-
import attrTokenizer from './attr-tokenizer.js';
|
|
15
|
+
import { attrTokenizer } from './attr-tokenizer.js';
|
|
6
16
|
import { pugParse } from './pug-parser/index.js';
|
|
7
17
|
export const parse = (rawCode, options) => {
|
|
8
18
|
let unknownParseError;
|
|
@@ -14,21 +24,21 @@ export const parse = (rawCode, options) => {
|
|
|
14
24
|
const parser = new Parser(rawCode);
|
|
15
25
|
nodeList = parser.getNodeList();
|
|
16
26
|
}
|
|
17
|
-
catch (
|
|
27
|
+
catch (error) {
|
|
18
28
|
nodeList = [];
|
|
19
|
-
if (
|
|
29
|
+
if (error instanceof Error && 'msg' in error && 'line' in error && 'column' in error && 'src' in error) {
|
|
20
30
|
throw new ParserError(
|
|
21
31
|
// @ts-ignore
|
|
22
|
-
|
|
32
|
+
error.msg, {
|
|
23
33
|
// @ts-ignore
|
|
24
|
-
line:
|
|
34
|
+
line: error.line,
|
|
25
35
|
// @ts-ignore
|
|
26
|
-
col:
|
|
36
|
+
col: error.column,
|
|
27
37
|
// @ts-ignore
|
|
28
|
-
raw:
|
|
38
|
+
raw: error.src,
|
|
29
39
|
});
|
|
30
40
|
}
|
|
31
|
-
unknownParseError =
|
|
41
|
+
unknownParseError = error instanceof Error ? error.message : new Error(`${error}`).message;
|
|
32
42
|
}
|
|
33
43
|
return {
|
|
34
44
|
nodeList,
|
|
@@ -117,17 +127,14 @@ class Parser {
|
|
|
117
127
|
isGhost: false,
|
|
118
128
|
};
|
|
119
129
|
}
|
|
120
|
-
const htmlDoc =
|
|
130
|
+
const htmlDoc = htmlParse(originNode.raw, {
|
|
121
131
|
offsetOffset: originNode.offset,
|
|
122
132
|
offsetLine: originNode.line - 1,
|
|
123
133
|
offsetColumn: originNode.column - 1,
|
|
124
134
|
});
|
|
125
|
-
const nodes = htmlDoc.nodeList
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
node.parentNode = parentNode;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
135
|
+
const nodes = htmlDoc.nodeList.filter(node => {
|
|
136
|
+
return node.parentNode == null && node.type !== 'endtag';
|
|
137
|
+
});
|
|
131
138
|
return nodes;
|
|
132
139
|
}
|
|
133
140
|
case 'Comment': {
|
|
@@ -219,7 +226,7 @@ class Parser {
|
|
|
219
226
|
}
|
|
220
227
|
let node;
|
|
221
228
|
if (Array.isArray(nodes)) {
|
|
222
|
-
const lastNode = nodes
|
|
229
|
+
const lastNode = nodes.at(-1);
|
|
223
230
|
if (!lastNode) {
|
|
224
231
|
continue;
|
|
225
232
|
}
|
package/lib/pug-parser/index.js
CHANGED
|
@@ -13,7 +13,7 @@ export function pugParse(pug) {
|
|
|
13
13
|
return ast;
|
|
14
14
|
}
|
|
15
15
|
function getOffsetsFromLines(pug) {
|
|
16
|
-
const lines = pug.split(/\n/
|
|
16
|
+
const lines = pug.split(/\n/);
|
|
17
17
|
let chars = 0;
|
|
18
18
|
const result = lines.map(line => {
|
|
19
19
|
chars += line.length + 1;
|
|
@@ -26,7 +26,7 @@ function mergeTextNode(
|
|
|
26
26
|
nodes, pug) {
|
|
27
27
|
const baseNodes = [];
|
|
28
28
|
for (const node of nodes) {
|
|
29
|
-
const prevNode = baseNodes
|
|
29
|
+
const prevNode = baseNodes.at(-1) ?? null;
|
|
30
30
|
if (prevNode && prevNode.type === 'Text' && node.type === 'Text') {
|
|
31
31
|
prevNode.raw = pug.slice(prevNode.offset, node.endOffset);
|
|
32
32
|
prevNode.endColumn = node.endColumn;
|
|
@@ -382,7 +382,7 @@ tokens, pug, offsets, depth) {
|
|
|
382
382
|
function getLocationFromToken(offset, line, column,
|
|
383
383
|
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
384
384
|
tokens, tokenType) {
|
|
385
|
-
const tokenTypes = typeof tokenType === 'string' ? (tokenType
|
|
385
|
+
const tokenTypes = typeof tokenType === 'string' ? (tokenType === '' ? null : [tokenType]) : tokenType ?? null;
|
|
386
386
|
let tokenOfCurrentNode = null;
|
|
387
387
|
for (const token of tokens) {
|
|
388
388
|
if ((tokenTypes == null || tokenTypes.includes(token.type)) &&
|
|
@@ -2,7 +2,7 @@ export function getOffsetFromLineAndCol(str, line, col) {
|
|
|
2
2
|
const lines = str.split('\n').slice(0, line);
|
|
3
3
|
const lastLine = lines.pop();
|
|
4
4
|
if (lastLine) {
|
|
5
|
-
lines.push(lastLine.
|
|
5
|
+
lines.push([...lastLine].slice(0, col).join(''));
|
|
6
6
|
}
|
|
7
7
|
return lines.join('\n').length;
|
|
8
8
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@markuplint/pug-parser",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.5",
|
|
4
4
|
"description": "Pug parser for markuplint",
|
|
5
5
|
"repository": "git@github.com:markuplint/markuplint.git",
|
|
6
6
|
"author": "Yusuke Hirao <yusukehirao@me.com>",
|
|
@@ -21,12 +21,11 @@
|
|
|
21
21
|
"clean": "tsc --build --clean"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@markuplint/html-parser": "4.0.0-alpha.
|
|
25
|
-
"@markuplint/ml-ast": "4.0.0-alpha.
|
|
26
|
-
"@markuplint/parser-utils": "4.0.0-alpha.
|
|
24
|
+
"@markuplint/html-parser": "4.0.0-alpha.5",
|
|
25
|
+
"@markuplint/ml-ast": "4.0.0-alpha.5",
|
|
26
|
+
"@markuplint/parser-utils": "4.0.0-alpha.5",
|
|
27
27
|
"pug-lexer": "^5.0.1",
|
|
28
|
-
"pug-parser": "^6.0.0"
|
|
29
|
-
"tslib": "^2.6.2"
|
|
28
|
+
"pug-parser": "^6.0.0"
|
|
30
29
|
},
|
|
31
|
-
"gitHead": "
|
|
30
|
+
"gitHead": "0c3e4690662edf1765bcc4b6411ec5507c1e2ea3"
|
|
32
31
|
}
|