Skip to content

Commit b62ac68

Browse files
authored
fix: parsing error in multiple tags with spaces (#289)
* fix: parsing error in multiple tags with spaces * Create clever-trees-melt.md
1 parent ef30205 commit b62ac68

File tree

8 files changed

+1553
-14
lines changed

8 files changed

+1553
-14
lines changed

.changeset/clever-trees-melt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro-eslint-parser": patch
3+
---
4+
5+
fix: parsing error in multiple tags with spaces

explorer-v3/astro.config.mjs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defineConfig } from "astro/config";
22
import svelte from "@astrojs/svelte";
33
import { resolve } from "path";
4+
import { version as monacoVersion } from "monaco-editor/package.json";
45

56
// https://astro.build/config
67
export default defineConfig({
@@ -11,23 +12,26 @@ export default defineConfig({
1112
server: {
1213
fs: { strict: false },
1314
},
15+
define: {
16+
MONACO_EDITOR_VERSION: JSON.stringify(monacoVersion),
17+
},
1418
resolve: {
1519
alias: {
1620
assert: resolve("./build-system/shim/assert.js"),
1721
path: resolve("./build-system/shim/path.js"),
1822
fs: resolve("./build-system/shim/fs.js"),
1923
module: resolve("./build-system/shim/module.js"),
2024
"eslint/package.json": resolve(
21-
"./build-system/shim/eslint/package.json"
25+
"./build-system/shim/eslint/package.json",
2226
),
2327
eslint: resolve("./build-system/shim/eslint/index.js"),
2428
"astro-eslint-parser": resolve(
25-
"./build-system/shim/astro-eslint-parser/index.js"
29+
"./build-system/shim/astro-eslint-parser/index.js",
2630
),
2731
globby: resolve("./build-system/shim/globby.js"),
2832
tslib: resolve("../node_modules/tslib/tslib.es6.js"),
2933
"escape-string-regexp": resolve(
30-
"./build-system/shim/escape-string-regexp/index.js"
34+
"./build-system/shim/escape-string-regexp/index.js",
3135
),
3236
resolve: resolve("./build-system/shim/resolve.js"),
3337
},

explorer-v3/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"astro-eslint-parser": "file:..",
1919
"eslint": "^8.15.0",
2020
"eslint-plugin-react": "^7.29.4",
21+
"monaco-editor": "^0.45.0",
2122
"pako": "^2.0.4",
2223
"svelte": "^4.0.0",
2324
"webpack": "^5.72.1",

explorer-v3/src/components/scripts/monaco-loader.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
async function setupMonaco() {
1+
/* global MONACO_EDITOR_VERSION -- defined */
2+
async function loadMonaco() {
23
if (typeof window !== "undefined") {
34
const monacoScript =
45
Array.from(document.head.querySelectorAll("script")).find(
@@ -23,8 +24,12 @@ async function setupMonaco() {
2324
function appendMonacoEditorScript() {
2425
return new Promise((resolve) => {
2526
const script = document.createElement("script");
26-
script.src =
27-
"https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.26.1/min/vs/loader.min.js";
27+
28+
if (typeof MONACO_EDITOR_VERSION !== "undefined") {
29+
script.src = `https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/${MONACO_EDITOR_VERSION}/min/vs/loader.min.js`;
30+
} else {
31+
script.src = "https://unpkg.com/monaco-editor@latest/min/vs/loader.js";
32+
}
2833
script.onload = () => {
2934
script.onload = null;
3035

@@ -42,11 +47,11 @@ function appendMonacoEditorScript() {
4247
});
4348
}
4449

45-
let setupedMonaco = null;
50+
let loadedMonaco = null;
4651
let editorLoaded = null;
4752

4853
export async function loadMonacoEditor() {
49-
await (setupedMonaco || (setupedMonaco = setupMonaco()));
54+
await (loadedMonaco || (loadedMonaco = loadMonaco()));
5055
return (
5156
editorLoaded ||
5257
(editorLoaded = new Promise((resolve) => {

src/parser/process-template.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ export function processTemplate(
118118
} else if (isTag(node)) {
119119
// Process for multiple tag
120120
if (parent.type === "expression") {
121-
const index = parent.children.indexOf(node);
122-
const before = parent.children[index - 1];
121+
const siblings = parent.children.filter(
122+
(n) => n.type !== "text" || n.value.trim(),
123+
);
124+
const index = siblings.indexOf(node);
125+
const before = siblings[index - 1];
123126
if (!before || !isTag(before)) {
124-
const after = parent.children[index + 1];
127+
const after = siblings[index + 1];
125128
if (after && (isTag(after) || after.type === "comment")) {
126129
const start = node.position!.start.offset;
127130
script.appendOriginal(start);
@@ -427,10 +430,13 @@ export function processTemplate(
427430
(isTag(node) || node.type === "comment") &&
428431
parent.type === "expression"
429432
) {
430-
const index = parent.children.indexOf(node);
431-
const after = parent.children[index + 1];
433+
const siblings = parent.children.filter(
434+
(n) => n.type !== "text" || n.value.trim(),
435+
);
436+
const index = siblings.indexOf(node);
437+
const after = siblings[index + 1];
432438
if (!after || (!isTag(after) && after.type !== "comment")) {
433-
const before = parent.children[index - 1];
439+
const before = siblings[index - 1];
434440
if (before && (isTag(before) || before.type === "comment")) {
435441
const end = getEndOffset(node, ctx);
436442
script.appendOriginal(end);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
[].map(i => (
3+
<p>title</p>
4+
<p>{i}</p>
5+
))
6+
}

0 commit comments

Comments
 (0)