Skip to content

Commit

Permalink
Merge pull request #2534 from clebert/public-parse
Browse files Browse the repository at this point in the history
Expose `parse` to the public
  • Loading branch information
Rich-Harris authored May 4, 2019
2 parents ab711ac + df448cb commit d3d5fa9
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
24 changes: 24 additions & 0 deletions site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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__';
5 changes: 5 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit d3d5fa9

Please sign in to comment.