aboutsummaryrefslogtreecommitdiff
path: root/node_modules/js-yaml/lib/js-yaml/type/js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/js-yaml/lib/js-yaml/type/js')
-rw-r--r--node_modules/js-yaml/lib/js-yaml/type/js/function.js92
-rw-r--r--node_modules/js-yaml/lib/js-yaml/type/js/regexp.js60
-rw-r--r--node_modules/js-yaml/lib/js-yaml/type/js/undefined.js28
3 files changed, 180 insertions, 0 deletions
diff --git a/node_modules/js-yaml/lib/js-yaml/type/js/function.js b/node_modules/js-yaml/lib/js-yaml/type/js/function.js
new file mode 100644
index 0000000..3604e23
--- /dev/null
+++ b/node_modules/js-yaml/lib/js-yaml/type/js/function.js
@@ -0,0 +1,92 @@
+'use strict';
+
+var esprima;
+
+// Browserified version does not have esprima
+//
+// 1. For node.js just require module as deps
+// 2. For browser try to require mudule via external AMD system.
+// If not found - try to fallback to window.esprima. If not
+// found too - then fail to parse.
+//
+try {
+ // workaround to exclude package from browserify list.
+ var _require = require;
+ esprima = _require('esprima');
+} catch (_) {
+ /*global window */
+ if (typeof window !== 'undefined') esprima = window.esprima;
+}
+
+var Type = require('../../type');
+
+function resolveJavascriptFunction(data) {
+ if (data === null) return false;
+
+ try {
+ var source = '(' + data + ')',
+ ast = esprima.parse(source, { range: true });
+
+ if (ast.type !== 'Program' ||
+ ast.body.length !== 1 ||
+ ast.body[0].type !== 'ExpressionStatement' ||
+ (ast.body[0].expression.type !== 'ArrowFunctionExpression' &&
+ ast.body[0].expression.type !== 'FunctionExpression')) {
+ return false;
+ }
+
+ return true;
+ } catch (err) {
+ return false;
+ }
+}
+
+function constructJavascriptFunction(data) {
+ /*jslint evil:true*/
+
+ var source = '(' + data + ')',
+ ast = esprima.parse(source, { range: true }),
+ params = [],
+ body;
+
+ if (ast.type !== 'Program' ||
+ ast.body.length !== 1 ||
+ ast.body[0].type !== 'ExpressionStatement' ||
+ (ast.body[0].expression.type !== 'ArrowFunctionExpression' &&
+ ast.body[0].expression.type !== 'FunctionExpression')) {
+ throw new Error('Failed to resolve function');
+ }
+
+ ast.body[0].expression.params.forEach(function (param) {
+ params.push(param.name);
+ });
+
+ body = ast.body[0].expression.body.range;
+
+ // Esprima's ranges include the first '{' and the last '}' characters on
+ // function expressions. So cut them out.
+ if (ast.body[0].expression.body.type === 'BlockStatement') {
+ /*eslint-disable no-new-func*/
+ return new Function(params, source.slice(body[0] + 1, body[1] - 1));
+ }
+ // ES6 arrow functions can omit the BlockStatement. In that case, just return
+ // the body.
+ /*eslint-disable no-new-func*/
+ return new Function(params, 'return ' + source.slice(body[0], body[1]));
+}
+
+function representJavascriptFunction(object /*, style*/) {
+ return object.toString();
+}
+
+function isFunction(object) {
+ return Object.prototype.toString.call(object) === '[object Function]';
+}
+
+module.exports = new Type('tag:yaml.org,2002:js/function', {
+ kind: 'scalar',
+ resolve: resolveJavascriptFunction,
+ construct: constructJavascriptFunction,
+ predicate: isFunction,
+ represent: representJavascriptFunction
+});
diff --git a/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js b/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
new file mode 100644
index 0000000..43fa470
--- /dev/null
+++ b/node_modules/js-yaml/lib/js-yaml/type/js/regexp.js
@@ -0,0 +1,60 @@
+'use strict';
+
+var Type = require('../../type');
+
+function resolveJavascriptRegExp(data) {
+ if (data === null) return false;
+ if (data.length === 0) return false;
+
+ var regexp = data,
+ tail = /\/([gim]*)$/.exec(data),
+ modifiers = '';
+
+ // if regexp starts with '/' it can have modifiers and must be properly closed
+ // `/foo/gim` - modifiers tail can be maximum 3 chars
+ if (regexp[0] === '/') {
+ if (tail) modifiers = tail[1];
+
+ if (modifiers.length > 3) return false;
+ // if expression starts with /, is should be properly terminated
+ if (regexp[regexp.length - modifiers.length - 1] !== '/') return false;
+ }
+
+ return true;
+}
+
+function constructJavascriptRegExp(data) {
+ var regexp = data,
+ tail = /\/([gim]*)$/.exec(data),
+ modifiers = '';
+
+ // `/foo/gim` - tail can be maximum 4 chars
+ if (regexp[0] === '/') {
+ if (tail) modifiers = tail[1];
+ regexp = regexp.slice(1, regexp.length - modifiers.length - 1);
+ }
+
+ return new RegExp(regexp, modifiers);
+}
+
+function representJavascriptRegExp(object /*, style*/) {
+ var result = '/' + object.source + '/';
+
+ if (object.global) result += 'g';
+ if (object.multiline) result += 'm';
+ if (object.ignoreCase) result += 'i';
+
+ return result;
+}
+
+function isRegExp(object) {
+ return Object.prototype.toString.call(object) === '[object RegExp]';
+}
+
+module.exports = new Type('tag:yaml.org,2002:js/regexp', {
+ kind: 'scalar',
+ resolve: resolveJavascriptRegExp,
+ construct: constructJavascriptRegExp,
+ predicate: isRegExp,
+ represent: representJavascriptRegExp
+});
diff --git a/node_modules/js-yaml/lib/js-yaml/type/js/undefined.js b/node_modules/js-yaml/lib/js-yaml/type/js/undefined.js
new file mode 100644
index 0000000..95b5569
--- /dev/null
+++ b/node_modules/js-yaml/lib/js-yaml/type/js/undefined.js
@@ -0,0 +1,28 @@
+'use strict';
+
+var Type = require('../../type');
+
+function resolveJavascriptUndefined() {
+ return true;
+}
+
+function constructJavascriptUndefined() {
+ /*eslint-disable no-undefined*/
+ return undefined;
+}
+
+function representJavascriptUndefined() {
+ return '';
+}
+
+function isUndefined(object) {
+ return typeof object === 'undefined';
+}
+
+module.exports = new Type('tag:yaml.org,2002:js/undefined', {
+ kind: 'scalar',
+ resolve: resolveJavascriptUndefined,
+ construct: constructJavascriptUndefined,
+ predicate: isUndefined,
+ represent: representJavascriptUndefined
+});