diff --git a/core/lib/annotation_exporter.js b/core/lib/annotation_exporter.js index d12054145..bd9cb72e8 100644 --- a/core/lib/annotation_exporter.js +++ b/core/lib/annotation_exporter.js @@ -3,7 +3,6 @@ var path = require('path'), glob = require('glob'), fs = require('fs-extra'), - JSON5 = require('json5'), _ = require('lodash'), mp = require('./markdown_parser'); @@ -30,7 +29,7 @@ var annotations_exporter = function (pl) { oldAnnotations = oldAnnotations.replace('};', '}'); try { - var oldAnnotationsJSON = JSON5.parse(oldAnnotations); + var oldAnnotationsJSON = JSON.parse(oldAnnotations); } catch (ex) { console.log('There was an error parsing JSON for ' + paths.source.annotations + 'annotations.js'); console.log(ex); diff --git a/core/lib/json_copy.js b/core/lib/json_copy.js new file mode 100644 index 000000000..84c9e249d --- /dev/null +++ b/core/lib/json_copy.js @@ -0,0 +1,14 @@ +"use strict"; + +const plutils = require('./utilities'); +const json_copy = (data, callee) => { + try { + return JSON.parse(JSON.stringify(data)); + } catch (e) { + //this is unlikely to be hit due to the passed in data already being loaded using JSON parsers + plutils.error(`JSON provided by ${callee} is invalid and cannot be copied`); + return {}; + } +}; + +module.exports = json_copy; diff --git a/core/lib/list_item_hunter.js b/core/lib/list_item_hunter.js index be55f35c5..d04b6d0a7 100644 --- a/core/lib/list_item_hunter.js +++ b/core/lib/list_item_hunter.js @@ -3,10 +3,10 @@ var list_item_hunter = function () { var extend = require('util')._extend, - JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), plutils = require('./utilities'), + jsonCopy = require('./json_copy'), Pattern = require('./object_factory').Pattern; var pattern_assembler = new pa(), @@ -42,7 +42,7 @@ var list_item_hunter = function () { //check for a local listitems.json file var listData; try { - listData = JSON5.parse(JSON5.stringify(patternlab.listitems)); + listData = jsonCopy(patternlab.listitems, 'config.paths.source.data listitems'); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); @@ -62,8 +62,8 @@ var list_item_hunter = function () { var globalData; var localData; try { - globalData = JSON5.parse(JSON5.stringify(patternlab.data)); - localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData)); + globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data'); + localData = jsonCopy(pattern.jsonFileData, `${pattern.patternPartial} data`); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); @@ -87,7 +87,8 @@ var list_item_hunter = function () { //create a copy of the partial so as to not pollute it after the get_pattern_by_key call. var cleanPartialPattern; try { - cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern)); + cleanPartialPattern = JSON.parse(JSON.stringify(partialPattern)); + cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partialName}`); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); diff --git a/core/lib/parameter_hunter.js b/core/lib/parameter_hunter.js index 6f2d50f1e..44f33adeb 100644 --- a/core/lib/parameter_hunter.js +++ b/core/lib/parameter_hunter.js @@ -3,11 +3,11 @@ var parameter_hunter = function () { var extend = require('util')._extend, - JSON5 = require('json5'), pa = require('./pattern_assembler'), smh = require('./style_modifier_hunter'), plutils = require('./utilities'), style_modifier_hunter = new smh(), + jsonCopy = require('./json_copy'), pattern_assembler = new pa(); /** @@ -19,7 +19,7 @@ var parameter_hunter = function () { * The steps on a high-level are as follows: * * Further escape all escaped quotes and colons. Use the string * representation of their unicodes for this. This has the added bonus - * of being interpreted correctly by JSON5.parse() without further + * of being interpreted correctly by JSON.parse() without further * modification. This will be useful later in the function. * * Once escaped quotes are out of the way, we know the remaining quotes * are either key/value wrappers or wrapped within those wrappers. We know @@ -260,9 +260,9 @@ var parameter_hunter = function () { var localData = {}; try { - paramData = JSON5.parse(paramStringWellFormed); - globalData = JSON5.parse(JSON5.stringify(patternlab.data)); - localData = JSON5.parse(JSON5.stringify(pattern.jsonFileData || {})); + paramData = JSON.parse(paramStringWellFormed); + globalData = jsonCopy(patternlab.data, 'config.paths.source.data global data'); + localData = jsonCopy(pattern.jsonFileData || {}, `pattern ${pattern.patternPartial} data`); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); diff --git a/core/lib/pattern_assembler.js b/core/lib/pattern_assembler.js index b85867d88..dc76832a6 100644 --- a/core/lib/pattern_assembler.js +++ b/core/lib/pattern_assembler.js @@ -13,8 +13,8 @@ var path = require('path'), lih = require('./list_item_hunter'), smh = require('./style_modifier_hunter'), ph = require('./parameter_hunter'), - ch = require('./changes_hunter'), - JSON5 = require('json5'); + jsonCopy = require('./json_copy'), + ch = require('./changes_hunter'); const markdown_parser = new mp(); const changes_hunter = new ch(); @@ -476,7 +476,7 @@ var pattern_assembler = function () { //complete assembly of extended template //create a copy of the partial so as to not pollute it after the getPartial call. var partialPattern = getPartial(partial, patternlab); - var cleanPartialPattern = JSON5.parse(JSON5.stringify(partialPattern)); + var cleanPartialPattern = jsonCopy(partialPattern, `partial pattern ${partial}`); //if partial has style modifier data, replace the styleModifier value if (currentPattern.stylePartials && currentPattern.stylePartials.length > 0) { @@ -494,7 +494,7 @@ var pattern_assembler = function () { linkRE = /(?:'|")(link\.[A-z0-9-_]+)(?:'|")/g; //stringify the passed in object - dataObjAsString = JSON5.stringify(obj); + dataObjAsString = JSON.stringify(obj); if (!dataObjAsString) { return obj; } //find matches @@ -533,7 +533,7 @@ var pattern_assembler = function () { var dataObj; try { - dataObj = JSON5.parse(dataObjAsString); + dataObj = JSON.parse(dataObjAsString); } catch (err) { console.log('There was an error parsing JSON for ' + key); console.log(err); diff --git a/core/lib/patternlab.js b/core/lib/patternlab.js index cfdc88c0c..f05ef9d57 100644 --- a/core/lib/patternlab.js +++ b/core/lib/patternlab.js @@ -21,6 +21,7 @@ var diveSync = require('diveSync'), fs = require('fs-extra'), packageInfo = require('../../package.json'), plutils = require('./utilities'), + jsonCopy = require('./json_copy'), PatternGraph = require('./pattern_graph').PatternGraph; //register our log events @@ -146,8 +147,7 @@ inherits(PatternLabEventEmitter, EventEmitter); var patternlab_engine = function (config) { 'use strict'; - var JSON5 = require('json5'), - pa = require('./pattern_assembler'), + var pa = require('./pattern_assembler'), pe = require('./pattern_exporter'), lh = require('./lineage_hunter'), ui = require('./ui_builder'), @@ -180,7 +180,7 @@ var patternlab_engine = function (config) { console.log(patternlab.package.version); } - function getSupportedTemplateExtensions(){ + function getSupportedTemplateExtensions() { return patternlab.engines.getSupportedFileExtensions(); } @@ -370,7 +370,7 @@ var patternlab_engine = function (config) { //render the pattern, but first consolidate any data we may have var allData; try { - allData = JSON5.parse(JSON5.stringify(patternlab.data)); + allData = jsonCopy(patternlab.data, 'config.paths.source.data global data'); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); @@ -421,7 +421,7 @@ var patternlab_engine = function (config) { var allFooterData; try { - allFooterData = JSON5.parse(JSON5.stringify(patternlab.data)); + allFooterData = jsonCopy(patternlab.data, 'config.paths.source.data global data'); } catch (err) { console.log('There was an error parsing JSON for ' + pattern.relPath); console.log(err); diff --git a/core/lib/ui_builder.js b/core/lib/ui_builder.js index c62ea9193..21865a2dd 100644 --- a/core/lib/ui_builder.js +++ b/core/lib/ui_builder.js @@ -1,7 +1,6 @@ "use strict"; var path = require('path'); -var JSON5 = require('json5'); var fs = require('fs-extra'); var ae = require('./annotation_exporter'); var of = require('./object_factory'); @@ -10,6 +9,7 @@ var pattern_assembler = require('./pattern_assembler')(); var plutils = require('./utilities'); var eol = require('os').EOL; var _ = require('lodash'); +var jsonCopy = require('./json_copy'); var ui_builder = function () { @@ -435,7 +435,7 @@ var ui_builder = function () { var allFooterData; try { - allFooterData = JSON5.parse(JSON5.stringify(patternlab.data)); + allFooterData = jsonCopy(patternlab.data, 'config.paths.source.data plus patterns data'); } catch (err) { console.log('There was an error parsing JSON for patternlab.data'); console.log(err); diff --git a/package.json b/package.json index 0f615b509..46f0d0fa8 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,6 @@ "glob": "^7.0.0", "js-beautify": "^1.6.3", "js-yaml": "^3.6.1", - "json5": "^0.5.0", "lodash": "~4.13.1", "markdown-it": "^6.0.1", "node-fetch": "^1.6.0",