From 7868c248d9b1eddeef843425f5902486e49ba767 Mon Sep 17 00:00:00 2001 From: araslanov_e Date: Sun, 24 Feb 2019 00:04:04 +0500 Subject: [PATCH] Fixed syntax code --- .gitignore | 3 ++- lib/haml-jsx.js | 28 +++++++++++++--------------- lib/replace-matching.js | 20 +++++++++----------- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index b512c09..d5f19d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +package-lock.json diff --git a/lib/haml-jsx.js b/lib/haml-jsx.js index 212ac9d..75b6916 100644 --- a/lib/haml-jsx.js +++ b/lib/haml-jsx.js @@ -1,7 +1,7 @@ -var haml = require("haml"); -var replaceMatching = require("./replace-matching") +const haml = require("haml"); +const replaceMatching = require("./replace-matching") -var DUCK_PREFIX = "haml-jsx-prop:"; +const DUCK_PREFIX = "haml-jsx-prop:"; /// // In: HAML that has {...} JS embeds in it @@ -11,7 +11,7 @@ var DUCK_PREFIX = "haml-jsx-prop:"; // haml-jsx-prop:inline="" // function duckJS(source) { - var options = { + const options = { open: '{', close: '}', findRegex: /((\w+=)|){/ @@ -24,7 +24,7 @@ function duckJS(source) { // If there's a "someProperty=" (match2), keep it in the encoded // because when it gets decoded the property still needs to be there, // otherwise, just encode the JS with the {} - var encode = (match[2] || "") + '{'+js+'}'; + const encode = (match[2] || "") + '{'+js+'}'; // If there's a "someProperty=", we need to include the property name // in the "duck" property (so the end result is something like, @@ -32,8 +32,8 @@ function duckJS(source) { // If we don't (just leave it at haml-jsx-prop=""), and there are multiple // attributes with JS, they'll get eaten up by HAML as the same attribute. // If not, we need to give it something so the decoder can work with it later. - var key = DUCK_PREFIX + (match[2] || "inline="); - var value = '"'+encodeURIComponent(encode)+'"'; + const key = DUCK_PREFIX + (match[2] || "inline="); + const value = '"'+encodeURIComponent(encode)+'"'; return key+value; }); } @@ -44,7 +44,7 @@ function duckJS(source) { // and replace them with the result of that. // function unduckJS(source, replacement) { - var regex = DUCK_PREFIX+"\\w+=\"(.*?)\""; + const regex = DUCK_PREFIX+"\\w+=\"(.*?)\""; return source.replace(new RegExp(regex, 'mg'), (all, js) => { return replacement(decodeURIComponent(js)); }); @@ -56,7 +56,7 @@ function renderHAML(source) { //// // Normalize spaces (remove any offset the first line had) // - var firstIndent = source.match(/^([ \t]*)./m)[1]; + const firstIndent = source.match(/^([ \t]*)./m)[1]; source = source.replace(new RegExp("^"+firstIndent,"mg"), ""); //// @@ -75,7 +75,7 @@ function renderHAML(source) { }); // Render the HTML - var render = haml.render(source); + let render = haml.render(source); // class= to className= // Has to be done after HTML render because HAML will render @@ -88,7 +88,7 @@ function renderHAML(source) { } function renderHamlJSX(source, openDelim, closeDelim) { - var options = { + const options = { open: openDelim || "(~", close: closeDelim || "~)", }; @@ -98,12 +98,10 @@ function renderHamlJSX(source, openDelim, closeDelim) { haml = duckJS(haml); // Transform the now pure-HAML into HTML - var html = renderHAML(haml); + const html = renderHAML(haml); // Re-inject the JS, recursively rendering any HAML-JSX hiding in there... - return unduckJS(html, function (js) { - return renderHamlJSX(js, openDelim, closeDelim); - }); + return unduckJS(html, (js) => renderHamlJSX(js, openDelim, closeDelim)); }); } diff --git a/lib/replace-matching.js b/lib/replace-matching.js index 6d5b5b1..defdd69 100644 --- a/lib/replace-matching.js +++ b/lib/replace-matching.js @@ -5,16 +5,14 @@ // brackets, { and }. // function replaceMatching(string, options, replacementFunc) { - var open = options.open; - var close = options.close; - var findRegex = options.findRegex; + const { open, close, findRegex } = options; - var newString = ""; - var offset = 0; + let newString = ""; + let offset = 0; do { - var unsearched = string.substring(offset); - var match, head, foundIndex; + const unsearched = string.substring(offset); + let match, head, foundIndex; // Check if we are given a regex to find the opening brace if (findRegex) { @@ -45,10 +43,10 @@ function replaceMatching(string, options, replacementFunc) { offset += foundIndex; // Mark the beginning of this segment. - var idx = offset + open.length; + const idx = offset + open.length; // Keep adding to the segment until we match delimiters... - var brackets = 0; + let brackets = 0; do { if (string.substring(offset, offset+open.length) === open) { brackets += 1; @@ -63,8 +61,8 @@ function replaceMatching(string, options, replacementFunc) { throw new Error("Couldn't find closing match ('"+close+"') for delimiter '"+open+"'"); } - var segment = string.substring(idx, offset - 1); - var sub = replacementFunc(segment, match); + const segment = string.substring(idx, offset - 1); + const sub = replacementFunc(segment, match); newString += head + sub;