Skip to content

Commit

Permalink
Merge with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Ehrencrona committed Aug 26, 2020
2 parents f8878a4 + b901925 commit a64ae42
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 18 deletions.
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# prettier-plugin-svelte changelog

## 1.1.1

* Fix bug that breaks plugin when using Prettier v2.1.x ([#123](https://github.com/sveltejs/prettier-plugin-svelte/issues/123))
* Fix incorrectly escaped regexp which broke style tags followed by "s" ([#118](https://github.com/sveltejs/prettier-plugin-svelte/issues/118))
* Write to console.error to prevent crash and erasion of files ([#115](https://github.com/sveltejs/prettier-plugin-svelte/issues/115))

## 1.1.0

* Support `<!-- prettier-ignore -->` comments ([#59](https://github.com/sveltejs/prettier-plugin-svelte/issues/59))
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

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

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "prettier-plugin-svelte",
"version": "1.1.0",
"version": "1.1.1",
"description": "Svelte plugin for prettier",
"main": "plugin.js",
"files": [
Expand Down 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
2 changes: 1 addition & 1 deletion src/lib/snipTagContent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function snipTagContent(tagName: string, source: string, placeholder = ''): string {
const regex = new RegExp(`[\s\n]*<${tagName}([^]*?)>([^]*?)<\/${tagName}>[\s\n]*`, 'gi');
const regex = new RegExp(`[\\s\n]*<${tagName}([^]*?)>([^]*?)<\/${tagName}>[\\s\n]*`, 'gi');
return source.replace(regex, (_, attributes, content) => {
const encodedContent = Buffer.from(content).toString('base64');
return `<${tagName}${attributes} ✂prettier:content✂="${encodedContent}">${placeholder}</${tagName}>`;
Expand Down
6 changes: 5 additions & 1 deletion src/print/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export interface ASTNode {
module?: ScriptNode;
}

/**
* 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;
}

export function isPreTagContent(path: FastPath): boolean {
Expand Down
3 changes: 2 additions & 1 deletion src/print/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D
return concat([line, '{...', printJS(path, print, 'expression'), '}']);
}

console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
}

Expand Down Expand Up @@ -695,6 +696,6 @@ function expandNode(node): string {
return ' ...' + node.argument.name;
}

console.log(JSON.stringify(node, null, 4));
console.error(JSON.stringify(node, null, 4));
throw new Error('unknown node type: ' + node.type);
}
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;
}
5 changes: 5 additions & 0 deletions test/printer/samples/s-after-style-tag.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<style>

</style>

sssssweet
2 changes: 1 addition & 1 deletion test/printer/samples/script-inside-comment.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- <script>alert(1)</script> -->
<!--<script>alert(1)</script>-->
2 changes: 1 addition & 1 deletion test/printer/samples/style-inside-comment.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!-- <style>div { color: red }</style> -->
<!--<style>div { color: red }</style>-->

0 comments on commit a64ae42

Please sign in to comment.