Skip to content

Commit 1185e29

Browse files
authored
Fix wrong location in last statement (#86)
1 parent bfd9cb0 commit 1185e29

33 files changed

+9841
-65
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"eslint-plugin-prettier": "^4.0.0",
8585
"eslint-plugin-react": "^7.29.4",
8686
"eslint-plugin-regexp": "^1.5.0",
87+
"eslint-plugin-simple-import-sort": "^7.0.0",
8788
"eslint-plugin-svelte": "^2.0.0",
8889
"estree-walker": "^3.0.0",
8990
"locate-character": "^2.0.5",

src/parser/process-template.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,20 @@ export function processTemplate(
7676
script.appendOriginal(start)
7777
script.skipOriginalOffset(3)
7878
const end = getEndOffset(node, ctx)
79-
script.appendOriginal(end - 3)
79+
const scriptStart = start + 3
80+
let scriptEnd = end - 3
81+
let endChar: string
82+
while (
83+
scriptStart < scriptEnd - 1 &&
84+
(endChar = script.originalCode[scriptEnd - 1]) &&
85+
!endChar.trim()
86+
) {
87+
scriptEnd--
88+
}
89+
script.appendOriginal(scriptEnd)
8090

81-
script.appendScript(";")
82-
script.skipOriginalOffset(3)
91+
script.appendScript("\n;")
92+
script.skipOriginalOffset(end - scriptEnd)
8393

8494
script.addRestoreNodeProcess((_scriptNode, { result }) => {
8595
for (
@@ -89,7 +99,10 @@ export function processTemplate(
8999
) {
90100
const st = result.ast.body[index] as TSESTree.Node
91101
if (st.type === AST_NODE_TYPES.EmptyStatement) {
92-
if (st.range[0] === end - 3 && st.range[1] <= end) {
102+
if (
103+
st.range[0] === scriptEnd &&
104+
st.range[1] <= end
105+
) {
93106
result.ast.body.splice(index, 1)
94107
break
95108
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
import Layout from '@/layouts/page.layout.astro'
3+
import Card from '@/components/card.astro'
4+
---
5+
6+
<Layout>
7+
<Card/>
8+
</Layout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
import Card from '@/components/card.astro'
3+
import Layout from '@/layouts/page.layout.astro'
4+
---
5+
6+
<Layout>
7+
<Card/>
8+
</Layout>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[
2+
{
3+
"ruleId": "simple-import-sort/imports",
4+
"code": "import Layout from '@/layouts/page.layout.astro'\nimport Card from '@/components/card.astro'",
5+
"line": 2,
6+
"column": 1,
7+
"message": "Run autofix to sort these imports!"
8+
}
9+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* eslint eslint-comments/require-description: 0, @typescript-eslint/explicit-module-boundary-types: 0 */
2+
import type { Linter } from "eslint"
3+
import { getBasicParserOptions } from "../../../src/parser/test-utils"
4+
// @ts-expect-error -- test
5+
import { rules } from "eslint-plugin-simple-import-sort"
6+
export function setupLinter(linter: Linter) {
7+
linter.defineRule("simple-import-sort/imports", rules.imports as never)
8+
}
9+
10+
export function getConfig() {
11+
return {
12+
parser: "astro-eslint-parser",
13+
parserOptions: getBasicParserOptions(),
14+
rules: {
15+
"simple-import-sort/imports": "error",
16+
},
17+
env: {
18+
browser: true,
19+
es2021: true,
20+
},
21+
}
22+
}

tests/fixtures/parser/ast/core-concepts/astro-components/05-component-props-01-output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,16 +213,16 @@
213213
],
214214
"range": [
215215
102,
216-
141
216+
140
217217
],
218218
"loc": {
219219
"start": {
220220
"line": 4,
221221
"column": 0
222222
},
223223
"end": {
224-
"line": 5,
225-
"column": 0
224+
"line": 4,
225+
"column": 38
226226
}
227227
}
228228
},

tests/fixtures/parser/ast/core-concepts/astro-components/05-component-props-03-output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,16 @@
136136
],
137137
"range": [
138138
98,
139-
119
139+
118
140140
],
141141
"loc": {
142142
"start": {
143143
"line": 4,
144144
"column": 0
145145
},
146146
"end": {
147-
"line": 5,
148-
"column": 0
147+
"line": 4,
148+
"column": 20
149149
}
150150
}
151151
},

tests/fixtures/parser/ast/core-concepts/astro-components/06-slots-01-output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,16 @@
378378
],
379379
"range": [
380380
112,
381-
141
381+
140
382382
],
383383
"loc": {
384384
"start": {
385385
"line": 6,
386386
"column": 0
387387
},
388388
"end": {
389-
"line": 7,
390-
"column": 0
389+
"line": 6,
390+
"column": 28
391391
}
392392
}
393393
},

tests/fixtures/parser/ast/core-concepts/astro-components/06-slots-03-output.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,16 +378,16 @@
378378
],
379379
"range": [
380380
112,
381-
141
381+
140
382382
],
383383
"loc": {
384384
"start": {
385385
"line": 6,
386386
"column": 0
387387
},
388388
"end": {
389-
"line": 7,
390-
"column": 0
389+
"line": 6,
390+
"column": 28
391391
}
392392
}
393393
},

0 commit comments

Comments
 (0)