Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed syntax code #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
package-lock.json
28 changes: 13 additions & 15 deletions lib/haml-jsx.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,7 +11,7 @@ var DUCK_PREFIX = "haml-jsx-prop:";
// haml-jsx-prop:inline="<URI-ENCODED-JS>"
//
function duckJS(source) {
var options = {
const options = {
open: '{',
close: '}',
findRegex: /((\w+=)|){/
Expand All @@ -24,16 +24,16 @@ 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,
// haml-jsx-prop:someProperty="someProperty={<JS ENCODED>}")
// 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;
});
}
Expand All @@ -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));
});
Expand All @@ -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"), "");

////
Expand All @@ -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
Expand All @@ -88,7 +88,7 @@ function renderHAML(source) {
}

function renderHamlJSX(source, openDelim, closeDelim) {
var options = {
const options = {
open: openDelim || "(~",
close: closeDelim || "~)",
};
Expand All @@ -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));
});
}

Expand Down
20 changes: 9 additions & 11 deletions lib/replace-matching.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand Down