|
|
@ -1,11 +1,13 @@
|
|
|
|
import jsdom from "jsdom";
|
|
|
|
import jsdom from 'jsdom';
|
|
|
|
import assert from "assert";
|
|
|
|
import assert from 'assert';
|
|
|
|
import * as fs from "fs";
|
|
|
|
import glob from 'glob';
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
import chalk from 'chalk';
|
|
|
|
|
|
|
|
|
|
|
|
import * as consoleGroup from "console-group";
|
|
|
|
import * as consoleGroup from 'console-group';
|
|
|
|
consoleGroup.install();
|
|
|
|
consoleGroup.install();
|
|
|
|
|
|
|
|
|
|
|
|
import * as sourceMapSupport from "source-map-support";
|
|
|
|
import * as sourceMapSupport from 'source-map-support';
|
|
|
|
sourceMapSupport.install();
|
|
|
|
sourceMapSupport.install();
|
|
|
|
|
|
|
|
|
|
|
|
// for coverage purposes, we need to test source files,
|
|
|
|
// for coverage purposes, we need to test source files,
|
|
|
@ -14,8 +16,8 @@ export function loadSvelte(test) {
|
|
|
|
if (test) global.__svelte_test = true;
|
|
|
|
if (test) global.__svelte_test = true;
|
|
|
|
|
|
|
|
|
|
|
|
const resolved = process.env.COVERAGE
|
|
|
|
const resolved = process.env.COVERAGE
|
|
|
|
? require.resolve("../src/index.js")
|
|
|
|
? require.resolve('../src/index.js')
|
|
|
|
: require.resolve("../compiler/svelte.js");
|
|
|
|
: require.resolve('../compiler/svelte.js');
|
|
|
|
|
|
|
|
|
|
|
|
delete require.cache[resolved];
|
|
|
|
delete require.cache[resolved];
|
|
|
|
return require(resolved);
|
|
|
|
return require(resolved);
|
|
|
@ -36,23 +38,23 @@ export function tryToLoadJson(file) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return JSON.parse(fs.readFileSync(file));
|
|
|
|
return JSON.parse(fs.readFileSync(file));
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "ENOENT") throw err;
|
|
|
|
if (err.code !== 'ENOENT') throw err;
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function tryToReadFile(file) {
|
|
|
|
export function tryToReadFile(file) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return fs.readFileSync(file, "utf-8");
|
|
|
|
return fs.readFileSync(file, 'utf-8');
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code !== "ENOENT") throw err;
|
|
|
|
if (err.code !== 'ENOENT') throw err;
|
|
|
|
return null;
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function env() {
|
|
|
|
export function env() {
|
|
|
|
return new Promise((fulfil, reject) => {
|
|
|
|
return new Promise((fulfil, reject) => {
|
|
|
|
jsdom.env("<main></main>", (err, window) => {
|
|
|
|
jsdom.env('<main></main>', (err, window) => {
|
|
|
|
if (err) {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -75,19 +77,19 @@ function cleanChildren(node) {
|
|
|
|
|
|
|
|
|
|
|
|
if (child.nodeType === 3) {
|
|
|
|
if (child.nodeType === 3) {
|
|
|
|
if (
|
|
|
|
if (
|
|
|
|
node.namespaceURI === "http://www.w3.org/2000/svg" &&
|
|
|
|
node.namespaceURI === 'http://www.w3.org/2000/svg' &&
|
|
|
|
node.tagName !== "text" &&
|
|
|
|
node.tagName !== 'text' &&
|
|
|
|
node.tagName !== "tspan"
|
|
|
|
node.tagName !== 'tspan'
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
node.removeChild(child);
|
|
|
|
node.removeChild(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
child.data = child.data.replace(/\s{2,}/, "\n");
|
|
|
|
child.data = child.data.replace(/\s{2,}/, '\n');
|
|
|
|
|
|
|
|
|
|
|
|
// text
|
|
|
|
// text
|
|
|
|
if (previous && previous.nodeType === 3) {
|
|
|
|
if (previous && previous.nodeType === 3) {
|
|
|
|
previous.data += child.data;
|
|
|
|
previous.data += child.data;
|
|
|
|
previous.data = previous.data.replace(/\s{2,}/, "\n");
|
|
|
|
previous.data = previous.data.replace(/\s{2,}/, '\n');
|
|
|
|
|
|
|
|
|
|
|
|
node.removeChild(child);
|
|
|
|
node.removeChild(child);
|
|
|
|
child = previous;
|
|
|
|
child = previous;
|
|
|
@ -101,12 +103,12 @@ function cleanChildren(node) {
|
|
|
|
|
|
|
|
|
|
|
|
// collapse whitespace
|
|
|
|
// collapse whitespace
|
|
|
|
if (node.firstChild && node.firstChild.nodeType === 3) {
|
|
|
|
if (node.firstChild && node.firstChild.nodeType === 3) {
|
|
|
|
node.firstChild.data = node.firstChild.data.replace(/^\s+/, "");
|
|
|
|
node.firstChild.data = node.firstChild.data.replace(/^\s+/, '');
|
|
|
|
if (!node.firstChild.data) node.removeChild(node.firstChild);
|
|
|
|
if (!node.firstChild.data) node.removeChild(node.firstChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (node.lastChild && node.lastChild.nodeType === 3) {
|
|
|
|
if (node.lastChild && node.lastChild.nodeType === 3) {
|
|
|
|
node.lastChild.data = node.lastChild.data.replace(/\s+$/, "");
|
|
|
|
node.lastChild.data = node.lastChild.data.replace(/\s+$/, '');
|
|
|
|
if (!node.lastChild.data) node.removeChild(node.lastChild);
|
|
|
|
if (!node.lastChild.data) node.removeChild(node.lastChild);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -115,15 +117,15 @@ export function setupHtmlEqual() {
|
|
|
|
return env().then(window => {
|
|
|
|
return env().then(window => {
|
|
|
|
assert.htmlEqual = (actual, expected, message) => {
|
|
|
|
assert.htmlEqual = (actual, expected, message) => {
|
|
|
|
window.document.body.innerHTML = actual
|
|
|
|
window.document.body.innerHTML = actual
|
|
|
|
.replace(/>[\s\r\n]+</g, "><")
|
|
|
|
.replace(/>[\s\r\n]+</g, '><')
|
|
|
|
.trim();
|
|
|
|
.trim();
|
|
|
|
cleanChildren(window.document.body, "");
|
|
|
|
cleanChildren(window.document.body, '');
|
|
|
|
actual = window.document.body.innerHTML;
|
|
|
|
actual = window.document.body.innerHTML;
|
|
|
|
|
|
|
|
|
|
|
|
window.document.body.innerHTML = expected
|
|
|
|
window.document.body.innerHTML = expected
|
|
|
|
.replace(/>[\s\r\n]+</g, "><")
|
|
|
|
.replace(/>[\s\r\n]+</g, '><')
|
|
|
|
.trim();
|
|
|
|
.trim();
|
|
|
|
cleanChildren(window.document.body, "");
|
|
|
|
cleanChildren(window.document.body, '');
|
|
|
|
expected = window.document.body.innerHTML;
|
|
|
|
expected = window.document.body.innerHTML;
|
|
|
|
|
|
|
|
|
|
|
|
assert.deepEqual(actual, expected, message);
|
|
|
|
assert.deepEqual(actual, expected, message);
|
|
|
@ -137,7 +139,7 @@ export function loadConfig(file) {
|
|
|
|
delete require.cache[resolved];
|
|
|
|
delete require.cache[resolved];
|
|
|
|
return require(resolved).default;
|
|
|
|
return require(resolved).default;
|
|
|
|
} catch (err) {
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === "E_NOT_FOUND") {
|
|
|
|
if (err.code === 'E_NOT_FOUND') {
|
|
|
|
return {};
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -147,16 +149,32 @@ export function loadConfig(file) {
|
|
|
|
|
|
|
|
|
|
|
|
export function addLineNumbers(code) {
|
|
|
|
export function addLineNumbers(code) {
|
|
|
|
return code
|
|
|
|
return code
|
|
|
|
.split("\n")
|
|
|
|
.split('\n')
|
|
|
|
.map((line, i) => {
|
|
|
|
.map((line, i) => {
|
|
|
|
i = String(i + 1);
|
|
|
|
i = String(i + 1);
|
|
|
|
while (i.length < 3) i = ` ${i}`;
|
|
|
|
while (i.length < 3) i = ` ${i}`;
|
|
|
|
|
|
|
|
|
|
|
|
return `${i}: ${line.replace(/^\t+/, match =>
|
|
|
|
return (
|
|
|
|
match.split("\t").join(" ")
|
|
|
|
chalk.grey(` ${i}: `) +
|
|
|
|
)}`;
|
|
|
|
line.replace(/^\t+/, match => match.split('\t').join(' '))
|
|
|
|
|
|
|
|
);
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.join("\n");
|
|
|
|
.join('\n');
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function showOutput(cwd, shared) {
|
|
|
|
|
|
|
|
glob.sync('**/*.html', { cwd }).forEach(file => {
|
|
|
|
|
|
|
|
const { code } = svelte.compile(
|
|
|
|
|
|
|
|
fs.readFileSync(`${cwd}/${file}`, 'utf-8'),
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
shared
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
|
|
|
`\n>> ${chalk.cyan.bold(file)}\n${addLineNumbers(code)}\n<< ${chalk.cyan.bold(file)}`
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const start = /\n(\t+)/;
|
|
|
|
const start = /\n(\t+)/;
|
|
|
|