@aerobuilt/interpolation 0.2.2
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 +21 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +127 -0
- package/package.json +29 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 James M. Wilson (Aerobuilt)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared tokenizer for `{ }` interpolation. Tracks brace depth and string/comment
|
|
3
|
+
* context so nested braces and content inside strings/comments are handled correctly.
|
|
4
|
+
*
|
|
5
|
+
* @remarks
|
|
6
|
+
* Used by @aerobuilt/core (compileInterpolation, compileAttributeInterpolation) and
|
|
7
|
+
* can be used by aero-vscode for consistent interpolation semantics.
|
|
8
|
+
*/
|
|
9
|
+
type LiteralSegment = {
|
|
10
|
+
kind: 'literal';
|
|
11
|
+
start: number;
|
|
12
|
+
end: number;
|
|
13
|
+
value: string;
|
|
14
|
+
};
|
|
15
|
+
type InterpolationSegment = {
|
|
16
|
+
kind: 'interpolation';
|
|
17
|
+
start: number;
|
|
18
|
+
end: number;
|
|
19
|
+
expression: string;
|
|
20
|
+
};
|
|
21
|
+
type Segment = LiteralSegment | InterpolationSegment;
|
|
22
|
+
interface TokenizeOptions {
|
|
23
|
+
/** When true, `{{` and `}}` emit literal `{` and `}`; otherwise they are two braces. */
|
|
24
|
+
attributeMode?: boolean;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Tokenize a string into literal and interpolation segments. Tracks nesting and
|
|
28
|
+
* string/comment context so expressions like `{ a({ b: 1 }) }` or `{ "}" }` are
|
|
29
|
+
* one interpolation.
|
|
30
|
+
*
|
|
31
|
+
* @param text - Input string (e.g. attribute value or text content).
|
|
32
|
+
* @param options - `attributeMode: true` for attribute values ({{ / }} = literal braces).
|
|
33
|
+
* @returns Array of segments in order.
|
|
34
|
+
*/
|
|
35
|
+
declare function tokenizeCurlyInterpolation(text: string, options?: TokenizeOptions): Segment[];
|
|
36
|
+
/**
|
|
37
|
+
* Build a template-literal-safe string from segments: escape backticks in literal
|
|
38
|
+
* segments and emit `${expression}` for interpolations.
|
|
39
|
+
*
|
|
40
|
+
* @param segments - Output from tokenizeCurlyInterpolation.
|
|
41
|
+
* @returns String safe to embed inside a template literal (backticks).
|
|
42
|
+
*/
|
|
43
|
+
declare function compileInterpolationFromSegments(segments: Segment[]): string;
|
|
44
|
+
|
|
45
|
+
export { type InterpolationSegment, type LiteralSegment, type Segment, type TokenizeOptions, compileInterpolationFromSegments, tokenizeCurlyInterpolation };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
function tokenizeCurlyInterpolation(text, options = {}) {
|
|
3
|
+
const attributeMode = options.attributeMode ?? false;
|
|
4
|
+
const segments = [];
|
|
5
|
+
let i = 0;
|
|
6
|
+
let depth = 0;
|
|
7
|
+
let literalStart = 0;
|
|
8
|
+
let interpOpenIndex = -1;
|
|
9
|
+
let interpExprStart = -1;
|
|
10
|
+
let inString = null;
|
|
11
|
+
let inComment = null;
|
|
12
|
+
function pushLiteral(from, to) {
|
|
13
|
+
if (from < to) {
|
|
14
|
+
segments.push({
|
|
15
|
+
kind: "literal",
|
|
16
|
+
start: from,
|
|
17
|
+
end: to,
|
|
18
|
+
value: text.slice(from, to)
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function pushInterpolation(openIndex, closeIndex) {
|
|
23
|
+
segments.push({
|
|
24
|
+
kind: "interpolation",
|
|
25
|
+
start: openIndex,
|
|
26
|
+
end: closeIndex + 1,
|
|
27
|
+
expression: text.slice(openIndex + 1, closeIndex)
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
while (i < text.length) {
|
|
31
|
+
const char = text[i];
|
|
32
|
+
const next = text[i + 1];
|
|
33
|
+
if (inComment) {
|
|
34
|
+
if (inComment === "//" && char === "\n") {
|
|
35
|
+
inComment = null;
|
|
36
|
+
} else if (inComment === "/*" && char === "*" && next === "/") {
|
|
37
|
+
inComment = null;
|
|
38
|
+
i++;
|
|
39
|
+
}
|
|
40
|
+
i++;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (inString) {
|
|
44
|
+
if (char === "\\") {
|
|
45
|
+
i++;
|
|
46
|
+
} else if (char === inString) {
|
|
47
|
+
inString = null;
|
|
48
|
+
}
|
|
49
|
+
i++;
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (attributeMode && depth === 0) {
|
|
53
|
+
if (char === "{" && next === "{") {
|
|
54
|
+
pushLiteral(literalStart, i);
|
|
55
|
+
segments.push({ kind: "literal", start: i, end: i + 2, value: "{" });
|
|
56
|
+
literalStart = i + 2;
|
|
57
|
+
i += 2;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
if (char === "}" && next === "}") {
|
|
61
|
+
pushLiteral(literalStart, i);
|
|
62
|
+
segments.push({ kind: "literal", start: i, end: i + 2, value: "}" });
|
|
63
|
+
literalStart = i + 2;
|
|
64
|
+
i += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (char === "/" && next === "/") {
|
|
69
|
+
inComment = "//";
|
|
70
|
+
i += 2;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (char === "/" && next === "*") {
|
|
74
|
+
inComment = "/*";
|
|
75
|
+
i += 2;
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if (char === '"' || char === "'" || char === "`") {
|
|
79
|
+
inString = char;
|
|
80
|
+
i++;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (char === "{") {
|
|
84
|
+
if (depth === 0) {
|
|
85
|
+
pushLiteral(literalStart, i);
|
|
86
|
+
interpOpenIndex = i;
|
|
87
|
+
interpExprStart = i + 1;
|
|
88
|
+
}
|
|
89
|
+
depth++;
|
|
90
|
+
i++;
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (char === "}") {
|
|
94
|
+
depth--;
|
|
95
|
+
if (depth === 0) {
|
|
96
|
+
pushInterpolation(interpOpenIndex, i);
|
|
97
|
+
literalStart = i + 1;
|
|
98
|
+
}
|
|
99
|
+
i++;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
i++;
|
|
103
|
+
}
|
|
104
|
+
if (depth > 0) {
|
|
105
|
+
segments.push({
|
|
106
|
+
kind: "interpolation",
|
|
107
|
+
start: interpOpenIndex,
|
|
108
|
+
end: text.length,
|
|
109
|
+
expression: text.slice(interpExprStart, text.length)
|
|
110
|
+
});
|
|
111
|
+
literalStart = text.length;
|
|
112
|
+
}
|
|
113
|
+
pushLiteral(literalStart, text.length);
|
|
114
|
+
return segments;
|
|
115
|
+
}
|
|
116
|
+
function compileInterpolationFromSegments(segments) {
|
|
117
|
+
return segments.map((seg) => {
|
|
118
|
+
if (seg.kind === "literal") {
|
|
119
|
+
return seg.value.replace(/`/g, "\\`");
|
|
120
|
+
}
|
|
121
|
+
return `\${${seg.expression}}`;
|
|
122
|
+
}).join("");
|
|
123
|
+
}
|
|
124
|
+
export {
|
|
125
|
+
compileInterpolationFromSegments,
|
|
126
|
+
tokenizeCurlyInterpolation
|
|
127
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aerobuilt/interpolation",
|
|
3
|
+
"version": "0.2.2",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Jamie Wilson",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/aerobuilt/aero.git",
|
|
10
|
+
"directory": "packages/interpolation"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/aerobuilt/aero",
|
|
13
|
+
"types": "./dist/index.d.ts",
|
|
14
|
+
"main": "./dist/index.js",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"@types/node": "^25.3.0",
|
|
20
|
+
"tsup": "^8.5.1",
|
|
21
|
+
"typescript": "^5.9.3",
|
|
22
|
+
"vitest": "^4.0.18"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsup src/index.ts --format esm --dts --clean --out-dir dist",
|
|
26
|
+
"typecheck": "tsc --noEmit",
|
|
27
|
+
"test": "vitest run"
|
|
28
|
+
}
|
|
29
|
+
}
|