From 194e99b350fb9b18b88bdb90f6539f4c0847962a Mon Sep 17 00:00:00 2001 From: Clemens Akens Date: Wed, 24 Apr 2019 00:41:47 +0200 Subject: [PATCH 1/2] expose parse to the public --- src/index.ts | 1 + src/interfaces.ts | 5 +++++ src/parse/index.ts | 7 +------ 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index c80b449825..bb0194fb43 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export { default as compile } from './compile/index'; +export { default as parse } from './parse/index'; export { default as preprocess } from './preprocess/index'; export const VERSION = '__VERSION__'; \ No newline at end of file diff --git a/src/interfaces.ts b/src/interfaces.ts index fd34f31ea1..68fef7472e 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -61,6 +61,11 @@ export interface CompileOptions { preserveWhitespace?: boolean; } +export interface ParserOptions { + filename?: string; + customElement?: boolean; +} + export interface Visitor { enter: (node: Node) => void; leave?: (node: Node) => void; diff --git a/src/parse/index.ts b/src/parse/index.ts index 39c6972213..a9c3950425 100644 --- a/src/parse/index.ts +++ b/src/parse/index.ts @@ -3,14 +3,9 @@ import fragment from './state/fragment'; import { whitespace } from '../utils/patterns'; import { reserved } from '../utils/names'; import full_char_code_at from '../utils/full_char_code_at'; -import { Node, Ast } from '../interfaces'; +import { Node, Ast, ParserOptions } from '../interfaces'; import error from '../utils/error'; -interface ParserOptions { - filename?: string; - customElement?: boolean; -} - type ParserState = (parser: Parser) => (ParserState | void); export class Parser { From df448cb36af056aa10604796f66d6b9d632c09f0 Mon Sep 17 00:00:00 2001 From: Conduitry Date: Sat, 4 May 2019 07:29:36 -0400 Subject: [PATCH 2/2] document svelte.parse --- site/content/docs/04-compile-time.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/site/content/docs/04-compile-time.md b/site/content/docs/04-compile-time.md index 7dd1e371f6..9abeee37ef 100644 --- a/site/content/docs/04-compile-time.md +++ b/site/content/docs/04-compile-time.md @@ -153,6 +153,30 @@ compiled: { --> +### `svelte.parse` + +```js +ast: object = svelte.parse( + source: string, + options?: { + filename?: string, + customElement?: boolean + } +) +``` + +--- + +The `parse` function parses a component, returning only its abstract syntax tree. Unlike compiling with the `generate: false` option, this will not perform any validation or other analysis of the component beyond parsing it. + + +```js +const svelte = require('svelte/compiler'); + +const ast = svelte.parse(source, { filename: 'App.svelte' }); +``` + + ### `svelte.preprocess` ```js