remove validate and Stylesheet from public API

pull/1348/head
Rich Harris 7 years ago
parent a3add04e7c
commit 80e0dceb9a

@ -26,6 +26,8 @@ function collapseTimings(timings) {
} }
export default class Stats { export default class Stats {
onwarn: (warning: Warning) => void;
startTime: number; startTime: number;
currentTiming: Timing; currentTiming: Timing;
currentChildren: Timing[]; currentChildren: Timing[];
@ -33,11 +35,15 @@ export default class Stats {
stack: Timing[]; stack: Timing[];
warnings: Warning[]; warnings: Warning[];
constructor() { constructor({ onwarn }: {
onwarn: (warning: Warning) => void
}) {
this.startTime = now(); this.startTime = now();
this.stack = []; this.stack = [];
this.currentChildren = this.timings = []; this.currentChildren = this.timings = [];
this.onwarn = onwarn;
this.warnings = []; this.warnings = [];
} }
@ -99,4 +105,9 @@ export default class Stats {
hooks hooks
}; };
} }
warn(warning) {
this.warnings.push(warning);
this.onwarn(warning);
}
} }

@ -106,11 +106,13 @@ export async function preprocess(source: string, options: PreprocessOptions) {
}; };
} }
export function compile(source: string, _options: CompileOptions) { function compile(source: string, _options: CompileOptions) {
const options = normalizeOptions(_options); const options = normalizeOptions(_options);
let parsed: Parsed; let parsed: Parsed;
const stats = new Stats(); const stats = new Stats({
onwarn: options.onwarn
});
try { try {
stats.start('parse'); stats.start('parse');
@ -126,23 +128,19 @@ export function compile(source: string, _options: CompileOptions) {
stats.stop('stylesheet'); stats.stop('stylesheet');
stats.start('validate'); stats.start('validate');
// TODO remove this when we remove svelte.validate from public API — we validate(parsed, source, stylesheet, stats, options);
// can use the stats object instead
const onwarn = options.onwarn;
options.onwarn = warning => {
stats.warnings.push(warning);
onwarn(warning);
};
validate(parsed, source, stylesheet, options);
stats.stop('validate'); stats.stop('validate');
if (options.generate === false) {
return { ast: parsed, stats, js: null, css: null };
}
const compiler = options.generate === 'ssr' ? generateSSR : generate; const compiler = options.generate === 'ssr' ? generateSSR : generate;
return compiler(parsed, source, stylesheet, options, stats); return compiler(parsed, source, stylesheet, options, stats);
}; };
export function create(source: string, _options: CompileOptions = {}) { function create(source: string, _options: CompileOptions = {}) {
_options.format = 'eval'; _options.format = 'eval';
const compiled = compile(source, _options); const compiled = compile(source, _options);
@ -163,4 +161,4 @@ export function create(source: string, _options: CompileOptions = {}) {
} }
} }
export { parse, validate, Stylesheet, version as VERSION }; export { parse, create, compile, version as VERSION };

@ -44,7 +44,7 @@ export interface CompileOptions {
format?: ModuleFormat; format?: ModuleFormat;
name?: string; name?: string;
filename?: string; filename?: string;
generate?: string; generate?: string | false;
globals?: ((id: string) => string) | object; globals?: ((id: string) => string) | object;
amd?: { amd?: {
id?: string; id?: string;

@ -5,15 +5,16 @@ import getCodeFrame from '../utils/getCodeFrame';
import Stats from '../Stats'; import Stats from '../Stats';
import error from '../utils/error'; import error from '../utils/error';
import Stylesheet from '../css/Stylesheet'; import Stylesheet from '../css/Stylesheet';
import Stats from '../Stats';
import { Node, Parsed, CompileOptions, Warning } from '../interfaces'; import { Node, Parsed, CompileOptions, Warning } from '../interfaces';
export class Validator { export class Validator {
readonly source: string; readonly source: string;
readonly filename: string; readonly filename: string;
readonly v2: boolean; readonly v2: boolean;
readonly stats: Stats;
options: CompileOptions; options: CompileOptions;
onwarn: ({}) => void;
locator?: (pos: number) => Location; locator?: (pos: number) => Location;
namespace: string; namespace: string;
@ -34,10 +35,11 @@ export class Validator {
actions: Set<string>; actions: Set<string>;
}; };
constructor(parsed: Parsed, source: string, options: CompileOptions) { constructor(parsed: Parsed, source: string, stats: Stats, options: CompileOptions) {
this.source = source; this.source = source;
this.stats = stats;
this.filename = options.filename; this.filename = options.filename;
this.onwarn = options.onwarn;
this.options = options; this.options = options;
this.v2 = options.parser === 'v2'; this.v2 = options.parser === 'v2';
@ -79,7 +81,7 @@ export class Validator {
const frame = getCodeFrame(this.source, start.line, start.column); const frame = getCodeFrame(this.source, start.line, start.column);
this.onwarn({ this.stats.warn({
code, code,
message, message,
frame, frame,
@ -96,9 +98,10 @@ export default function validate(
parsed: Parsed, parsed: Parsed,
source: string, source: string,
stylesheet: Stylesheet, stylesheet: Stylesheet,
stats: Stats,
options: CompileOptions options: CompileOptions
) { ) {
const { onwarn, onerror, name, filename, store, dev, parser } = options; const { onerror, name, filename, store, dev, parser } = options;
try { try {
if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) { if (name && !/^[a-zA-Z_$][a-zA-Z_$0-9]*$/.test(name)) {
@ -108,7 +111,7 @@ export default function validate(
if (name && /^[a-z]/.test(name)) { if (name && /^[a-z]/.test(name)) {
const message = `options.name should be capitalised`; const message = `options.name should be capitalised`;
onwarn({ stats.warn({
code: `options-lowercase-name`, code: `options-lowercase-name`,
message, message,
filename, filename,
@ -116,8 +119,7 @@ export default function validate(
}); });
} }
const validator = new Validator(parsed, source, { const validator = new Validator(parsed, source, stats, {
onwarn,
name, name,
filename, filename,
store, store,

@ -28,7 +28,8 @@ describe("validate", () => {
const { code, message, pos, loc, end } = warning; const { code, message, pos, loc, end } = warning;
warnings.push({ code, message, pos, loc, end }); warnings.push({ code, message, pos, loc, end });
}, },
dev: config.dev dev: config.dev,
generate: false
}); });
assert.equal(stats.warnings.length, warnings.length); assert.equal(stats.warnings.length, warnings.length);
@ -87,7 +88,8 @@ describe("validate", () => {
it("errors if options.name is illegal", () => { it("errors if options.name is illegal", () => {
assert.throws(() => { assert.throws(() => {
svelte.compile("<div></div>", { svelte.compile("<div></div>", {
name: "not.valid" name: "not.valid",
generate: false
}); });
}, /options\.name must be a valid identifier/); }, /options\.name must be a valid identifier/);
}); });
@ -103,7 +105,8 @@ describe("validate", () => {
pos: warning.pos, pos: warning.pos,
loc: warning.loc loc: warning.loc
}); });
} },
generate: false
}); });
assert.deepEqual(warnings, [ assert.deepEqual(warnings, [
{ {
@ -126,7 +129,8 @@ describe("validate", () => {
pos: warning.pos, pos: warning.pos,
loc: warning.loc loc: warning.loc
}); });
} },
generate: false
}); });
assert.deepEqual(warnings, []); assert.deepEqual(warnings, []);
}); });

Loading…
Cancel
Save