Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) Make isASTNode function work with prettier 2.1.x #125

Merged
merged 1 commit into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.vscode
.history
plugin.js
plugin.js.map
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
"devDependencies": {
"@types/node": "^10.12.18",
"@types/prettier": "^2.0.0",
"@types/prettier": "^2.0.2",
"ava": "1.2.0",
"prettier": "^2.0.0",
"prettier": "^2.1.0",
"rollup": "1.1.2",
"rollup-plugin-commonjs": "9.2.0",
"rollup-plugin-node-resolve": "4.0.0",
Expand Down
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SupportLanguage, Parser, Printer } from 'prettier';
import { print } from './print';
import { ASTNode } from './print/nodes';
import { embed } from './embed';
import { snipTagContent } from './lib/snipTagContent';

Expand All @@ -21,9 +22,9 @@ export const languages: Partial<SupportLanguage>[] = [

export const parsers: Record<string, Parser> = {
svelte: {
parse: text => {
parse: (text) => {
try {
return require(`svelte/compiler`).parse(text);
return <ASTNode>{ ...require(`svelte/compiler`).parse(text), __isRoot: true };
} catch (err) {
err.loc = {
start: err.start,
Expand All @@ -34,7 +35,7 @@ export const parsers: Record<string, Parser> = {
throw err;
}
},
preprocess: text => {
preprocess: (text) => {
text = snipTagContent('style', text);
text = snipTagContent('script', text, '{}');
return text.trim();
Expand Down
22 changes: 6 additions & 16 deletions src/print/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,9 @@
import { Node, ScriptNode, InstanceScriptNode, ModuleScriptNode } from './nodes';

export interface ASTNode {
html: Node;
css?: Node & {
attributes: Node[];
children: Node[];
content: Node & {
styles: string;
};
};
js?: ScriptNode;
instance?: ScriptNode;
module?: ScriptNode;
}
import { ASTNode } from './nodes';

/**
* Determines whether or not given node
* is the root of the Svelte AST.
*/
export function isASTNode(n: any): n is ASTNode {
return 'html' in n && 'tokens' in n;
return n && n.__isRoot;
}
23 changes: 23 additions & 0 deletions src/print/nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,3 +285,26 @@ export type Node =
| ModuleScriptNode
| BodyNode
| OptionsNode;

/**
* The Svelte AST root node
*/
export interface ASTNode {
html: Node;
css?: Node & {
attributes: Node[];
children: Node[];
content: Node & {
styles: string;
};
};
js?: ScriptNode;
instance?: ScriptNode;
module?: ScriptNode;
/**
* This is not actually part of the Svelte parser output,
* but we add it afterwards to make sure we can distinguish
* the root node from other nodes afterwards.
*/
__isRoot: boolean;
}