From 92bf9802b1973365e50f7ce008bc420cba2a6711 Mon Sep 17 00:00:00 2001 From: Behrang Yarahmadi <6979966+byara@users.noreply.github.com> Date: Tue, 23 Apr 2019 09:54:24 +0200 Subject: [PATCH] Warn about usage of non-breaking space (#107) * Throws better error for NBSP tokens [#85] Co-Authored-By: byara <6979966+byara@users.noreply.github.com> --- CHANGELOG.md | 1 + .../__fixtures__/error/nonbreakable_space.template | 1 + .../__tests__/__snapshots__/CompilerSpec.js.snap | 7 +++++++ packages/melody-parser/src/Lexer.js | 5 +++++ 4 files changed, 14 insertions(+) create mode 100644 packages/melody-compiler/__tests__/__fixtures__/error/nonbreakable_space.template diff --git a/CHANGELOG.md b/CHANGELOG.md index 66540c2..e5d9c37 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ - incorrect 'is' method call in melody-types [#20](https://github.com/trivago/melody/issues/20) - getFocusedPath can break on IE11 for svg elements [#57](https://github.com/trivago/melody/issues/57) +- Warn about usage of non-breaking space [#85](https://github.com/trivago/melody/issues/85) ### Chore & Maintenance diff --git a/packages/melody-compiler/__tests__/__fixtures__/error/nonbreakable_space.template b/packages/melody-compiler/__tests__/__fixtures__/error/nonbreakable_space.template new file mode 100644 index 0000000..2f3c8cd --- /dev/null +++ b/packages/melody-compiler/__tests__/__fixtures__/error/nonbreakable_space.template @@ -0,0 +1 @@ +{{ 'success_msg' | sort }} diff --git a/packages/melody-compiler/__tests__/__snapshots__/CompilerSpec.js.snap b/packages/melody-compiler/__tests__/__snapshots__/CompilerSpec.js.snap index e030a59..0d11176 100644 --- a/packages/melody-compiler/__tests__/__snapshots__/CompilerSpec.js.snap +++ b/packages/melody-compiler/__tests__/__snapshots__/CompilerSpec.js.snap @@ -2277,6 +2277,13 @@ Example: This is the placeholder content that will be shown to your users while the async component is being loaded." `; +exports[`Compiler should fail transforming nonbreakable space.template 1`] = ` +"ERROR: Unsupported token: Non-breaking space +> 1 | {{ 'success_msg' | sort }} + | ^ + 2 | " +`; + exports[`Compiler should fail transforming unknown filter.template 1`] = ` "Unknown filter \\"unknown\\" > 1 | {{ test | unknown }} diff --git a/packages/melody-parser/src/Lexer.js b/packages/melody-parser/src/Lexer.js index b2d5929..596fb98 100644 --- a/packages/melody-parser/src/Lexer.js +++ b/packages/melody-parser/src/Lexer.js @@ -355,6 +355,11 @@ export default class Lexer { } else if (CHAR_TO_TOKEN.hasOwnProperty(c)) { input.next(); return this.createToken(CHAR_TO_TOKEN[c], pos); + } else if (c === '\xa0') { + return this.error( + 'Unsupported token: Non-breaking space', + pos + ); } else { return this.error(`Unknown token ${c}`, pos); }