From ce24764124b2e22c927690f8d0b8af2c1ee33a9b Mon Sep 17 00:00:00 2001 From: Forbes Lindesay Date: Sat, 6 May 2017 02:15:28 +0100 Subject: [PATCH] Use prettier for JavaScript snapshots (#2797) --- package.json | 12 ++- .../pug/test/__snapshots__/pug.test.js.snap | 98 +++++++++++++++++-- scripts/prettier-javascript-serializer.js | 21 ++++ 3 files changed, 117 insertions(+), 14 deletions(-) create mode 100644 scripts/prettier-javascript-serializer.js diff --git a/package.json b/package.json index 0fa9d5e6d..b6eea874f 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,12 @@ "devDependencies": { "coveralls": "^2.11.2", "jest": "^18.1.0", - "lerna": "2.0.0-beta.32" + "lerna": "2.0.0-beta.32", + "prettier": "^1.3.1" }, "repository": { - "type": "git", - "url": "https://github.com/pugjs/pug.git" + "type": "git", + "url": "https://github.com/pugjs/pug.git" }, "scripts": { "clean": "lerna clean", @@ -22,7 +23,10 @@ }, "jest": { "testEnvironment": "node", - "snapshotSerializers": ["./scripts/filename-serializer.js"] + "snapshotSerializers": [ + "./scripts/filename-serializer.js", + "./scripts/prettier-javascript-serializer.js" + ] }, "license": "MIT" } diff --git a/packages/pug/test/__snapshots__/pug.test.js.snap b/packages/pug/test/__snapshots__/pug.test.js.snap index 5ee3e8336..c7c4f1f29 100644 --- a/packages/pug/test/__snapshots__/pug.test.js.snap +++ b/packages/pug/test/__snapshots__/pug.test.js.snap @@ -1,15 +1,93 @@ exports[`pug .compileClient() should support module syntax in pug.compileClient(str, options) when inlineRuntimeFunctions it false 1`] = ` -"var pug = require(\"pug-runtime\");function template(locals) {var pug_html = \"\", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_debug_line;try {var self = locals || {};;pug_debug_line = 1; -pug_html = pug_html + \"\\u003Cdiv class=\\\"bar\\\"\\u003E\"; -;pug_debug_line = 1; -pug_html = pug_html + (pug.escape(null == (pug_interp = self.foo) ? \"\" : pug_interp)) + \"\\u003C\\u002Fdiv\\u003E\";} catch (err) {pug.rethrow(err, pug_debug_filename, pug_debug_line);};return pug_html;} module.exports = template;" +"var pug = require(\"pug-runtime\"); +function template(locals) { + var pug_html = \"\", pug_mixins = {}, pug_interp; + var pug_debug_filename, pug_debug_line; + try { + var self = locals || {}; + pug_debug_line = 1; + pug_html = pug_html + \'\\u003Cdiv class=\"bar\"\\u003E\'; + pug_debug_line = 1; + pug_html = + pug_html + + pug.escape(null == (pug_interp = self.foo) ? \"\" : pug_interp) + + \"\\u003C\\u002Fdiv\\u003E\"; + } catch (err) { + pug.rethrow(err, pug_debug_filename, pug_debug_line); + } + return pug_html; +} +module.exports = template; +" `; exports[`pug .compileClient() should support module syntax in pug.compileClient(str, options) when inlineRuntimeFunctions it true 1`] = ` -"function pug_escape(e){var a=\"\"+e,t=pug_match_html.exec(a);if(!t)return e;var r,c,n,s=\"\";for(r=t.index,c=0;r]/; -function pug_rethrow(n,e,r,t){if(!(n instanceof Error))throw n;if(!(\"undefined\"==typeof window&&e||t))throw n.message+=\" on line \"+r,n;try{t=t||require(\"fs\").readFileSync(e,\"utf8\")}catch(e){pug_rethrow(n,null,r)}var i=3,a=t.split(\"\\n\"),o=Math.max(r-i,0),h=Math.min(a.length,r+i),i=a.slice(o,h).map(function(n,e){var t=e+o+1;return(t==r?\" > \":\" \")+t+\"| \"+n}).join(\"\\n\");throw n.path=e,n.message=(e||\"Pug\")+\":\"+r+\"\\n\"+i+\"\\n\\n\"+n.message,n}function template(locals) {var pug_html = \"\", pug_mixins = {}, pug_interp;var pug_debug_filename, pug_debug_line;try {var self = locals || {};;pug_debug_line = 1; -pug_html = pug_html + \"\\u003Cdiv class=\\\"bar\\\"\\u003E\"; -;pug_debug_line = 1; -pug_html = pug_html + (pug_escape(null == (pug_interp = self.foo) ? \"\" : pug_interp)) + \"\\u003C\\u002Fdiv\\u003E\";} catch (err) {pug_rethrow(err, pug_debug_filename, pug_debug_line);};return pug_html;} module.exports = template;" +"function pug_escape(e) { + var a = \"\" + e, t = pug_match_html.exec(a); + if (!t) return e; + var r, c, n, s = \"\"; + for ((r = t.index), (c = 0); r < a.length; r++) { + switch (a.charCodeAt(r)) { + case 34: + n = \""\"; + break; + case 38: + n = \"&\"; + break; + case 60: + n = \"<\"; + break; + case 62: + n = \">\"; + break; + default: + continue; + } + c !== r && (s += a.substring(c, r)), (c = r + 1), (s += n); + } + return c !== r ? s + a.substring(c, r) : s; +} +var pug_match_html = /[\"&<>]/; +function pug_rethrow(n, e, r, t) { + if (!(n instanceof Error)) throw n; + if (!((\"undefined\" == typeof window && e) || t)) + throw ((n.message += \" on line \" + r), n); + try { + t = t || require(\"fs\").readFileSync(e, \"utf8\"); + } catch (e) { + pug_rethrow(n, null, r); + } + var i = 3, + a = t.split(\"\\n\"), + o = Math.max(r - i, 0), + h = Math.min(a.length, r + i), + i = a + .slice(o, h) + .map(function(n, e) { + var t = e + o + 1; + return (t == r ? \" > \" : \" \") + t + \"| \" + n; + }) + .join(\"\\n\"); + throw ((n.path = e), (n.message = + (e || \"Pug\") + \":\" + r + \"\\n\" + i + \"\\n\\n\" + n.message), n); +} +function template(locals) { + var pug_html = \"\", pug_mixins = {}, pug_interp; + var pug_debug_filename, pug_debug_line; + try { + var self = locals || {}; + pug_debug_line = 1; + pug_html = pug_html + \'\\u003Cdiv class=\"bar\"\\u003E\'; + pug_debug_line = 1; + pug_html = + pug_html + + pug_escape(null == (pug_interp = self.foo) ? \"\" : pug_interp) + + \"\\u003C\\u002Fdiv\\u003E\"; + } catch (err) { + pug_rethrow(err, pug_debug_filename, pug_debug_line); + } + return pug_html; +} +module.exports = template; +" `; diff --git a/scripts/prettier-javascript-serializer.js b/scripts/prettier-javascript-serializer.js new file mode 100644 index 000000000..4a0630a22 --- /dev/null +++ b/scripts/prettier-javascript-serializer.js @@ -0,0 +1,21 @@ +const prettier = require('prettier'); + +const prettierOptions = {} // optional + +// filename serializer that removes the basedir +module.exports = { + test: function(val) { + try { + return ( + typeof val === 'string' && + /function /.test(val) && + val !== prettier.format(val, prettierOptions) + ); + } catch (ex) { + return false; + } + }, + print: function(val, serialize, indent) { + return serialize(prettier.format(val, prettierOptions)); + } +};