Skip to content

Commit

Permalink
Reinstate code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Nov 23, 2017
1 parent 5190144 commit d590dbe
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 65 deletions.
7 changes: 4 additions & 3 deletions mocha.coverage.opts
@@ -1,4 +1,5 @@
--require babel-register
--compilers ts-node/register
--require source-map-support/register
--full-trace
--recursive
./**/__test__.js
test/*/index.js
test/test.js
13 changes: 7 additions & 6 deletions package.json
Expand Up @@ -61,27 +61,28 @@
"node-resolve": "^1.3.3",
"nyc": "^11.1.0",
"prettier": "^1.7.0",
"reify": "^0.12.3",
"rollup": "^0.48.2",
"rollup-plugin-buble": "^0.15.0",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-json": "^2.1.0",
"rollup-plugin-node-resolve": "^3.0.0",
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-typescript": "^0.8.1",
"rollup-plugin-virtual": "^1.0.1",
"rollup-watch": "^4.3.1",
"source-map": "^0.5.6",
"source-map-support": "^0.4.8",
"ts-node": "^3.3.0",
"tslib": "^1.8.0",
"typescript": "^2.3.2"
"typescript": "^2.6.1"
},
"nyc": {
"include": [
"src/**/*.js",
"compiler/svelte.js",
"shared.js"
],
"exclude": [
"src/**/__test__.js",
"src/shared/**"
]
"sourceMap": true,
"instrument": true
}
}
5 changes: 5 additions & 0 deletions rollup.config.js
@@ -1,9 +1,11 @@
import path from 'path';
import replace from 'rollup-plugin-replace';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import json from 'rollup-plugin-json';
import typescript from 'rollup-plugin-typescript';
import buble from 'rollup-plugin-buble';
import pkg from './package.json';

const src = path.resolve('src');

Expand All @@ -27,6 +29,9 @@ export default [
}
}
},
replace({
__VERSION__: pkg.version
}),
resolve(),
commonjs(),
json(),
Expand Down
1 change: 1 addition & 0 deletions src/config.ts
@@ -0,0 +1 @@
export const test = typeof process !== 'undefined' && process.env.TEST;
2 changes: 1 addition & 1 deletion src/css/Stylesheet.ts
Expand Up @@ -322,7 +322,7 @@ export default class Stylesheet {

leave: (node: Node) => {
if (node.type === 'Rule' || node.type === 'Atrule') stack.pop();
if (node.type === 'Atrule') currentAtrule = stack[stack.length - 1];
if (node.type === 'Atrule') currentAtrule = <Atrule>stack[stack.length - 1];
}
});
} else {
Expand Down
3 changes: 1 addition & 2 deletions src/generators/Generator.ts
Expand Up @@ -16,10 +16,9 @@ import clone from '../utils/clone';
import DomBlock from './dom/Block';
import SsrBlock from './server-side-rendering/Block';
import Stylesheet from '../css/Stylesheet';
import { test } from '../config';
import { Node, GenerateOptions, Parsed, CompileOptions, CustomElementOptions } from '../interfaces';

const test = typeof global !== 'undefined' && global.__svelte_test;

interface Computation {
key: string;
deps: string[]
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/Block.ts
Expand Up @@ -202,7 +202,7 @@ export default class Block {
}

// minor hack – we need to ensure that any {{{triples}}} are detached first
this.builders.unmount.addBlockAtStart(this.builders.detachRaw);
this.builders.unmount.addBlockAtStart(this.builders.detachRaw.toString());

const properties = new CodeBuilder();

Expand Down
10 changes: 4 additions & 6 deletions src/generators/dom/index.ts
Expand Up @@ -14,11 +14,9 @@ import Generator from '../Generator';
import Stylesheet from '../../css/Stylesheet';
import preprocess from './preprocess';
import Block from './Block';
import { version } from '../../../package.json';
import { test } from '../../config';
import { Parsed, CompileOptions, Node } from '../../interfaces';

const test = typeof global !== 'undefined' && global.__svelte_test;

export class DomGenerator extends Generator {
blocks: (Block|string)[];
readonly: Set<string>;
Expand Down Expand Up @@ -167,9 +165,9 @@ export default function dom(
builder.addBlock(block.toString());
});

const sharedPath = options.shared === true
const sharedPath: string = options.shared === true
? 'svelte/shared.js'
: options.shared;
: options.shared || '';

const prototypeBase =
`${name}.prototype` +
Expand Down Expand Up @@ -444,7 +442,7 @@ export default function dom(
);

return generator.generate(result, options, {
banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${version} */`,
banner: `/* ${filename ? `${filename} ` : ``}generated by Svelte v${"__VERSION__"} */`,
sharedPath,
helpers,
name,
Expand Down
2 changes: 1 addition & 1 deletion src/generators/dom/visitors/Element/meta/Window.ts
Expand Up @@ -28,7 +28,7 @@ export default function visitWindow(
node: Node
) {
const events = {};
const bindings = {};
const bindings: Record<string, string> = {};

node.attributes.forEach((attribute: Node) => {
if (attribute.type === 'EventHandler') {
Expand Down
2 changes: 1 addition & 1 deletion src/generators/server-side-rendering/visitors/EachBlock.ts
Expand Up @@ -26,7 +26,7 @@ export default function visitEachBlock(
contextDependencies.set(node.context, dependencies);

if (node.destructuredContexts) {
for (const i = 0; i < node.destructuredContexts.length; i++) {
for (let i = 0; i < node.destructuredContexts.length; i += 1) {
contexts.set(node.destructuredContexts[i], `${node.context}[${i}]`);
contextDependencies.set(node.destructuredContexts[i], dependencies);
}
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Expand Up @@ -3,10 +3,11 @@ import validate from './validate/index';
import generate from './generators/dom/index';
import generateSSR from './generators/server-side-rendering/index';
import { assign } from './shared/index.js';
import { version } from '../package.json';
import Stylesheet from './css/Stylesheet';
import { Parsed, CompileOptions, Warning } from './interfaces';

const version = '__VERSION__';

function normalizeOptions(options: CompileOptions): CompileOptions {
let normalizedOptions = assign({ generate: 'dom' }, options);
const { onwarn, onerror } = normalizedOptions;
Expand Down
2 changes: 1 addition & 1 deletion src/interfaces.ts
Expand Up @@ -65,7 +65,7 @@ export interface GenerateOptions {
name: string;
format: ModuleFormat;
banner?: string;
sharedPath?: string | boolean;
sharedPath?: string;
helpers?: { name: string, alias: string }[];
}

Expand Down
6 changes: 4 additions & 2 deletions src/parse/index.ts
Expand Up @@ -23,6 +23,8 @@ interface ParserOptions {
filename?: string;
}

type ParserState = (parser: Parser) => (ParserState | void);

export class Parser {
readonly template: string;
readonly filename?: string;
Expand Down Expand Up @@ -59,7 +61,7 @@ export class Parser {

this.stack.push(this.html);

let state = fragment;
let state: ParserState = fragment;

while (this.index < this.template.length) {
state = state(this) || fragment;
Expand Down Expand Up @@ -94,7 +96,7 @@ export class Parser {
return this.stack[this.stack.length - 1];
}

acornError(err: Error) {
acornError(err: any) {
this.error(err.message.replace(/ \(\d+:\d+\)$/, ''), err.pos);
}

Expand Down
6 changes: 2 additions & 4 deletions test/helpers.js
Expand Up @@ -8,11 +8,9 @@ import chalk from 'chalk';
// for coverage purposes, we need to test source files,
// but for sanity purposes, we need to test dist files
export function loadSvelte(test) {
if (test) global.__svelte_test = true;
process.env.TEST = test ? 'true' : '';

const resolved = process.env.COVERAGE
? require.resolve('../src/index.js')
: require.resolve('../compiler/svelte.js');
const resolved = require.resolve('../compiler/svelte.js');

delete require.cache[resolved];
return require(resolved);
Expand Down
4 changes: 1 addition & 3 deletions test/server-side-rendering/index.js
Expand Up @@ -21,9 +21,7 @@ function tryToReadFile(file) {

describe("ssr", () => {
before(() => {
require(process.env.COVERAGE
? "../../src/server-side-rendering/register.js"
: "../../ssr/register");
require("../../ssr/register");

return setupHtmlEqual();
});
Expand Down
2 changes: 2 additions & 0 deletions test/setup.js
Expand Up @@ -4,6 +4,8 @@ const path = require('path');
require('console-group').install();
require('source-map-support').install();

process.env.TEST = true;

require.extensions['.js'] = function(module, filename) {
const exports = [];

Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
@@ -1,6 +1,5 @@
{
"compilerOptions": {
"noImplicitAny": true,
"diagnostics": true,
"noImplicitThis": true,
"noEmitOnError": true,
Expand Down

0 comments on commit d590dbe

Please sign in to comment.