From a3347646493950735ffa3d9e5a0fa4f8ae0deefa Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 25 Aug 2020 20:05:24 +0200 Subject: [PATCH 1/5] (fix) Make isASTNode function work with prettier 2.1.x (#125) This change alters the code such that we are independent of property-changes of both Svelte and Prettier by adding a `__isRoot`-property to the parser output root. Fixes #123 --- .gitignore | 1 + package-lock.json | 12 ++++++------ package.json | 4 ++-- src/index.ts | 7 ++++--- src/print/helpers.ts | 22 ++++++---------------- src/print/nodes.ts | 23 +++++++++++++++++++++++ 6 files changed, 42 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index 4bf4be00..5f34e423 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ node_modules .vscode +.history plugin.js plugin.js.map diff --git a/package-lock.json b/package-lock.json index 16d15408..4cb0c0b4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -412,9 +412,9 @@ "dev": true }, "@types/prettier": { - "version": "2.0.0", - "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.0.tgz", - "integrity": "sha512-/rM+sWiuOZ5dvuVzV37sUuklsbg+JPOP8d+nNFlo2ZtfpzPiPvh1/gc8liWOLBqe+sR+ZM7guPaIcTt6UZTo7Q==", + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/@types/prettier/-/prettier-2.0.2.tgz", + "integrity": "sha512-IkVfat549ggtkZUthUzEX49562eGikhSYeVGX97SkMFn+sTZrgRewXjQ4tPKFPCykZHkX1Zfd9OoELGqKU2jJA==", "dev": true }, "acorn": { @@ -3530,9 +3530,9 @@ "dev": true }, "prettier": { - "version": "2.0.5", - "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.0.5.tgz", - "integrity": "sha512-7PtVymN48hGcO4fGjybyBSIWDsLU4H4XlvOHfq91pz9kkGlonzwTfYkaIEwiRg/dAJF9YlbsduBAgtYLi+8cFg==", + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/prettier/-/prettier-2.1.0.tgz", + "integrity": "sha512-lz28cCbA1cDFHVuY8vvj6QuqOwIpyIfPUYkSl8AZ/vxH8qBXMMjE2knfLHCrZCmUsK/H1bg1P0tOo0dJkTJHvw==", "dev": true }, "pretty-ms": { diff --git a/package.json b/package.json index e5c13e77..3fa33af0 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index 4ae19807..cebe10a2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'; @@ -21,9 +22,9 @@ export const languages: Partial[] = [ export const parsers: Record = { svelte: { - parse: text => { + parse: (text) => { try { - return require(`svelte/compiler`).parse(text); + return { ...require(`svelte/compiler`).parse(text), __isRoot: true }; } catch (err) { err.loc = { start: err.start, @@ -34,7 +35,7 @@ export const parsers: Record = { throw err; } }, - preprocess: text => { + preprocess: (text) => { text = snipTagContent('style', text); text = snipTagContent('script', text, '{}'); return text.trim(); diff --git a/src/print/helpers.ts b/src/print/helpers.ts index 02e39b57..3007f925 100644 --- a/src/print/helpers.ts +++ b/src/print/helpers.ts @@ -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; } diff --git a/src/print/nodes.ts b/src/print/nodes.ts index d8bcb64c..dced6e75 100644 --- a/src/print/nodes.ts +++ b/src/print/nodes.ts @@ -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; +} From ab79f9a3a4b6723da1b78b6136ac4e65df3e684b Mon Sep 17 00:00:00 2001 From: Andreas Ehrencrona Date: Tue, 25 Aug 2020 20:06:13 +0200 Subject: [PATCH 2/5] Incorrectly escaped regexp broke style tags followed by "s" (#122) Fixes #118 --- src/lib/snipTagContent.ts | 2 +- test/printer/samples/s-after-style-tag.html | 5 +++++ test/printer/samples/script-inside-comment.html | 2 +- test/printer/samples/style-inside-comment.html | 2 +- 4 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 test/printer/samples/s-after-style-tag.html diff --git a/src/lib/snipTagContent.ts b/src/lib/snipTagContent.ts index dd819b70..fb62f024 100644 --- a/src/lib/snipTagContent.ts +++ b/src/lib/snipTagContent.ts @@ -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}`; diff --git a/test/printer/samples/s-after-style-tag.html b/test/printer/samples/s-after-style-tag.html new file mode 100644 index 00000000..ec560513 --- /dev/null +++ b/test/printer/samples/s-after-style-tag.html @@ -0,0 +1,5 @@ + + +sssssweet diff --git a/test/printer/samples/script-inside-comment.html b/test/printer/samples/script-inside-comment.html index a330bce2..8af60b81 100644 --- a/test/printer/samples/script-inside-comment.html +++ b/test/printer/samples/script-inside-comment.html @@ -1 +1 @@ - + diff --git a/test/printer/samples/style-inside-comment.html b/test/printer/samples/style-inside-comment.html index 1fe9b582..8ba612fd 100644 --- a/test/printer/samples/style-inside-comment.html +++ b/test/printer/samples/style-inside-comment.html @@ -1 +1 @@ - + From 930c59f063d8dc550ab5e4667d72fc6cf9e2ae29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C3=B6rnqvist?= <624396+toerndev@users.noreply.github.com> Date: Tue, 25 Aug 2020 20:08:16 +0200 Subject: [PATCH 3/5] send debug info to stderr instead of stdout (#126) Fixes #115 --- src/print/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/print/index.ts b/src/print/index.ts index e6b5ac03..3eab9ce8 100644 --- a/src/print/index.ts +++ b/src/print/index.ts @@ -459,7 +459,7 @@ export function print(path: FastPath, options: ParserOptions, print: PrintFn): D return concat([line, '{...', printJS(path, print, 'expression'), '}']); } - console.log(JSON.stringify(node, null, 4)); + console.error(JSON.stringify(node, null, 4)); throw new Error('unknown node type: ' + node.type); } @@ -642,6 +642,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); } From 1ca00be0b4a13af689459e04c5b65b3c1ec853a9 Mon Sep 17 00:00:00 2001 From: Simon H <5968653+dummdidumm@users.noreply.github.com> Date: Tue, 25 Aug 2020 20:12:52 +0200 Subject: [PATCH 4/5] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24e48e8b..db55be1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # prettier-plugin-svelte changelog +## Unreleased + +* 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 `` comments ([#59](https://github.com/sveltejs/prettier-plugin-svelte/issues/59)) From b90192544e2754c6d4c63f086312b6ec283cc49e Mon Sep 17 00:00:00 2001 From: Conduitry Date: Tue, 25 Aug 2020 15:27:34 -0400 Subject: [PATCH 5/5] -> v1.1.1 --- CHANGELOG.md | 2 +- package-lock.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index db55be1a..3fba64f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # prettier-plugin-svelte changelog -## Unreleased +## 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)) diff --git a/package-lock.json b/package-lock.json index 4cb0c0b4..e9055871 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "prettier-plugin-svelte", - "version": "1.1.0", + "version": "1.1.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3fa33af0..de6643d6 100644 --- a/package.json +++ b/package.json @@ -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": [