@mapbox/expression-jamsession
Advanced tools
Comparing version 0.4.2 to 0.5.0
# Changelog | ||
## 0.5.0 | ||
- Enable support for object literals. | ||
- Updates style spec version to `13.21.0` and removes block list predicated on object support. | ||
- Fix handling of the `literal` expression, which now supports any literal value (arrays, objects, strings, etc), in accordance with the style spec. | ||
## 0.4.1 | ||
@@ -4,0 +10,0 @@ |
'use strict'; | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
var jsep = require('jsep'); | ||
var jsep = _interopDefault(require('jsep')); | ||
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } | ||
var jsep__default = /*#__PURE__*/_interopDefaultLegacy(jsep); | ||
const OCURLY_CODE = 123; // { | ||
const CCURLY_CODE = 125; // } | ||
const OBJECT_EXP = 'ObjectExpression'; | ||
const PROPERTY = 'Property'; | ||
var index$1 = { | ||
name: 'object', | ||
init(jsep) { | ||
jsep.addBinaryOp(':', 0.5); | ||
// Object literal support | ||
function gobbleObjectExpression(env) { | ||
if (this.code === OCURLY_CODE) { | ||
this.index++; | ||
const properties = this.gobbleArguments(CCURLY_CODE) | ||
.map((arg) => { | ||
if (arg.type === jsep.IDENTIFIER) { | ||
return { | ||
type: PROPERTY, | ||
computed: false, | ||
key: arg, | ||
shorthand: true, | ||
}; | ||
} | ||
if (arg.type === jsep.BINARY_EXP) { | ||
const computed = arg.left.type === jsep.ARRAY_EXP; | ||
return { | ||
type: PROPERTY, | ||
computed, | ||
key: computed | ||
? arg.left.elements[0] | ||
: arg.left, | ||
value: arg.right, | ||
shorthand: false, | ||
}; | ||
} | ||
// complex value (i.e. ternary, spread) | ||
return arg; | ||
}); | ||
env.node = { | ||
type: OBJECT_EXP, | ||
properties, | ||
}; | ||
} | ||
} | ||
jsep.hooks.add('gobble-token', gobbleObjectExpression); | ||
jsep.hooks.add('after-token', gobbleObjectExpression); | ||
} | ||
}; | ||
function handleSyntaxErrors(error) { | ||
var input = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ''; | ||
var newError = new Error('Syntax error'); | ||
@@ -30,13 +82,26 @@ newError.type = 'SyntaxError'; | ||
// GL expressions use ^ for exponentiation, while JS uses **. | ||
// 15 is precedence of ** operator in JS. | ||
jsep.addBinaryOp('^', 15); | ||
jsep__default['default'].addBinaryOp('^', 15); | ||
jsep__default['default'].plugins.register(index$1); | ||
function handleLiteralArgument(arg) { | ||
switch (arg.type) { | ||
case 'Literal': | ||
return arg.value; | ||
case 'ArrayExpression': | ||
return arg.elements.map(function (item) { | ||
if (item.type === 'Literal') return item.value; | ||
return handleLiteralArgument(item); | ||
}); | ||
return arg.elements.map(handleLiteralArgument); | ||
case 'ObjectExpression': | ||
{ | ||
var object = {}; | ||
arg.properties.forEach(function (property) { | ||
var k = handleLiteralArgument(property.key); | ||
var v = handleLiteralArgument(property.value); | ||
object[k] = v; | ||
}); | ||
return object; | ||
} | ||
default: | ||
@@ -54,4 +119,3 @@ throw handleSyntaxErrors(new Error('Invalid syntax')); | ||
if (input.name !== undefined) return input.name; | ||
var expressionOperator = void 0; | ||
var expressionOperator; | ||
var expressionArguments = []; | ||
@@ -61,2 +125,13 @@ | ||
return input.elements.map(astToExpression); | ||
} // Collect all properties into a single object | ||
if (input.type === 'ObjectExpression') { | ||
var object = {}; | ||
input.properties.forEach(function (property) { | ||
var k = astToExpression(property.key); | ||
var v = astToExpression(property.value); | ||
object[k] = v; | ||
}); | ||
return object; | ||
} | ||
@@ -70,6 +145,5 @@ | ||
if (input.type === 'BinaryExpression') { | ||
expressionOperator = input.operator === '&' ? 'concat' : input.operator; | ||
expressionOperator = input.operator === '&' ? 'concat' : input.operator; // Collapse concat arguments, in case the & operator was used to join | ||
// more than two successive strings. | ||
// Collapse concat arguments, in case the & operator was used to join | ||
// more than two successive strings. | ||
var addBinaryArgument = function addBinaryArgument(arg) { | ||
@@ -82,2 +156,3 @@ if (expressionOperator === 'concat' && Array.isArray(arg) && arg[0] === 'concat') { | ||
}; | ||
addBinaryArgument(astToExpression(input.left)); | ||
@@ -97,6 +172,6 @@ addBinaryArgument(astToExpression(input.right)); | ||
} | ||
} | ||
} // Change undescores in expression operators to hyphens, reversing the | ||
// transformation below. | ||
// Change undescores in expression operators to hyphens, reversing the | ||
// transformation below. | ||
if (/[a-z]+_[a-z]+/.test(expressionOperator)) { | ||
@@ -116,11 +191,11 @@ expressionOperator = expressionOperator.replace(/([a-z]+)_([a-z]+)/, '$1-$2'); | ||
throw new Error('input must be a string'); | ||
} | ||
} // Change hyphens in expression operators to underscores. This allows JS | ||
// parsing to work, but then needs to be reversed above. | ||
// Change hyphens in expression operators to underscores. This allows JS | ||
// parsing to work, but then needs to be reversed above. | ||
input = input.replace(/([a-z]+)-([a-z]+)\(/, '$1_$2('); | ||
var ast; | ||
var ast = void 0; | ||
try { | ||
ast = jsep(input); | ||
ast = jsep__default['default'](input); | ||
} catch (syntaxError) { | ||
@@ -134,8 +209,22 @@ throw handleSyntaxErrors(syntaxError, input); | ||
function _typeof(obj) { | ||
"@babel/helpers - typeof"; | ||
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { | ||
_typeof = function (obj) { | ||
return typeof obj; | ||
}; | ||
} else { | ||
_typeof = function (obj) { | ||
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; | ||
}; | ||
} | ||
return _typeof(obj); | ||
} | ||
// DO NOT MODIFY DIRECTLY! | ||
// This file is generated by scripts/build-operator-whitelist.js | ||
// This file is generated by scripts/build-operator-list.js | ||
// from the Mapbox GL Style Spec. | ||
var operators = new Set(); | ||
operators.add('let'); | ||
@@ -146,2 +235,5 @@ operators.add('var'); | ||
operators.add('at'); | ||
operators.add('in'); | ||
operators.add('index-of'); | ||
operators.add('slice'); | ||
operators.add('case'); | ||
@@ -152,2 +244,4 @@ operators.add('match'); | ||
operators.add('interpolate'); | ||
operators.add('interpolate-hcl'); | ||
operators.add('interpolate-lab'); | ||
operators.add('ln2'); | ||
@@ -161,2 +255,6 @@ operators.add('pi'); | ||
operators.add('object'); | ||
operators.add('collator'); | ||
operators.add('format'); | ||
operators.add('image'); | ||
operators.add('number-format'); | ||
operators.add('to-string'); | ||
@@ -179,2 +277,4 @@ operators.add('to-number'); | ||
operators.add('line-progress'); | ||
operators.add('sky-radial-progress'); | ||
operators.add('accumulated'); | ||
operators.add('+'); | ||
@@ -202,2 +302,3 @@ operators.add('*'); | ||
operators.add('floor'); | ||
operators.add('distance'); | ||
operators.add('=='); | ||
@@ -212,6 +313,11 @@ operators.add('!='); | ||
operators.add('!'); | ||
operators.add('within'); | ||
operators.add('is-supported-script'); | ||
operators.add('upcase'); | ||
operators.add('downcase'); | ||
operators.add('concat'); | ||
operators.add('resolved-locale'); | ||
var insertSpaceAfter = /("(?:[^\\"]|\\.)*")|[:,]/g; | ||
function stringifyLiteralArray(arr) { | ||
@@ -222,3 +328,3 @@ var items = arr.map(function (item) { | ||
}); | ||
return '[' + items.join(', ') + ']'; | ||
return "[".concat(items.join(', '), "]"); | ||
} | ||
@@ -228,4 +334,3 @@ | ||
if (operator === '!') return false; | ||
return (/^[^a-zA-Z]/.test(operator) | ||
); | ||
return /^[^a-zA-Z]/.test(operator); | ||
} | ||
@@ -246,6 +351,6 @@ | ||
var arg = expression[1]; | ||
if (!Array.isArray(arg)) { | ||
throw new Error('Only array arguments are supported for the literal expression'); | ||
if (Array.isArray(arg)) { | ||
return "".concat(operator, "(").concat(stringifyLiteralArray(arg), ")"); | ||
} | ||
return operator + '(' + stringifyLiteralArray(arg) + ')'; | ||
} | ||
@@ -255,14 +360,27 @@ | ||
if (typeof arg === 'string') { | ||
return '"' + arg + '"'; | ||
return "\"".concat(arg, "\""); | ||
} | ||
if (!Array.isArray(arg)) { | ||
var isArray = Array.isArray(arg); | ||
if (_typeof(arg) === 'object' && !!arg && !isArray) { | ||
// Insert spaces after commas/colons for better inline display. | ||
var pretty = JSON.stringify(arg).replace(insertSpaceAfter, function (match, literal) { | ||
return literal || match + ' '; | ||
}); | ||
return pretty; | ||
} | ||
if (!isArray) { | ||
return arg; | ||
} | ||
var argOperator = arg[0]; | ||
var argFormula = expressionToFormula(arg); | ||
if ( | ||
// Use parentheses to deal with operator precedence. | ||
if ( // Use parentheses to deal with operator precedence. | ||
/^[+-]$/.test(argOperator) && /^[*/%]$/.test(operator) || operator === '^') { | ||
return '(' + argFormula + ')'; | ||
return "(".concat(argFormula, ")"); | ||
} | ||
return argFormula; | ||
@@ -272,7 +390,7 @@ }); | ||
if (operator === '^') { | ||
return '' + args.join(operator); | ||
return "".concat(args.join(operator)); | ||
} | ||
if (isInfixOperator(operator)) { | ||
return '' + args.join(' ' + operator + ' '); | ||
return "".concat(args.join(" ".concat(operator, " "))); | ||
} | ||
@@ -284,3 +402,3 @@ | ||
return operator + '(' + args.join(', ') + ')'; | ||
return "".concat(operator, "(").concat(args.join(', '), ")"); | ||
} | ||
@@ -287,0 +405,0 @@ |
{ | ||
"name": "@mapbox/expression-jamsession", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"description": "Write Mapbox GL expressions in a more familiar, handwritable, spreadsheet-like, programming-like syntax.", | ||
@@ -15,3 +15,3 @@ "main": "dist/index.js", | ||
"format": "prettier --write '{,src/**/,test/**/}*.js'", | ||
"build": "scripts/build-operator-whitelist.js && rm -rf dist && rollup -c", | ||
"build": "scripts/build-operator-list.js && rm -rf dist && rollup -c", | ||
"prepublishOnly": "npm run build" | ||
@@ -38,3 +38,3 @@ }, | ||
[ | ||
"env" | ||
"@babel/preset-env" | ||
] | ||
@@ -46,3 +46,3 @@ ] | ||
[ | ||
"env", | ||
"@babel/preset-env", | ||
{ | ||
@@ -67,17 +67,19 @@ "modules": false | ||
"devDependencies": { | ||
"@mapbox/mapbox-gl-style-spec": "12.0.0-pre.1", | ||
"babel-cli": "^6.26.0", | ||
"babel-preset-env": "^1.6.1", | ||
"@babel/preset-env": "^7.15.6", | ||
"@mapbox/mapbox-gl-style-spec": "^13.21.0", | ||
"@rollup/plugin-babel": "^5.3.0", | ||
"@rollup/plugin-commonjs": "^20.0.0", | ||
"@rollup/plugin-node-resolve": "^13.0.4", | ||
"eslint": "^4.14.0", | ||
"eslint-plugin-node": "^6.0.1", | ||
"husky": "^0.14.3", | ||
"jest": "^22.0.4", | ||
"husky": "^7.0.2", | ||
"jest": "^27.2.1", | ||
"lint-staged": "^6.0.0", | ||
"prettier": "^1.9.2", | ||
"rollup": "^0.53.3", | ||
"rollup-plugin-babel": "^3.0.3" | ||
"rollup": "^2.56.3" | ||
}, | ||
"dependencies": { | ||
"jsep": "^0.3.3" | ||
"@jsep-plugin/object": "^1.0.1", | ||
"jsep": "^1.0.3" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
17529
328
2
12
+ Added@jsep-plugin/object@^1.0.1
+ Added@jsep-plugin/object@1.2.2(transitive)
+ Addedjsep@1.4.0(transitive)
- Removedjsep@0.3.5(transitive)
Updatedjsep@^1.0.3