Skip to content

Commit 4d4c06a

Browse files
authored
Merge pull request #22 from ieedan/better-formatting
fix: improve formatting rules
2 parents 1a3a5c7 + cd2f9c9 commit 4d4c06a

File tree

5 files changed

+43
-19
lines changed

5 files changed

+43
-19
lines changed

.changeset/cyan-bats-peel.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"sv-strip": patch
3+
---
4+
5+
fix: Improve formatting rules during removal to prevent unneccessary new lines at the end of script tags.

package.json

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,7 @@
1414
"bugs": {
1515
"url": "https://github.com/ieedan/sv-strip/issues"
1616
},
17-
"keywords": [
18-
"svelte",
19-
"strip",
20-
"types",
21-
"typescript",
22-
"javascript"
23-
],
17+
"keywords": ["svelte", "strip", "types", "typescript", "javascript"],
2418
"packageManager": "[email protected]",
2519
"type": "module",
2620
"scripts": {
@@ -32,9 +26,7 @@
3226
"changeset": "changeset",
3327
"ci:release": "unbuild && changeset publish"
3428
},
35-
"files": [
36-
"dist"
37-
],
29+
"files": ["dist"],
3830
"exports": {
3931
".": {
4032
"import": "./dist/index.mjs",

src/index.ts

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -220,26 +220,43 @@ function removeNode(src: MagicString, start: number, end: number, format: boolea
220220
let newEnd = end;
221221

222222
if (format) {
223-
newStart = 0;
224-
225-
// remove whitespace proceeding the node until the next newline / none whitespace character
226-
for (let i = start - 1; i > -1; i--) {
227-
if (/\S|\n/.test(src.original[i])) {
228-
newStart = i + 1;
229-
break;
230-
}
231-
}
223+
let isLast = false;
232224

233225
// remove whitespace beyond the node until the proceeding whitespace of the next node
234226
for (let i = end; i < src.original.length; i++) {
235227
if (/\S/.test(src.original[i])) {
228+
if (src.original[i] === '<' && src.original.slice(i).startsWith('</script')) {
229+
isLast = true;
230+
// back up 1 character so that the last node isn't on the same line as the script
231+
newEnd -= 1;
232+
}
236233
break;
237234
}
238235

239236
if (src.original[i] === '\n') {
240237
newEnd = i + 1;
241238
}
242239
}
240+
241+
newStart = 0;
242+
243+
// remove whitespace proceeding the node until the next newline / none whitespace character
244+
for (let i = start - 1; i > -1; i--) {
245+
let regex: RegExp;
246+
247+
// if we are last then we get rid of all whitespace trailing the previous node
248+
if (isLast) {
249+
regex = new RegExp(/\S/);
250+
} else {
251+
// else we only remove to the new line
252+
regex = new RegExp(/\S|\n/);
253+
}
254+
255+
if (regex.test(src.original[i])) {
256+
newStart = i + 1;
257+
break;
258+
}
259+
}
243260
}
244261

245262
src.update(newStart, newEnd, '');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script module>
2+
export const foo = 5;
3+
</script>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script lang="ts" module>
2+
type Foo = number;
3+
4+
export const foo: Foo = 5;
5+
6+
type Bar = string;
7+
</script>

0 commit comments

Comments
 (0)