@markuplint/svelte-parser 4.0.0-alpha.4 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2017-2019 Yusuke Hirao
3
+ Copyright (c) 2017-2023 Yusuke Hirao
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/lib/attr.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { defaultValueDelimiters, parseAttr, sliceFragment } from '@markuplint/parser-utils';
2
- import directiveTokenizer from './directive-tokenizer.js';
2
+ import { directiveTokenizer } from './directive-tokenizer.js';
3
3
  const mustacheTag = {
4
4
  start: '{',
5
5
  end: '}',
@@ -1,2 +1,2 @@
1
1
  import type { MLASTHTMLAttr } from '@markuplint/ml-ast';
2
- export default function directiveTokenizer(raw: string, rawValue: string, line: number, col: number, startOffset: number): MLASTHTMLAttr;
2
+ export declare function directiveTokenizer(raw: string, rawValue: string, line: number, col: number, startOffset: number): MLASTHTMLAttr;
@@ -1,101 +1,16 @@
1
- import { attrTokenizer } from '@markuplint/html-parser';
2
- import { tokenizer, uuid } from '@markuplint/parser-utils';
3
- // eslint-disable-next-line no-control-regex
4
- const reNameOnly = /^[^\u0000-\u001F /=>{\u007F-\u009F]+/;
5
- // eslint-disable-next-line no-control-regex
6
- const reBeforeStructure = /^(\s*)([^\u0000-\u001F /=>{\u007F-\u009F]+)(\s*)(=)(\s*){(\s*)$/;
7
- const reBeforeStructureWithoutName = /^{(\s*)$/;
8
- const reAfterStructure = /(\s*)}/;
9
- export default function directiveTokenizer(raw, rawValue, line, col, startOffset) {
10
- let spacesBeforeAttrString = '';
11
- let nameChars = '';
12
- let spacesBeforeEqualChars = '';
13
- let equalChars = null;
14
- let spacesAfterEqualChars = '';
15
- let valueChars = '';
16
- const [before, after] = raw.split(rawValue);
17
- const beforeMatchedMap = before?.match(reBeforeStructure);
18
- const beforeWithoutNameMatchedMap = before?.match(reBeforeStructureWithoutName);
19
- const afterMatchedMap = after?.match(reAfterStructure);
20
- if (beforeMatchedMap && afterMatchedMap) {
21
- spacesBeforeAttrString = beforeMatchedMap[1] ?? '';
22
- nameChars = beforeMatchedMap[2] ?? '';
23
- spacesBeforeEqualChars = beforeMatchedMap[3] ?? '';
24
- equalChars = beforeMatchedMap[4] ?? null;
25
- spacesAfterEqualChars = beforeMatchedMap[5] ?? '';
26
- valueChars = (beforeMatchedMap[6] ?? '') + rawValue + (afterMatchedMap[1] ?? '');
27
- }
28
- else if (beforeWithoutNameMatchedMap && afterMatchedMap) {
29
- valueChars = (beforeWithoutNameMatchedMap[1] ?? '') + rawValue + (afterMatchedMap[1] ?? '');
30
- }
31
- else if (reNameOnly.test(raw)) {
32
- const token = attrTokenizer(raw, line, col, startOffset);
33
- token.isDirective = true;
34
- return token;
35
- }
36
- else {
37
- throw new SyntaxError('Illegal attribute token');
38
- }
39
- let offset = startOffset;
40
- const attrToken = tokenizer(raw, line, col, offset);
41
- const spacesBeforeName = tokenizer(spacesBeforeAttrString, line, col, offset);
42
- line = spacesBeforeName.endLine;
43
- col = spacesBeforeName.endCol;
44
- offset = spacesBeforeName.endOffset;
45
- const name = tokenizer(nameChars, line, col, offset);
46
- line = name.endLine;
47
- col = name.endCol;
48
- offset = name.endOffset;
49
- const spacesBeforeEqual = tokenizer(spacesBeforeEqualChars, line, col, offset);
50
- line = spacesBeforeEqual.endLine;
51
- col = spacesBeforeEqual.endCol;
52
- offset = spacesBeforeEqual.endOffset;
53
- const equal = tokenizer(equalChars, line, col, offset);
54
- line = equal.endLine;
55
- col = equal.endCol;
56
- offset = equal.endOffset;
57
- const spacesAfterEqual = tokenizer(spacesAfterEqualChars, line, col, offset);
58
- line = spacesAfterEqual.endLine;
59
- col = spacesAfterEqual.endCol;
60
- offset = spacesAfterEqual.endOffset;
61
- const startQuote = tokenizer('{', line, col, offset);
62
- line = startQuote.endLine;
63
- col = startQuote.endCol;
64
- offset = startQuote.endOffset;
65
- const value = tokenizer(valueChars, line, col, offset);
66
- line = value.endLine;
67
- col = value.endCol;
68
- offset = value.endOffset;
69
- const endQuote = tokenizer('}', line, col, offset);
70
- line = endQuote.endLine;
71
- col = endQuote.endCol;
72
- offset = endQuote.endOffset;
73
- const result = {
74
- type: 'html-attr',
75
- uuid: uuid(),
76
- raw: attrToken.raw,
77
- startOffset: attrToken.startOffset,
78
- endOffset: attrToken.endOffset,
79
- startLine: attrToken.startLine,
80
- endLine: attrToken.endLine,
81
- startCol: attrToken.startCol,
82
- endCol: attrToken.endCol,
83
- spacesBeforeName,
84
- name,
85
- spacesBeforeEqual,
86
- equal,
87
- spacesAfterEqual,
88
- startQuote,
89
- value,
90
- endQuote,
91
- isDirective: true,
92
- isDuplicatable: false,
93
- nodeName: name.raw,
94
- parentNode: null,
95
- nextNode: null,
96
- prevNode: null,
97
- isFragment: false,
98
- isGhost: false,
99
- };
100
- return result;
1
+ import { AttrState, attrTokenizer } from '@markuplint/parser-utils';
2
+ export function directiveTokenizer(raw, rawValue, line, col, startOffset) {
3
+ const nameOnly = raw.trim().startsWith('{') && raw.trim().endsWith('}');
4
+ const directiveToken = attrTokenizer(raw, line, col, startOffset, [
5
+ { start: '"', end: '"' },
6
+ { start: "'", end: "'" },
7
+ { start: '{', end: '}' },
8
+ ], nameOnly ? AttrState.BeforeValue : AttrState.BeforeName, [
9
+ { start: '"', end: '"' },
10
+ { start: "'", end: "'" },
11
+ { start: '`', end: '`' },
12
+ { start: '${', end: '}' },
13
+ ]);
14
+ directiveToken.isDirective = true;
15
+ return directiveToken;
101
16
  }
package/lib/nodeize.js CHANGED
@@ -1,5 +1,5 @@
1
- import { getNamespace, parseRawTag } from '@markuplint/html-parser';
2
- import { detectElementType, sliceFragment, uuid } from '@markuplint/parser-utils';
1
+ import { getNamespace } from '@markuplint/html-parser';
2
+ import { detectElementType, sliceFragment, uuid, tagParser } from '@markuplint/parser-utils';
3
3
  import { attr } from './attr.js';
4
4
  import { parseCtrlBlock } from './parse-ctrl-block.js';
5
5
  import { traverse } from './traverse.js';
@@ -96,7 +96,7 @@ parentNode, rawHtml, options) {
96
96
  const directives = originNode.attributes.map(a => attr(a, rawHtml)) ?? [];
97
97
  const attributes = directives.filter((d) => !('__spreadAttr' in d));
98
98
  const hasSpreadAttr = directives.some(d => '__spreadAttr' in d);
99
- const tagTokens = parseRawTag(startTagLocation.raw, startTagLocation.startLine, startTagLocation.startCol, startTagLocation.startOffset);
99
+ const tagTokens = tagParser(startTagLocation.raw, startTagLocation.startLine, startTagLocation.startCol, startTagLocation.startOffset);
100
100
  const namespace = getNamespace(originNode.name, parentNamespace);
101
101
  const startTag = {
102
102
  uuid: uuid(),
@@ -104,7 +104,7 @@ parentNode, rawHtml, options) {
104
104
  nodeName: originNode.name,
105
105
  type: 'starttag',
106
106
  namespace,
107
- elementType: detectElementType(originNode.name, options?.authoredElementName, /[A-Z]|\./),
107
+ elementType: detectElementType(originNode.name, options?.authoredElementName, /[.A-Z]/),
108
108
  attributes,
109
109
  hasSpreadAttr,
110
110
  parentNode,
@@ -112,7 +112,7 @@ parentNode, rawHtml, options) {
112
112
  nextNode,
113
113
  pearNode: endTag,
114
114
  selfClosingSolidus: tagTokens.selfClosingSolidus,
115
- endSpace: tagTokens.endSpace,
115
+ endSpace: tagTokens.afterAttrSpaces,
116
116
  isFragment: false,
117
117
  isGhost: false,
118
118
  tagOpenChar: '<',
@@ -189,7 +189,8 @@ parentNode, rawHtml, options) {
189
189
  awaitCatch.childNodes = traverse(awaitCatchNode.children, awaitCatch, rawHtml, options);
190
190
  }
191
191
  }
192
- const reEndTag = new RegExp('{/await}$', 'i');
192
+ // eslint-disable-next-line regexp/strict
193
+ const reEndTag = /{\/await}$/i;
193
194
  let endTag = null;
194
195
  if (reEndTag.test(raw)) {
195
196
  const endTagRawMatched = raw.match(reEndTag);
@@ -13,7 +13,8 @@ nextNode, options) {
13
13
  return parseIfBlock(originNode, raw, rawHtml, startOffset, originNode.start, parentNode, prevNode, nextNode, options);
14
14
  }
15
15
  const children = originNode.children ?? [];
16
- const reEndTag = new RegExp('{/each}$', 'i');
16
+ // eslint-disable-next-line regexp/strict
17
+ const reEndTag = /{\/each}$/i;
17
18
  const startTagEndOffset = children.length > 0 ? children[0]?.start ?? 0 : raw.replace(reEndTag, '').length + startOffset;
18
19
  const startTagLocation = sliceFragment(rawHtml, startOffset, startTagEndOffset);
19
20
  const tag = {
package/lib/parse.js CHANGED
@@ -1,17 +1,17 @@
1
1
  import { flattenNodes, ParserError, ignoreBlock, restoreNode } from '@markuplint/parser-utils';
2
- import svelteParse from './svelte-parser/index.js';
2
+ import { svelteParse } from './svelte-parser/index.js';
3
3
  import { traverse } from './traverse.js';
4
4
  export const parse = (rawCode, options) => {
5
5
  const blocks = ignoreBlock(rawCode, [
6
6
  {
7
7
  type: 'Script',
8
- start: /<script/,
9
- end: /<\/script>/,
8
+ start: '<script',
9
+ end: '</script>',
10
10
  },
11
11
  {
12
12
  type: 'Style',
13
- start: /<style/,
14
- end: /<\/style>/,
13
+ start: '<style',
14
+ end: '</style>',
15
15
  },
16
16
  ], '-');
17
17
  let ast;
@@ -1,4 +1,4 @@
1
1
  import type { Directive, TemplateNode, Attribute, SpreadAttribute } from 'svelte/types/compiler/interfaces';
2
2
  export type SvelteNode = TemplateNode;
3
- export default function svelteParse(template: string): SvelteNode[];
3
+ export declare function svelteParse(template: string): SvelteNode[];
4
4
  export type SvelteDirective = Directive | Attribute | SpreadAttribute;
@@ -1,5 +1,5 @@
1
1
  import { parse } from 'svelte/compiler';
2
- export default function svelteParse(template) {
2
+ export function svelteParse(template) {
3
3
  const ast = parse(template, { customElement: true });
4
4
  const start = ast.html.start;
5
5
  const children = ast.html.children ?? [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/svelte-parser",
3
- "version": "4.0.0-alpha.4",
3
+ "version": "4.0.0-alpha.5",
4
4
  "description": "Svelte parser for markuplint",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -21,10 +21,10 @@
21
21
  "clean": "tsc --build --clean"
22
22
  },
23
23
  "dependencies": {
24
- "@markuplint/html-parser": "4.0.0-alpha.4",
25
- "@markuplint/ml-ast": "4.0.0-alpha.4",
26
- "@markuplint/parser-utils": "4.0.0-alpha.4",
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
  "svelte": "^4.2.2"
28
28
  },
29
- "gitHead": "991b3aef77fde42c79343ee8c807257a35c589d7"
29
+ "gitHead": "0c3e4690662edf1765bcc4b6411ec5507c1e2ea3"
30
30
  }