@markuplint/parser-utils 4.5.0 → 4.5.1

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.
@@ -23,6 +23,7 @@ export function attrTokenizer(raw, quoteSet = defaultQuoteSet, startState = Attr
23
23
  let quoteStart = '';
24
24
  let attrValue = '';
25
25
  let valueType = noQuoteValueType;
26
+ let parser;
26
27
  let quoteEnd = '';
27
28
  const isBeforeValueStarted = startState === AttrState.BeforeValue;
28
29
  const chars = [...raw];
@@ -105,6 +106,7 @@ export function attrTokenizer(raw, quoteSet = defaultQuoteSet, startState = Attr
105
106
  if (quote) {
106
107
  quoteStart = quote.start;
107
108
  valueType = quote.type;
109
+ parser = quote.parser;
108
110
  state = AttrState.Value;
109
111
  break;
110
112
  }
@@ -125,7 +127,7 @@ export function attrTokenizer(raw, quoteSet = defaultQuoteSet, startState = Attr
125
127
  }
126
128
  if (valueType === 'script') {
127
129
  const raw = char + chars.join('');
128
- const { validScript } = safeScriptParser(raw);
130
+ const { validScript } = safeScriptParser(raw, parser);
129
131
  attrValue += validScript;
130
132
  chars.splice(0, validScript.length - 1);
131
133
  break;
@@ -1,5 +1,6 @@
1
+ import type { CustomParser } from './types.js';
1
2
  export declare function scriptParser(script: string): ScriptTokenType[];
2
- export declare function safeScriptParser(script: string): {
3
+ export declare function safeScriptParser(script: string, parse?: CustomParser): {
3
4
  validScript: string;
4
5
  leftover: string;
5
6
  };
@@ -10,19 +10,19 @@ export function scriptParser(script) {
10
10
  value: token.value,
11
11
  }));
12
12
  }
13
- export function safeScriptParser(script) {
14
- let { validScript, leftover } = safeParse(script);
13
+ export function safeScriptParser(script, parse = defaultParse) {
14
+ let { validScript, leftover } = safeParse(script, parse);
15
15
  // Support for object literal
16
16
  if (leftover.trim()) {
17
17
  const assignment = '$=';
18
- ({ validScript } = safeParse(`${assignment}${script}`));
18
+ ({ validScript } = safeParse(`${assignment}${script}`, parse));
19
19
  validScript = validScript.length > assignment.length ? validScript.slice(assignment.length) : '';
20
20
  }
21
21
  // Support for spread operator
22
22
  if (validScript.trim() === '') {
23
23
  const coverStart = '$={';
24
24
  const coverEnd = '}';
25
- ({ validScript } = safeParse(`${coverStart}${script}${coverEnd}`));
25
+ ({ validScript } = safeParse(`${coverStart}${script}${coverEnd}`, parse));
26
26
  const coverEndLastIndex = validScript.lastIndexOf(coverEnd);
27
27
  validScript =
28
28
  validScript.length > coverStart.length + coverEnd.length
@@ -35,16 +35,11 @@ export function safeScriptParser(script) {
35
35
  leftover,
36
36
  };
37
37
  }
38
- function safeParse(script) {
38
+ function safeParse(script, parse) {
39
39
  let validScript;
40
40
  let leftover;
41
41
  try {
42
- parse(script, {
43
- ecmaVersion: 'latest',
44
- ecmaFeatures: {
45
- jsx: true,
46
- },
47
- });
42
+ parse(script);
48
43
  validScript = script;
49
44
  leftover = '';
50
45
  }
@@ -67,3 +62,11 @@ function safeParse(script) {
67
62
  leftover,
68
63
  };
69
64
  }
65
+ function defaultParse(script) {
66
+ parse(script, {
67
+ ecmaVersion: 'latest',
68
+ ecmaFeatures: {
69
+ jsx: true,
70
+ },
71
+ });
72
+ }
package/lib/types.d.ts CHANGED
@@ -54,5 +54,7 @@ export type QuoteSet = {
54
54
  readonly start: string;
55
55
  readonly end: string;
56
56
  readonly type: ValueType;
57
+ readonly parser?: CustomParser;
57
58
  };
59
+ export type CustomParser = (code: string) => void;
58
60
  export type ValueType = 'string' | 'script';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@markuplint/parser-utils",
3
- "version": "4.5.0",
3
+ "version": "4.5.1",
4
4
  "description": "Utility module for markuplint parser plugin",
5
5
  "repository": "git@github.com:markuplint/markuplint.git",
6
6
  "author": "Yusuke Hirao <yusukehirao@me.com>",
@@ -28,7 +28,7 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@markuplint/ml-ast": "4.2.0",
31
- "@markuplint/ml-spec": "4.4.0",
31
+ "@markuplint/ml-spec": "4.4.1",
32
32
  "@markuplint/types": "4.3.0",
33
33
  "@types/uuid": "9.0.8",
34
34
  "debug": "4.3.4",
@@ -36,5 +36,8 @@
36
36
  "type-fest": "4.15.0",
37
37
  "uuid": "9.0.1"
38
38
  },
39
- "gitHead": "d5c8786b0dbbd82cdd89018dd57941d62bbe8d06"
39
+ "devDependencies": {
40
+ "@typescript-eslint/typescript-estree": "7.7.0"
41
+ },
42
+ "gitHead": "b029c86a6b3a9ea8189d2e5535e3023aaea753fd"
40
43
  }