Skip to content

Commit 8d18121

Browse files
authored
Fix bug (#16)
* Fix bug * fix * fix * fix
1 parent 1110ae0 commit 8d18121

27 files changed

+84093
-91
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"cover": "nyc --reporter=lcov npm run test",
2020
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot --timeout 60000",
2121
"preversion": "npm run lint && npm test",
22-
"update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts",
22+
"update-fixtures": "DEBUG='astro-eslint-parser' ts-node --transpile-only ./tools/update-fixtures.ts",
2323
"debug-parser": "ts-node --transpile-only ./tools/parser-test.ts",
2424
"eslint-playground": "eslint tests/fixtures --ext .astro --config .eslintrc-for-playground.js --format codeframe",
2525
"benchmark": "ts-node --transpile-only benchmark/index.ts"

src/astro/index.ts

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -95,18 +95,14 @@ export function getStartTagEndOffset(node: TagLikeNode, ctx: Context): number {
9595
/**
9696
* Get end offset of tag
9797
*/
98-
export function getTagEndOffset(
99-
node: TagLikeNode,
100-
parents: ParentNode[],
101-
ctx: Context,
102-
): number {
98+
export function getTagEndOffset(node: TagLikeNode, ctx: Context): number {
10399
if (node.position!.end?.offset != null) {
104100
return node.position!.end.offset
105101
}
106102
let beforeIndex: number
107103
if (node.children.length) {
108104
const lastChild = node.children[node.children.length - 1]
109-
beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx)
105+
beforeIndex = getEndOffset(lastChild, ctx)
110106
} else {
111107
beforeIndex = getStartTagEndOffset(node, ctx)
112108
}
@@ -125,15 +121,14 @@ export function getTagEndOffset(
125121
*/
126122
export function getExpressionEndOffset(
127123
node: ExpressionNode,
128-
parents: ParentNode[],
129124
ctx: Context,
130125
): number {
131126
if (node.position!.end?.offset != null) {
132127
return node.position!.end.offset
133128
}
134129
if (node.children.length) {
135130
const lastChild = node.children[node.children.length - 1]
136-
const beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx)
131+
const beforeIndex = getEndOffset(lastChild, ctx)
137132
const info = getTokenInfo(ctx, ["}"], beforeIndex)
138133
return info.index + info.match.length
139134
}
@@ -242,24 +237,23 @@ export function getCommentEndOffset(node: CommentNode, ctx: Context): number {
242237
/**
243238
* Get content end offset
244239
*/
245-
export function getContentEndOffset(
246-
parent: ParentNode,
247-
parents: ParentNode[],
248-
ctx: Context,
249-
): number {
240+
export function getContentEndOffset(parent: ParentNode, ctx: Context): number {
250241
const code = ctx.code
251242
if (isTag(parent)) {
252-
const end = getTagEndOffset(parent, parents, ctx)
243+
const end = getTagEndOffset(parent, ctx)
253244
if (code[end - 1] !== ">") {
254245
return end
255246
}
256-
const index = code.lastIndexOf("</", end)
257-
if (index >= 0 && code.slice(index, end).trim() === parent.name) {
247+
const index = code.lastIndexOf("</", end - 1)
248+
if (
249+
index >= 0 &&
250+
code.slice(index + 2, end - 1).trim() === parent.name
251+
) {
258252
return index
259253
}
260254
return end
261255
} else if (parent.type === "expression") {
262-
const end = getExpressionEndOffset(parent, parents, ctx)
256+
const end = getExpressionEndOffset(parent, ctx)
263257
return code.lastIndexOf("}", end)
264258
} else if (parent.type === "root") {
265259
return code.length
@@ -272,25 +266,21 @@ export function getContentEndOffset(
272266
*/
273267
export function getSelfClosingTag(
274268
node: TagLikeNode,
275-
parents: ParentNode[],
269+
parent: ParentNode,
276270
ctx: Context,
277271
): null | {
278272
offset: number
279273
end: "/>" | ">"
280274
} {
281-
const children = node.children.filter(
282-
(c) => c.type !== "text" || c.value.trim(),
283-
)
284-
if (children.length > 0) {
275+
if (node.children.length > 0) {
285276
return null
286277
}
287-
const parent = parents[0]
288278
const code = ctx.code
289279
let nextElementIndex = code.length
290280
const childIndex = parent.children.indexOf(node)
291281
if (childIndex === parent.children.length - 1) {
292282
// last
293-
nextElementIndex = getContentEndOffset(parent, parents.slice(1), ctx)
283+
nextElementIndex = getContentEndOffset(parent, ctx)
294284
} else {
295285
const next = parent.children[childIndex + 1]
296286
nextElementIndex = next.position!.start.offset
@@ -310,7 +300,6 @@ export function getSelfClosingTag(
310300
*/
311301
export function getEndTag(
312302
node: TagLikeNode,
313-
parents: ParentNode[],
314303
ctx: Context,
315304
): null | {
316305
offset: number
@@ -319,7 +308,7 @@ export function getEndTag(
319308
let beforeIndex: number
320309
if (node.children.length) {
321310
const lastChild = node.children[node.children.length - 1]
322-
beforeIndex = getEndOffset(lastChild, [node, ...parents], ctx)
311+
beforeIndex = getEndOffset(lastChild, ctx)
323312
} else {
324313
beforeIndex = getStartTagEndOffset(node, ctx)
325314
}
@@ -341,13 +330,12 @@ export function getEndTag(
341330
/**
342331
* Get end offset of tag
343332
*/
344-
function getEndOffset(node: Node, parents: ParentNode[], ctx: Context): number {
333+
function getEndOffset(node: Node, ctx: Context): number {
345334
if (node.position!.end?.offset != null) {
346335
return node.position!.end.offset
347336
}
348-
if (isTag(node)) return getTagEndOffset(node, parents, ctx)
349-
if (node.type === "expression")
350-
return getExpressionEndOffset(node, parents, ctx)
337+
if (isTag(node)) return getTagEndOffset(node, ctx)
338+
if (node.type === "expression") return getExpressionEndOffset(node, ctx)
351339
if (node.type === "comment") return getCommentEndOffset(node, ctx)
352340
if (node.type === "frontmatter") {
353341
const start = node.position!.start.offset

src/parser/astro-parser/parse.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import * as service from "./astrojs-compiler-service"
1010
import {
1111
getAttributeEndOffset,
1212
getCommentEndOffset,
13+
getSelfClosingTag,
1314
getStartTagEndOffset,
1415
skipSpaces,
1516
walk,
@@ -252,24 +253,24 @@ function fixLocations(node: ParentNode, ctx: Context): void {
252253
if (attributes[attributes.length - 1] === node) {
253254
start = getStartTagEndOffset(parent as TagLikeNode, ctx)
254255
}
255-
return
256-
}
257-
if (node.type === "expression") {
256+
} else if (node.type === "expression") {
258257
start = tokenIndex(ctx, "}", start) + 1
259258
} else if (
260259
node.type === "fragment" ||
261260
node.type === "element" ||
262261
node.type === "component" ||
263262
node.type === "custom-element"
264263
) {
265-
const closeTagStart = tokenIndexSafe(
266-
ctx.code,
267-
`</${node.name}`,
268-
start,
269-
)
270-
if (closeTagStart != null) {
271-
start = closeTagStart + 2 + node.name.length
272-
start = tokenIndex(ctx, ">", start) + 1
264+
if (!getSelfClosingTag(node, parent, ctx)) {
265+
const closeTagStart = tokenIndexSafe(
266+
ctx.code,
267+
`</${node.name}`,
268+
start,
269+
)
270+
if (closeTagStart != null) {
271+
start = closeTagStart + 2 + node.name.length
272+
start = tokenIndex(ctx, ">", start) + 1
273+
}
273274
}
274275
} else {
275276
return

src/parser/process-template.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export function processTemplate(
4848
resultTemplate.ast,
4949
ctx.code,
5050
// eslint-disable-next-line complexity -- X(
51-
(node, parents) => {
51+
(node, [parent]) => {
5252
if (node.type === "frontmatter") {
5353
const start = node.position!.start.offset
5454
script.appendOriginal(start)
@@ -87,7 +87,6 @@ export function processTemplate(
8787
script.addToken(AST_TOKEN_TYPES.Punctuator, [end - 3, end])
8888
} else if (isTag(node)) {
8989
// Process for multiple tag
90-
const parent = parents[0]
9190
if (parent.type === "expression") {
9291
const index = parent.children.indexOf(node)
9392
const before = parent.children[index - 1]
@@ -280,7 +279,7 @@ export function processTemplate(
280279
}
281280

282281
// Process for start tag close
283-
const closing = getSelfClosingTag(node, parents, ctx)
282+
const closing = getSelfClosingTag(node, parent, ctx)
284283
if (closing && closing.end === ">") {
285284
script.appendOriginal(closing.offset - 1)
286285
script.appendScript("/")
@@ -421,13 +420,13 @@ export function processTemplate(
421420
script.addToken("HTMLDocType" as AST_TOKEN_TYPES, [start, end])
422421
}
423422
},
424-
(node, parents) => {
423+
(node, [parent]) => {
425424
if (isTag(node)) {
426-
const closing = getSelfClosingTag(node, parents, ctx)
425+
const closing = getSelfClosingTag(node, parent, ctx)
427426
if (!closing) {
428-
const end = getEndTag(node, parents, ctx)
427+
const end = getEndTag(node, ctx)
429428
if (!end) {
430-
const offset = getContentEndOffset(node, parents, ctx)
429+
const offset = getContentEndOffset(node, ctx)
431430
script.appendOriginal(offset)
432431
script.appendScript(`</${node.name}>`)
433432
script.addRestoreNodeProcess(
@@ -448,7 +447,6 @@ export function processTemplate(
448447
}
449448
}
450449
// Process for multiple tag
451-
const parent = parents[0]
452450
if (
453451
(isTag(node) || node.type === "comment") &&
454452
parent.type === "expression"
@@ -462,7 +460,7 @@ export function processTemplate(
462460
(isTag(before) || before.type === "comment")
463461
) {
464462
const end = isTag(node)
465-
? getTagEndOffset(node, parents, ctx)
463+
? getTagEndOffset(node, ctx)
466464
: getCommentEndOffset(node, ctx)
467465
script.appendOriginal(end)
468466
script.appendScript("</>")

src/parser/script.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ export function parseScript(code: string, ctx: Context): ESLintExtendedProgram {
3636
debug(
3737
"[script] parsing error:",
3838
(e as any).message,
39-
`@ ${JSON.stringify(code)}`,
39+
`@ ${JSON.stringify(code)}
40+
41+
${code}`,
4042
)
4143
throw e
4244
} finally {

tests/fixtures/parser/ast/reference/directives-reference/01-common-directives-03-output.json

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,27 @@
134134
},
135135
{
136136
"type": "JSXElement",
137-
"children": [],
137+
"children": [
138+
{
139+
"type": "JSXText",
140+
"raw": "\n",
141+
"value": "\n",
142+
"range": [
143+
87,
144+
88
145+
],
146+
"loc": {
147+
"start": {
148+
"line": 4,
149+
"column": 32
150+
},
151+
"end": {
152+
"line": 5,
153+
"column": 0
154+
}
155+
}
156+
}
157+
],
138158
"closingElement": null,
139159
"openingElement": {
140160
"type": "JSXOpeningElement",
@@ -263,7 +283,7 @@
263283
}
264284
}
265285
],
266-
"selfClosing": true,
286+
"selfClosing": false,
267287
"range": [
268288
55,
269289
87
@@ -281,31 +301,12 @@
281301
},
282302
"range": [
283303
55,
284-
87
285-
],
286-
"loc": {
287-
"start": {
288-
"line": 4,
289-
"column": 0
290-
},
291-
"end": {
292-
"line": 4,
293-
"column": 32
294-
}
295-
}
296-
},
297-
{
298-
"type": "JSXText",
299-
"raw": "\n",
300-
"value": "\n",
301-
"range": [
302-
87,
303304
88
304305
],
305306
"loc": {
306307
"start": {
307308
"line": 4,
308-
"column": 32
309+
"column": 0
309310
},
310311
"end": {
311312
"line": 5,
@@ -316,7 +317,7 @@
316317
],
317318
"range": [
318319
54,
319-
88
320+
99
320321
],
321322
"loc": {
322323
"start": {
@@ -325,7 +326,7 @@
325326
},
326327
"end": {
327328
"line": 5,
328-
"column": 0
329+
"column": 11
329330
}
330331
}
331332
}
@@ -732,7 +733,7 @@
732733
],
733734
"range": [
734735
4,
735-
88
736+
99
736737
],
737738
"loc": {
738739
"start": {
@@ -741,7 +742,7 @@
741742
},
742743
"end": {
743744
"line": 5,
744-
"column": 0
745+
"column": 11
745746
}
746747
}
747748
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div>
2+
<div>
3+
<div />
4+
</div>
5+
</div>

0 commit comments

Comments
 (0)