Skip to content
This repository has been archived by the owner on Nov 3, 2019. It is now read-only.

Commit

Permalink
fix(markdown-scope): throw exception when using a reserved word as a …
Browse files Browse the repository at this point in the history
…scope variable
  • Loading branch information
zkochan committed May 6, 2016
1 parent 37b9a22 commit d7a5cd6
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ Do you want to write a new one? Read the [plugins readme](./plugins/README.md).
- [normalize-path](https://github.com/jonschlinkert/normalize-path): Normalize file path slashes to be unix-like forward slashes. Also condenses repeat slashes to a single slash and removes and trailing slashes.
- [relative](https://github.com/jonschlinkert/relative): Get the relative filepath from path A to path B. Calculates from file-to-directory, file-to-file, directory-to-file, and directory-to-directory.
- [remark-mos](https://github.com/zkochan/remark-mos): Inject parts of markdown via hidden JavaScript snippets
- [reserved-words](https://github.com/zxqfox/reserved-words): ECMAScript reserved words checker
- [resolve](https://github.com/substack/node-resolve): resolve like require.resolve() on behalf of files asynchronously and synchronously
- [run-async](https://github.com/sboudrias/run-async): Utility method to run function either synchronously or asynchronously using the common `this.async()` style.
- [tape](https://github.com/substack/tape): tap-producing test harness for node and browsers
Expand Down
11 changes: 10 additions & 1 deletion lib/create-md-scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module.exports = createMDScope

const runAsync = require('run-async')
const reserved = require('reserved-words')

function createMDScope (plugins, markdown) {
return Promise.all(
Expand All @@ -10,6 +11,14 @@ function createMDScope (plugins, markdown) {
.map(plugin => plugin(markdown))
)
.then(scopes => scopes.reduce(
(scope, pluginScope) => Object.assign(scope, pluginScope), {})
(scope, pluginScope) => {
Object.keys(pluginScope).forEach(scopeVar => {
if (reserved.check(scopeVar, 'next')) { // TODO: make it strict
throw new Error(`Cannot make '${scopeVar}' a scope variable because it is a reserved word`)
}
scope[scopeVar] = pluginScope[scopeVar]
})
return scope
}, {})
)
}
9 changes: 9 additions & 0 deletions lib/create-md-scope.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,13 @@ describe('createMDScope', () => {
})
})
})

it('should throw error if the scope variable is a reserved word', done => {
createMDScope([() => ({ delete: 1 })], {})
.catch(err => {
expect(err).to.be.instanceof(Error)
expect(err.message).to.eq("Cannot make 'delete' a scope variable because it is a reserved word")
done()
})
})
})
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"normalize-path": "~2.0.1",
"relative": "~3.0.2",
"remark-mos": "^1.3.0",
"reserved-words": "^0.1.1",
"resolve": "~1.1.7",
"run-async": "~2.2.0",
"tape": "~4.5.1"
Expand Down

0 comments on commit d7a5cd6

Please sign in to comment.