mirror of https://github.com/sveltejs/svelte
parent
880f5567da
commit
2cf1ae6ad3
@ -0,0 +1,50 @@
|
||||
import MagicString from 'magic-string';
|
||||
|
||||
function replace(search, replace, content, src, options = { storeName: true }) {
|
||||
let idx = -1;
|
||||
while ((idx = content.indexOf(search, idx + 1)) != -1) {
|
||||
src.overwrite(idx, idx + search.length, replace, options);
|
||||
}
|
||||
}
|
||||
|
||||
function result(src, filename) {
|
||||
return {
|
||||
code: src.toString(),
|
||||
map: src.generateMap({
|
||||
source: filename,
|
||||
hires: true,
|
||||
includeContent: false
|
||||
})
|
||||
};
|
||||
}
|
||||
|
||||
export default {
|
||||
preprocess: [
|
||||
{
|
||||
markup: ({ content, filename }) => {
|
||||
const src = new MagicString(content);
|
||||
replace('baritone', 'bar', content, src);
|
||||
replace('--bazitone', '--baz', content, src);
|
||||
replace('old_name_1', 'temp_new_name_1', content, src);
|
||||
replace('old_name_2', 'temp_new_name_2', content, src);
|
||||
return result(src, filename);
|
||||
}
|
||||
},
|
||||
{
|
||||
markup: ({ content, filename }) => {
|
||||
const src = new MagicString(content);
|
||||
replace('temp_new_name_1', 'temp_temp_new_name_1', content, src);
|
||||
replace('temp_new_name_2', 'temp_temp_new_name_2', content, src);
|
||||
return result(src, filename);
|
||||
}
|
||||
},
|
||||
{
|
||||
markup: ({ content, filename }) => {
|
||||
const src = new MagicString(content);
|
||||
replace('temp_temp_new_name_1', 'new_name_1', content, src);
|
||||
replace('temp_temp_new_name_2', 'new_name_2', content, src);
|
||||
return result(src, filename);
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
@ -0,0 +1,12 @@
|
||||
<script>
|
||||
export let old_name_1 = { baritone: 5 };
|
||||
let old_name_2 = 'value_2';
|
||||
</script>
|
||||
<style>
|
||||
div {
|
||||
background-color: var(--bazitone);
|
||||
}
|
||||
</style>
|
||||
<h1>use-names</h1>
|
||||
<div>{old_name_1.baritone}</div>
|
||||
<pre style="color: var(--bazitone)">{old_name_2}</pre>
|
@ -0,0 +1,42 @@
|
||||
// needed for workaround, TODO remove
|
||||
import { getLocator } from 'locate-character';
|
||||
|
||||
export function test({ assert, input, preprocessed, js, css }) {
|
||||
|
||||
assert.deepEqual(
|
||||
preprocessed.map.names.sort(),
|
||||
['baritone', '--bazitone', 'old_name_1', 'old_name_2'].sort()
|
||||
);
|
||||
|
||||
function test_name(old_name, new_name, where) {
|
||||
|
||||
let loc = { character: -1 };
|
||||
while (loc = where.locate(new_name, loc.character + 1)) {
|
||||
const actualMapping = where.mapConsumer.originalPositionFor({
|
||||
line: loc.line + 1, column: loc.column
|
||||
});
|
||||
if (actualMapping.line === null) {
|
||||
// location is not mapped - ignore
|
||||
continue;
|
||||
}
|
||||
assert.equal(actualMapping.name, old_name);
|
||||
}
|
||||
if (loc === undefined) {
|
||||
// workaround for bug in locate-character, TODO remove
|
||||
// https://github.com/Rich-Harris/locate-character/pull/5
|
||||
where.locate = getLocator(where.code);
|
||||
}
|
||||
}
|
||||
|
||||
test_name('baritone', 'bar', js);
|
||||
test_name('baritone', 'bar', preprocessed);
|
||||
|
||||
test_name('--bazitone', '--baz', css);
|
||||
test_name('--bazitone', '--baz', preprocessed);
|
||||
|
||||
test_name('old_name_1', 'new_name_1', js);
|
||||
test_name('old_name_1', 'new_name_1', preprocessed);
|
||||
|
||||
test_name('old_name_2', 'new_name_2', js);
|
||||
test_name('old_name_2', 'new_name_2', preprocessed);
|
||||
}
|
Loading…
Reference in new issue