Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mode to throw on invalid property access #24

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ be passed to eliminate the "power of" operator.
$ yarn install
$ yarn test
```

### Precommit hook

Mozjexl provides a config for
Expand All @@ -341,6 +340,14 @@ it with Pip, and then run `therapist install` in this repo to set it
up. It will automatically format your Javascript with Prettier, and
run ESLint checks before committing your code.

## Building a JSM for use in mozilla-central ###
```shell
$ yarn build
```

The result will be in `vendor/mozjexl.js`. Though it is not named ".jsm", it will
function as one.

## License
Mozjexl is licensed under the MIT license. Please see `LICENSE.txt` for full
details.
Expand Down
3 changes: 2 additions & 1 deletion lib/Jexl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ var Evaluator = require("./evaluator/Evaluator"),
* xpath-like drilldown into native Javascript objects.
* @constructor
*/
function Jexl() {
function Jexl(throwOnMissingProp = false) {
this._customGrammar = null;
this._lexer = null;
this._transforms = {};
this._throwOnMissingProp = throwOnMissingProp;
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/evaluator/handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ exports.FilterExpression = function(ast) {
exports.Identifier = function(ast) {
if (ast.from) {
return this.eval(ast.from).then(function(context) {
if (
this._throwOnMissingProp &&
!Object.hasOwnProperty(context, ast.value)
) {
throw new Error(
`property does not have an identifier named ${ast.value}`
);
}
if (Array.isArray(context)) context = context[0];
if (context === undefined) return undefined;
return context[ast.value];
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"test": "test"
},
"scripts": {
"watch": "webpack --config ./webpack.config.js --watch",
"build": "webpack --config ./webpack.config.js",
"test": "mocha -R spec --recursive test",
"lint": "eslint lib test .eslintrc.js --ignore-pattern '!.eslintrc.js'"
},
Expand Down Expand Up @@ -35,12 +37,15 @@
},
"homepage": "https://github.com/mozilla/mozjexl",
"devDependencies": {
"babili-webpack-plugin": "0.1.2",
"chai": "^2.0.0",
"chai-as-promised": "^4.2.0",
"mocha": "^2.1.0",
"eslint": "^4.18.1",
"eslint-config-prettier": "^2.9.0",
"eslint-plugin-prettier": "^2.6.0",
"prettier": "^1.10.2"
"license-webpack-plugin": "0.5.1",
"prettier": "^1.10.2",
"webpack": "3.1.0"
}
}
Empty file added vendor/LICENSE_THIRDPARTY
Empty file.
Loading