Skip to content

Commit

Permalink
replacing JSON with JSON5 for better error messaging
Browse files Browse the repository at this point in the history
  • Loading branch information
e2tha-e committed Mar 18, 2016
1 parent 2a297f4 commit c8acb5a
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 9 deletions.
28 changes: 24 additions & 4 deletions core/lib/list_item_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var list_item_hunter = function () {

var extend = require('util')._extend,
JSON = require('json5'),
pa = require('./pattern_assembler'),
smh = require('./style_modifier_hunter'),
pattern_assembler = new pa(),
Expand Down Expand Up @@ -44,7 +45,13 @@ var list_item_hunter = function () {
}

//check for a local listitems.json file
var listData = JSON.parse(JSON.stringify(patternlab.listitems));
var listData;
try {
listData = JSON.parse(JSON.stringify(patternlab.listitems));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.abspath);
console.log(err);
}
listData = pattern_assembler.merge_data(listData, pattern.listitems);

//iterate over each copied block, rendering its contents along with pattenlab.listitems[i]
Expand All @@ -54,8 +61,15 @@ var list_item_hunter = function () {

//combine listItem data with pattern data with global data
var itemData = listData['' + items.indexOf(loopNumberString)]; //this is a property like "2"
var globalData = JSON.parse(JSON.stringify(patternlab.data));
var localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
var globalData;
var localData;
try {
globalData = JSON.parse(JSON.stringify(patternlab.data));
localData = JSON.parse(JSON.stringify(pattern.jsonFileData));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.abspath);
console.log(err);
}

var allData = pattern_assembler.merge_data(globalData, localData);
allData = pattern_assembler.merge_data(allData, itemData !== undefined ? itemData[i] : {}); //itemData could be undefined if the listblock contains no partial, just markup
Expand All @@ -71,7 +85,13 @@ var list_item_hunter = function () {
var partialPattern = pattern_assembler.get_pattern_by_key(partialName, patternlab);

//create a copy of the partial so as to not pollute it after the get_pattern_by_key call.
var cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
var cleanPartialPattern;
try {
cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.abspath);
console.log(err);
}

//if partial has style modifier data, replace the styleModifier value
if (foundPartials[j].indexOf(':') > -1) {
Expand Down
6 changes: 4 additions & 2 deletions core/lib/parameter_hunter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
var parameter_hunter = function () {

var extend = require('util')._extend,
JSON = require('json5'),
pa = require('./pattern_assembler'),
smh = require('./style_modifier_hunter'),
pattern_assembler = new pa(),
Expand Down Expand Up @@ -223,8 +224,9 @@ var parameter_hunter = function () {
paramData = JSON.parse(paramStringWellFormed);
globalData = JSON.parse(JSON.stringify(patternlab.data));
localData = JSON.parse(JSON.stringify(pattern.jsonFileData || {}));
} catch (e) {
console.log(e);
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.abspath);
console.log(err);
}

var allData = pattern_assembler.merge_data(globalData, localData);
Expand Down
12 changes: 11 additions & 1 deletion core/lib/pattern_assembler.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ var pattern_assembler = function () {
}

function parseDataLinksHelper(patternlab, obj, key) {
var JSON = require('json5');
var linkRE, dataObjAsString, linkMatches, expandedLink;

linkRE = /link\.[A-z0-9-_]+/g;
Expand All @@ -349,7 +350,16 @@ var pattern_assembler = function () {
}
}
}
return JSON.parse(dataObjAsString);

var dataObj;
try {
dataObj = JSON.parse(dataObjAsString);
} catch (err) {
console.log('There was an error parsing JSON for ' + key);
console.log(err);
}

return dataObj;
}

//look for pattern links included in data files.
Expand Down
9 changes: 8 additions & 1 deletion core/lib/patternlab.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var patternlab_engine = function (config) {
'use strict';

var path = require('path'),
JSON = require('json5'),
fs = require('fs-extra'),
diveSync = require('diveSync'),
of = require('./object_factory'),
Expand Down Expand Up @@ -191,7 +192,13 @@ var patternlab_engine = function (config) {
pattern.lineageR = lineageRArray;

//render the pattern, but first consolidate any data we may have
var allData = JSON.parse(JSON.stringify(patternlab.data));
var allData;
try {
allData = JSON.parse(JSON.stringify(patternlab.data));
} catch (err) {
console.log('There was an error parsing JSON for ' + pattern.abspath);
console.log(err);
}
allData = pattern_assembler.merge_data(allData, pattern.jsonFileData);

//also add the cachebuster value. slight chance this could collide with a user that has defined cacheBuster as a value
Expand Down
4 changes: 3 additions & 1 deletion test/parameter_hunter_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,14 @@
patternlab.patterns[0].template = "<p>{{foo}}</p>";
patternlab.patterns[0].extendedTemplate = patternlab.patterns[0].template;

currentPattern.abspath = __filename;
currentPattern.template = "{{> molecules-single-comment( missing-val: , : missing-key, : , , foo: \"Hello World\") }}";
currentPattern.extendedTemplate = currentPattern.template;
currentPattern.parameteredPartials[0] = currentPattern.template;

console.log('\nPattern Lab should catch JSON.parse() errors and output useful debugging information...');
parameter_hunter.find_parameters(currentPattern, patternlab);
test.equals(currentPattern.extendedTemplate, '<p>Hello World</p>');
test.equals(currentPattern.extendedTemplate, '<p></p>');

test.done();
}
Expand Down

0 comments on commit c8acb5a

Please sign in to comment.