Skip to content

Commit

Permalink
add printCode(ast)
Browse files Browse the repository at this point in the history
  • Loading branch information
mizchi committed Feb 22, 2021
1 parent 800b2cd commit dc8ddb2
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

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

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "prettier-plugin-svelte",
"name": "@mizchi/svelte-printer",
"version": "2.1.6",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
Expand Down Expand Up @@ -28,13 +28,13 @@
},
"homepage": "https://github.com/sveltejs/prettier-plugin-svelte#readme",
"devDependencies": {
"@rollup/plugin-commonjs": "14.0.0",
"@rollup/plugin-node-resolve": "11.0.1",
"@types/node": "^10.12.18",
"@types/prettier": "^2.1.6",
"ava": "3.15.0",
"prettier": "^2.2.1",
"rollup": "2.36.0",
"@rollup/plugin-commonjs": "14.0.0",
"@rollup/plugin-node-resolve": "11.0.1",
"rollup-plugin-typescript": "1.0.1",
"svelte": "^3.30.0",
"ts-node": "^9.1.1",
Expand All @@ -44,5 +44,8 @@
"peerDependencies": {
"prettier": "^1.16.4 || ^2.0.0",
"svelte": "^3.2.0"
},
"dependencies": {
"astring": "^1.7.0"
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,5 @@ export const printers: Record<string, Printer> = {
};

export { options } from './options';

export { printCode } from './printCode';
9 changes: 7 additions & 2 deletions src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { hasSnippedContent, unsnipContent } from '../lib/snipTagContent';
import { parseSortOrder, SortOrderPart } from '../options';
import { isEmptyDoc, isLine, trim, trimRight } from './doc-helpers';
import { flatten, isASTNode, isPreTagContent } from './helpers';
import { generate } from 'astring';
import {
checkWhitespaceAtEndOfSvelteBlock,
checkWhitespaceAtStartOfSvelteBlock,
Expand Down Expand Up @@ -622,8 +623,12 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
return concat([line, '{...', printJS(path, print, false, false, 'expression'), '}']);
}

console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
try {
return generate(node);
} catch (err) {
console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
}
}

function printTopLevelParts(
Expand Down
22 changes: 22 additions & 0 deletions src/printCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Ast } from 'svelte/types/compiler/interfaces';
import prettier from 'prettier/standalone';
import * as p from './index';

export function printCode(ast: Ast) {
const overridePlugin = {
...p,
parsers: {
...p.parsers,
svelte: {
...p.parsers.svelte,
parse: () => {
return { ...ast, __isRoot: true };
},
},
},
};
return prettier.format(' dummy ', {
parser: 'svelte',
plugins: [overridePlugin as any],
});
}

0 comments on commit dc8ddb2

Please sign in to comment.