Skip to content

Commit 7c1e6a6

Browse files
halfnelsonmilahu
andauthored
refactor sourcemap and preprocessor tests (#5583)
Co-authored-by: Milan Hauth <[email protected]>
1 parent 65104e8 commit 7c1e6a6

File tree

14 files changed

+133
-89
lines changed

14 files changed

+133
-89
lines changed

.gitignore

-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@ node_modules
1818
/coverage/
1919
/coverage.lcov
2020
/test/*/samples/_
21-
/test/sourcemaps/samples/*/output.js
22-
/test/sourcemaps/samples/*/output.js.map
23-
/test/sourcemaps/samples/*/output.css
24-
/test/sourcemaps/samples/*/output.css.map
2521
/yarn-error.log
2622
_actual*.*
2723
_output

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"posttest": "agadoo internal/index.mjs",
3838
"prepublishOnly": "npm run lint && PUBLISH=true npm test",
3939
"tsd": "tsc -p src/compiler --emitDeclarationOnly && tsc -p src/runtime --emitDeclarationOnly",
40-
"lint": "eslint '{src,test}/**/*.{ts,js}'"
40+
"lint": "eslint \"{src,test}/**/*.{ts,js}\""
4141
},
4242
"repository": {
4343
"type": "git",

test/preprocess/index.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ describe('preprocess', () => {
88

99
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
1010
const solo = config.solo || /\.solo/.test(dir);
11+
const skip = config.skip || /\.skip/.test(dir);
1112

1213
if (solo && process.env.CI) {
1314
throw new Error('Forgot to remove `solo: true` from test');
1415
}
1516

16-
(config.skip ? it.skip : solo ? it.only : it)(dir, async () => {
17+
(skip ? it.skip : solo ? it.only : it)(dir, async () => {
1718
const input = fs.readFileSync(`${__dirname}/samples/${dir}/input.svelte`, 'utf-8');
1819
const expected = fs.readFileSync(`${__dirname}/samples/${dir}/output.svelte`, 'utf-8');
1920

20-
const result = await svelte.preprocess(input, config.preprocess);
21+
const result = await svelte.preprocess(
22+
input,
23+
config.preprocess || {},
24+
config.options || { filename: 'input.svelte' }
25+
);
2126
fs.writeFileSync(`${__dirname}/samples/${dir}/_actual.html`, result.code);
2227

2328
assert.equal(result.code, expected);

test/preprocess/samples/filename/_config.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export default {
22
preprocess: {
3-
filename: 'file.svelte',
43
markup: ({ content, filename }) => {
54
return {
65
code: content.replace('__MARKUP_FILENAME__', filename)
@@ -16,5 +15,8 @@ export default {
1615
code: content.replace('__SCRIPT_FILENAME__', filename)
1716
};
1817
}
18+
},
19+
options: {
20+
filename: 'file.svelte'
1921
}
2022
};

test/sourcemaps/index.ts

+74-34
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,113 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
33
import * as assert from 'assert';
4-
import { svelte } from '../helpers';
5-
import { SourceMapConsumer } from 'source-map';
4+
import { loadConfig, svelte } from '../helpers';
5+
// keep source-map at version 0.7.x
6+
// https://github.com/mozilla/source-map/issues/400
67
import { getLocator } from 'locate-character';
8+
import { SourceMapConsumer } from 'source-map';
9+
710

811
describe('sourcemaps', () => {
912
fs.readdirSync(`${__dirname}/samples`).forEach(dir => {
1013
if (dir[0] === '.') return;
1114

15+
const config = loadConfig(`${__dirname}/samples/${dir}/_config.js`);
16+
1217
// add .solo to a sample directory name to only run that test
13-
const solo = /\.solo/.test(dir);
14-
const skip = /\.skip/.test(dir);
18+
const solo = config.solo || /\.solo/.test(dir);
19+
const skip = config.skip || /\.skip/.test(dir);
1520

1621
if (solo && process.env.CI) {
1722
throw new Error('Forgot to remove `solo: true` from test');
1823
}
1924

2025
(solo ? it.only : skip ? it.skip : it)(dir, async () => {
21-
const filename = path.resolve(
22-
`${__dirname}/samples/${dir}/input.svelte`
23-
);
24-
const outputFilename = path.resolve(
25-
`${__dirname}/samples/${dir}/output`
26-
);
26+
const { test } = require(`./samples/${dir}/test.js`);
27+
const inputFile = path.resolve(`${__dirname}/samples/${dir}/input.svelte`);
28+
const outputName = '_actual';
29+
const outputBase = path.resolve(`${__dirname}/samples/${dir}/${outputName}`);
30+
31+
const inputCode = fs.readFileSync(inputFile, 'utf-8');
32+
const input = {
33+
code: inputCode,
34+
locate: getLocator(inputCode)
35+
};
2736

28-
const input = fs.readFileSync(filename, 'utf-8').replace(/\s+$/, '');
29-
const { js, css } = svelte.compile(input, {
30-
filename,
31-
outputFilename: `${outputFilename}.js`,
32-
cssOutputFilename: `${outputFilename}.css`
37+
const preprocessed = await svelte.preprocess(
38+
input.code,
39+
config.preprocess || {},
40+
{
41+
filename: 'input.svelte'
42+
}
43+
);
44+
45+
const { js, css } = svelte.compile(
46+
preprocessed.code, {
47+
filename: 'input.svelte',
48+
// filenames for sourcemaps
49+
outputFilename: `${outputName}.js`,
50+
cssOutputFilename: `${outputName}.css`
3351
});
3452

35-
const _code = js.code.replace(/Svelte v\d+\.\d+\.\d+/, match => match.replace(/\d/g, 'x'));
53+
js.code = js.code.replace(
54+
/generated by Svelte v\d+\.\d+\.\d+/,
55+
match => match.replace(/\d/g, 'x')
56+
);
3657

58+
fs.writeFileSync(`${outputBase}.svelte`, preprocessed.code);
59+
if (preprocessed.map) {
60+
fs.writeFileSync(
61+
`${outputBase}.svelte.map`,
62+
// TODO encode mappings for output - svelte.preprocess returns decoded mappings
63+
JSON.stringify(preprocessed.map, null, 2)
64+
);
65+
}
3766
fs.writeFileSync(
38-
`${outputFilename}.js`,
39-
`${_code}\n//# sourceMappingURL=output.js.map`
67+
`${outputBase}.js`,
68+
`${js.code}\n//# sourceMappingURL=${outputName}.js.map`
4069
);
4170
fs.writeFileSync(
42-
`${outputFilename}.js.map`,
43-
JSON.stringify(js.map, null, ' ')
71+
`${outputBase}.js.map`,
72+
JSON.stringify(js.map, null, 2)
4473
);
45-
4674
if (css.code) {
4775
fs.writeFileSync(
48-
`${outputFilename}.css`,
49-
`${css.code}\n/*# sourceMappingURL=output.css.map */`
76+
`${outputBase}.css`,
77+
`${css.code}\n/*# sourceMappingURL=${outputName}.css.map */`
5078
);
5179
fs.writeFileSync(
52-
`${outputFilename}.css.map`,
80+
`${outputBase}.css.map`,
5381
JSON.stringify(css.map, null, ' ')
5482
);
5583
}
5684

57-
assert.deepEqual(js.map.sources, ['input.svelte']);
58-
if (css.map) assert.deepEqual(css.map.sources, ['input.svelte']);
59-
60-
const { test } = require(`./samples/${dir}/test.js`);
85+
assert.deepEqual(
86+
js.map.sources.slice().sort(),
87+
(config.js_map_sources || ['input.svelte']).sort()
88+
);
89+
if (css.map) {
90+
assert.deepEqual(
91+
css.map.sources.slice().sort(),
92+
(config.css_map_sources || ['input.svelte']).sort()
93+
);
94+
}
6195

62-
const locateInSource = getLocator(input);
96+
// use locate_1 with mapConsumer:
97+
// lines are one-based, columns are zero-based
6398

64-
const smc = await new SourceMapConsumer(js.map);
65-
const locateInGenerated = getLocator(_code);
99+
preprocessed.mapConsumer = preprocessed.map && await new SourceMapConsumer(preprocessed.map);
100+
preprocessed.locate = getLocator(preprocessed.code);
101+
preprocessed.locate_1 = getLocator(preprocessed.code, { offsetLine: 1 });
66102

67-
const smcCss = css.map && await new SourceMapConsumer(css.map);
68-
const locateInGeneratedCss = getLocator(css.code || '');
103+
js.mapConsumer = js.map && await new SourceMapConsumer(js.map);
104+
js.locate = getLocator(js.code);
105+
js.locate_1 = getLocator(js.code, { offsetLine: 1 });
69106

70-
test({ assert, code: _code, map: js.map, smc, smcCss, locateInSource, locateInGenerated, locateInGeneratedCss });
107+
css.mapConsumer = css.map && await new SourceMapConsumer(css.map);
108+
css.locate = getLocator(css.code || '');
109+
css.locate_1 = getLocator(css.code || '', { offsetLine: 1 });
110+
test({ assert, input, preprocessed, js, css });
71111
});
72112
});
73113
});

test/sourcemaps/samples/basic/test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource('foo.bar.baz');
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate('foo.bar.baz');
33

44
let start;
55
let actual;
66

7-
start = locateInGenerated('ctx[0].bar.baz');
7+
start = js.locate('ctx[0].bar.baz');
88

9-
actual = smc.originalPositionFor({
9+
actual = js.mapConsumer.originalPositionFor({
1010
line: start.line + 1,
1111
column: start.column
1212
});
@@ -18,9 +18,9 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
1818
column: expected.column
1919
});
2020

21-
start = locateInGenerated('ctx[0].bar.baz', start.character + 1);
21+
start = js.locate('ctx[0].bar.baz', start.character + 1);
2222

23-
actual = smc.originalPositionFor({
23+
actual = js.mapConsumer.originalPositionFor({
2424
line: start.line + 1,
2525
column: start.column
2626
});

test/sourcemaps/samples/binding-shorthand.skip/test.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource('potato');
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate('potato');
33

44
let start;
55

6-
start = locateInGenerated('potato');
7-
start = locateInGenerated('potato', start.character + 1);
8-
start = locateInGenerated('potato', start.character + 1); // we need the third instance of 'potato'
6+
start = js.locate('potato');
7+
start = js.locate('potato', start.character + 1);
8+
start = js.locate('potato', start.character + 1);
9+
// we need the third instance of 'potato'
910

10-
const actual = smc.originalPositionFor({
11+
const actual = js.mapConsumer.originalPositionFor({
1112
line: start.line + 1,
1213
column: start.column
1314
});

test/sourcemaps/samples/binding/test.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource('bar.baz');
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate('bar.baz');
33

44
let start;
55
let actual;
66

7-
start = locateInGenerated('bar.baz');
7+
start = js.locate('bar.baz');
88

9-
actual = smc.originalPositionFor({
9+
actual = js.mapConsumer.originalPositionFor({
1010
line: start.line + 1,
1111
column: start.column
1212
});
@@ -18,9 +18,9 @@ export function test({ assert, smc, locateInSource, locateInGenerated }) {
1818
column: expected.column
1919
});
2020

21-
start = locateInGenerated('bar.baz', start.character + 1);
21+
start = js.locate('bar.baz', start.character + 1);
2222

23-
actual = smc.originalPositionFor({
23+
actual = js.mapConsumer.originalPositionFor({
2424
line: start.line + 1,
2525
column: start.column
2626
});

test/sourcemaps/samples/css/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
export function test({ assert, smcCss, locateInSource, locateInGeneratedCss }) {
2-
const expected = locateInSource( '.foo' );
1+
export function test({ assert, input, css }) {
2+
const expected = input.locate('.foo');
33

4-
const start = locateInGeneratedCss( '.foo' );
4+
const start = css.locate('.foo');
55

6-
const actual = smcCss.originalPositionFor({
6+
const actual = css.mapConsumer.originalPositionFor({
77
line: start.line + 1,
88
column: start.column
99
});
1010

11-
assert.deepEqual( actual, {
11+
assert.deepEqual(actual, {
1212
source: 'input.svelte',
1313
name: null,
1414
line: expected.line + 1,

test/sourcemaps/samples/each-block/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
export function test({ assert, code, smc, map, locateInSource, locateInGenerated }) {
2-
const startIndex = code.indexOf('create_main_fragment');
1+
export function test({ assert, input, js }) {
2+
const startIndex = js.code.indexOf('create_main_fragment');
33

4-
const expected = locateInSource('each');
5-
const start = locateInGenerated('length', startIndex);
4+
const expected = input.locate('each');
5+
const start = js.locate('length', startIndex);
66

7-
const actual = smc.originalPositionFor({
7+
const actual = js.mapConsumer.originalPositionFor({
88
line: start.line + 1,
99
column: start.column
1010
});

test/sourcemaps/samples/script-after-comment/test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource( 'assertThisLine' );
3-
const start = locateInGenerated( 'assertThisLine' );
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate('assertThisLine');
3+
const start = js.locate('assertThisLine');
44

5-
const actual = smc.originalPositionFor({
5+
const actual = js.mapConsumer.originalPositionFor({
66
line: start.line + 1,
77
column: start.column
88
});
99

10-
assert.deepEqual( actual, {
10+
assert.deepEqual(actual, {
1111
source: 'input.svelte',
1212
name: null,
1313
line: expected.line + 1,

test/sourcemaps/samples/script/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource( '42' );
3-
const start = locateInGenerated( '42' );
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate('42');
3+
const start = js.locate('42');
44

5-
const actual = smc.originalPositionFor({
5+
const actual = js.mapConsumer.originalPositionFor({
66
line: start.line + 1,
77
column: start.column
88
});
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const fs = require( 'fs' );
2-
const path = require( 'path' );
1+
const fs = require('fs');
2+
const path = require('path');
33

4-
export function test({ assert, map }) {
5-
assert.deepEqual( map.sources, [ 'input.svelte' ]);
6-
assert.deepEqual( map.sourcesContent, [
7-
fs.readFileSync( path.join( __dirname, 'input.svelte' ), 'utf-8' )
4+
export function test({ assert, js }) {
5+
assert.deepEqual(js.map.sources, ['input.svelte']);
6+
assert.deepEqual(js.map.sourcesContent, [
7+
fs.readFileSync(path.join(__dirname, 'input.svelte'), 'utf-8')
88
]);
99
}

test/sourcemaps/samples/two-scripts/test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
export function test({ assert, smc, locateInSource, locateInGenerated }) {
2-
const expected = locateInSource( 'assertThisLine' );
3-
const start = locateInGenerated( 'assertThisLine' );
1+
export function test({ assert, input, js }) {
2+
const expected = input.locate( 'assertThisLine' );
3+
const start = js.locate( 'assertThisLine' );
44

5-
const actual = smc.originalPositionFor({
5+
const actual = js.mapConsumer.originalPositionFor({
66
line: start.line + 1,
77
column: start.column
88
});

0 commit comments

Comments
 (0)