Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) adjust snipping spans #256

Merged
merged 2 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.5.0 (Unreleased)

* (feat) Support `bracketSameLine` option and deprecate `svelteBracketNewLine` ([#251](https://github.com/sveltejs/prettier-plugin-svelte/pull/251))
* (fix) Prevent script/style contents from being erased in certain cases ([#255](https://github.com/sveltejs/prettier-plugin-svelte/issues/255))

## 2.4.0

Expand Down
20 changes: 18 additions & 2 deletions src/lib/snipTagContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export function snipScriptAndStyleTagContent(source: string): string {
const indexes: [number, number][] = [];
let match = null;
while ((match = regex.exec(source)) != null) {
indexes.push([match.index, regex.lastIndex]);
if (!source.substr(match.index, 10).startsWith('<!--')) {
indexes.push([match.index, regex.lastIndex]);
}
}
return indexes;
}
Expand All @@ -27,18 +29,32 @@ export function snipScriptAndStyleTagContent(source: string): string {
placeholder: string,
otherSpans: [number, number][],
) {
// Replace valid matches
const regex = getRegexp(tagName);
return _source.replace(regex, (match, attributes, content, index) => {
const newSource = _source.replace(regex, (match, attributes, content, index) => {
if (match.startsWith('<!--') || withinOtherSpan(index)) {
return match;
}
const encodedContent = Buffer.from(content).toString('base64');
return `<${tagName}${attributes} ${snippedTagContentAttribute}="${encodedContent}">${placeholder}</${tagName}>`;
});

// Adjust the spans because the source now has a different content length
adjustSpans(scriptMatchSpans);
adjustSpans(styleMatchSpans);

return newSource;

function withinOtherSpan(idx: number) {
return otherSpans.some((otherSpan) => idx > otherSpan[0] && idx < otherSpan[1]);
}
function adjustSpans(spans: [number, number][]) {
const lengthDiff = _source.length - newSource.length;
spans.forEach((span) => {
span[0] -= lengthDiff;
span[1] -= lengthDiff;
});
}
}

function getRegexp(tagName: string) {
Expand Down
13 changes: 13 additions & 0 deletions test/printer/samples/style-scripts-snipping-spans.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script lang="ts" context="module">
// we need a certain length here
</script>

<script lang="ts">
// we need a certain length here
</script>

<style type="scss">
/* we need a certain length here */
</style>

<!-- this test checks that the matching spans are adjusted after replacing the code, as otherwise it would mess up the snipping -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelteSortOrder": "options-scripts-styles-markup"
}