Skip to content

Commit

Permalink
Merge pull request #73 from groupon/mo-support-node-10
Browse files Browse the repository at this point in the history
Fix audited packages, support Node 10, drop support for Node 4
  • Loading branch information
jkrems committed Oct 29, 2018
2 parents 3da32c1 + 16800e3 commit 0807323
Show file tree
Hide file tree
Showing 10 changed files with 2,168 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
@@ -1,3 +1,3 @@
{
"extends": "groupon/es5"
"extends": "groupon/node6"
}
3 changes: 1 addition & 2 deletions .gitignore
@@ -1,5 +1,4 @@
/package-lock.json
/yarn.lock
/.nyc_output
node_modules/
npm-debug.log
/tmp
10 changes: 6 additions & 4 deletions .travis.yml
@@ -1,12 +1,14 @@
language: node_js
node_js:
- 4.6.1
- 6.11.5
- 8.9.0
- 6.14.3
- 8.11.3
- 10.5.0
deploy:
- provider: script
script: ./node_modules/.bin/nlm release
skip_cleanup: true
'on':
branch: master
node: 8.9.0
node: 10.5.0
before_install:
- npm i -g npm@^6
4 changes: 2 additions & 2 deletions lib/cson-parser.js
Expand Up @@ -32,8 +32,8 @@

'use strict';

var stringify = require('./stringify');
var parse = require('./parse');
const stringify = require('./stringify');
const parse = require('./parse');

module.exports = {
stringify: stringify,
Expand Down
62 changes: 30 additions & 32 deletions lib/parse.js
Expand Up @@ -32,8 +32,8 @@

'use strict';

var runInThisContext = require('vm').runInThisContext;
var nodes = require('coffeescript').nodes;
const runInThisContext = require('vm').runInThisContext;
const nodes = require('coffeescript').nodes;

function defaultReviver(key, value) {
return value;
Expand All @@ -44,23 +44,23 @@ function getFunctionNameIE(fn) {
}

function nodeTypeString(csNode) {
var ref = csNode.constructor.name;
const ref = csNode.constructor.name;
return ref != null ? ref : getFunctionNameIE(csNode.constructor);
}

function syntaxErrorMessage(csNode, msg) {
var ref = csNode.locationData;
var lineIdx = ref.first_line;
var columnIdx = ref.first_column;
var line = '';
var column = '';
const ref = csNode.locationData;
const lineIdx = ref.first_line;
const columnIdx = ref.first_column;
let line = '';
let column = '';
if (lineIdx != null) {
line = lineIdx + 1;
}
if (columnIdx != null) {
column = columnIdx + 1;
}
return 'Syntax error on line ' + line + ', column ' + column + ': ' + msg;
return `Syntax error on line ${line}, column ${column}: ${msg}`;
}

function parseStringLiteral(literal) {
Expand All @@ -72,11 +72,11 @@ function parseRegExpLiteral(literal) {
}

function transformKey(csNode) {
var type = nodeTypeString(csNode);
const type = nodeTypeString(csNode);
if (type !== 'Value') {
throw new SyntaxError(syntaxErrorMessage(csNode, type + ' used as key'));
throw new SyntaxError(syntaxErrorMessage(csNode, `${type} used as key`));
}
var value = csNode.base.value;
const value = csNode.base.value;
switch (value.charAt(0)) {
case "'":
case '"':
Expand All @@ -86,10 +86,9 @@ function transformKey(csNode) {
}
}

var nodeTransforms = {
const nodeTransforms = {
Block: function Block(node, transformNode) {
var expressions;
expressions = node.expressions;
const expressions = node.expressions;
if (!expressions || expressions.length !== 1) {
throw new SyntaxError(
syntaxErrorMessage(node, 'One top level value expected')
Expand All @@ -113,7 +112,7 @@ var nodeTransforms = {
return null;
},
Literal: function Literal(node) {
var value = node.value;
const value = node.value;
try {
switch (value.charAt(0)) {
case "'":
Expand Down Expand Up @@ -141,22 +140,22 @@ var nodeTransforms = {
return node.objects.map(transformNode);
},
Obj: function Obj(node, transformNode, reviver) {
return node.properties.reduce(function addProperty(outObject, property) {
var variable = property.variable;
var value = property.value;
return node.properties.reduce((outObject, property) => {
const variable = property.variable;
let value = property.value;
if (!variable) {
return outObject;
}
var keyName = transformKey(variable);
const keyName = transformKey(variable);
value = transformNode(value);
outObject[keyName] = reviver.call(outObject, keyName, value);
return outObject;
}, {});
},
Op: function Op(node, transformNode) {
if (node.second != null) {
var left = transformNode(node.first);
var right = transformNode(node.second);
const left = transformNode(node.first);
const right = transformNode(node.second);
switch (node.operator) {
case '-':
return left - right;
Expand All @@ -182,7 +181,7 @@ var nodeTransforms = {
return left >> right;
default:
throw new SyntaxError(
syntaxErrorMessage(node, 'Unknown binary operator ' + node.operator)
syntaxErrorMessage(node, `Unknown binary operator ${node.operator}`)
);
}
} else {
Expand All @@ -193,14 +192,13 @@ var nodeTransforms = {
return ~transformNode(node.first);
default:
throw new SyntaxError(
syntaxErrorMessage(node, 'Unknown unary operator ' + node.operator)
syntaxErrorMessage(node, `Unknown unary operator ${node.operator}`)
);
}
}
},
Parens: function Parens(node, transformNode) {
var expressions;
expressions = node.body.expressions;
const expressions = node.body.expressions;
if (!expressions || expressions.length !== 1) {
throw new SyntaxError(
syntaxErrorMessage(node, 'Parenthesis may only contain one expression')
Expand All @@ -215,22 +213,22 @@ function parse(source, reviver) {
reviver = defaultReviver;
}
function transformNode(csNode) {
var type = nodeTypeString(csNode);
var transform = nodeTransforms[type];
const type = nodeTypeString(csNode);
const transform = nodeTransforms[type];
if (!transform) {
throw new SyntaxError(syntaxErrorMessage(csNode, 'Unexpected ' + type));
throw new SyntaxError(syntaxErrorMessage(csNode, `Unexpected ${type}`));
}
return transform(csNode, transformNode, reviver);
}
if (typeof reviver !== 'function') {
throw new TypeError('reviver has to be a function');
}
var coffeeAst = nodes(source.toString('utf8'));
var parsed = transformNode(coffeeAst);
const coffeeAst = nodes(source.toString('utf8'));
const parsed = transformNode(coffeeAst);
if (reviver === defaultReviver) {
return parsed;
}
var contextObj = {};
const contextObj = {};
contextObj[''] = parsed;
return reviver.call(contextObj, '', parsed);
}
Expand Down
72 changes: 37 additions & 35 deletions lib/stringify.js
Expand Up @@ -32,16 +32,16 @@

'use strict';

var jsIdentifierRE = /^[a-z_$][a-z0-9_$]*$/i;
var tripleQuotesRE = /'''/g;
var SPACES = ' ';
const jsIdentifierRE = /^[a-z_$][a-z0-9_$]*$/i;
const tripleQuotesRE = /'''/g;
const SPACES = ' ';

function contains(str1, str2) {
return str1.indexOf(str2) >= 0;
}

function newlineWrap(str) {
return str && '\n' + str + '\n';
return str && `\n${str}\n`;
}

function isObject(obj) {
Expand All @@ -53,7 +53,7 @@ function parseIndent(indent) {
case 'string':
return indent.slice(0, 10);
case 'number':
var n = Math.max(0, Math.min(10, Math.floor(indent)));
const n = Math.max(0, Math.min(10, Math.floor(indent)));
return SPACES.slice(0, n);
default:
return 0;
Expand All @@ -75,14 +75,10 @@ function indentLines(indent, str) {
}

function singleQuoteStringify(str) {
return (
"'" +
JSON.stringify(str)
.slice(1, -1)
.replace(/\\"/g, '"')
.replace(/'/g, "\\'") +
"'"
);
return `'${JSON.stringify(str)
.slice(1, -1)
.replace(/\\"/g, '"')
.replace(/'/g, "\\'")}'`;
}

function quoteType(str) {
Expand All @@ -96,72 +92,75 @@ function onelineStringify(str) {
}

function buildKeyPairs(visitNode, indent, obj) {
return Object.keys(obj).map(function addKey(key) {
var value = obj[key];
return Object.keys(obj).map(key => {
const value = obj[key];
if (!key.match(jsIdentifierRE)) {
key = onelineStringify(key);
}
var serializedValue = visitNode(value, {
let serializedValue = visitNode(value, {
bracesRequired: !indent,
});
if (indent) {
serializedValue =
isObject(value) && Object.keys(value).length > 0
? '\n' + indentLines(indent, serializedValue)
: ' ' + serializedValue;
? `\n${indentLines(indent, serializedValue)}`
: ` ${serializedValue}`;
}
return key + ':' + serializedValue;
return `${key}:${serializedValue}`;
});
}

function visitArray(visitNode, indent, arr) {
var items = arr.map(function visitElement(value) {
const items = arr.map(value => {
return visitNode(value, {
bracesRequired: true,
});
});
var serializedItems = indent
const serializedItems = indent
? newlineWrap(indentLines(indent, items.join('\n')))
: items.join(',');
return '[' + serializedItems + ']';
return `[${serializedItems}]`;
}

function visitObject(visitNode, indent, obj, arg) {
var bracesRequired = arg.bracesRequired;
var keypairs = buildKeyPairs(visitNode, indent, obj);
const bracesRequired = arg.bracesRequired;
const keypairs = buildKeyPairs(visitNode, indent, obj);

if (keypairs.length === 0) return '{}';

if (indent) {
var keyPairLines = keypairs.join('\n');
const keyPairLines = keypairs.join('\n');
if (bracesRequired) {
return '{' + newlineWrap(indentLines(indent, keyPairLines)) + '}';
return `{${newlineWrap(indentLines(indent, keyPairLines))}}`;
}
return keyPairLines;
}

var serializedKeyPairs = keypairs.join(',');
const serializedKeyPairs = keypairs.join(',');
if (bracesRequired) {
return '{' + serializedKeyPairs + '}';
return `{${serializedKeyPairs}}`;
}
return serializedKeyPairs;
}

function visitString(visitNode, indent, str) {
var string;
if (str.indexOf('\n') === -1 || !indent) {
return onelineStringify(str);
}
string = str.replace(/\\/g, '\\\\').replace(tripleQuotesRE, "\\'''");
return "'''" + newlineWrap(indentLines(indent, string)) + "'''";
const string = str.replace(/\\/g, '\\\\').replace(tripleQuotesRE, "\\'''");
return `'''${newlineWrap(indentLines(indent, string))}'''`;
}

// disabling consistent return so cson parser handles undefined like JSON.stringify does
function stringify(data, visitor, indent) {
if (typeof data === 'function' || typeof data === 'undefined')
if (typeof data === 'function' || typeof data === 'undefined') {
// eslint-disable-next-line consistent-return
return undefined;
}

indent = parseIndent(indent);

var normalized = JSON.parse(JSON.stringify(data, visitor));
const normalized = JSON.parse(JSON.stringify(data, visitor));

function visitNode(node, options) {
if (options == null) {
Expand All @@ -170,11 +169,11 @@ function stringify(data, visitor, indent) {

switch (typeof node) {
case 'boolean':
return '' + node;
return `${node}`;

case 'number':
if (isFinite(node)) {
return '' + node;
return `${node}`;
}
return 'null';

Expand All @@ -190,10 +189,13 @@ function stringify(data, visitor, indent) {
return visitObject(visitNode, indent, node, options);

default:
// eslint-disable-next-line consistent-return
return undefined;
}
}

// eslint-disable-next-line consistent-return
return visitNode(normalized);
}

module.exports = stringify;

0 comments on commit 0807323

Please sign in to comment.