Skip to content

Commit

Permalink
feat(expressions): handle curried functions, close #21
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jan 27, 2017
1 parent 6c6cdec commit 57e114f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ add(2, -3) //> -1

You can start comments to be updated with `//>` or `//=>`.

## Composed functions

You can even get results from composed functions, for example, the values
below were all computed automatically

```js
var R = require('ramda')
R.compose(
Math.abs, //=> 7
R.add(1), //=> -7
R.multiply(2) //=> -8
)(-4) //=> 7
```

## Console log statements

If the value comment is on the left of `console.log(value)` expression,
Expand Down
26 changes: 20 additions & 6 deletions src/instrument-source.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,26 +66,41 @@ function instrumentSource (source, filename) {
}

const isConsoleLog = node =>
node &&
node.type === 'CallExpression' &&
node.callee.type === 'MemberExpression' &&
node.callee.object.name === 'console' &&
node.callee.property.name === 'log'

const isConsoleLogExpression = node =>
node.expression &&
node.expression.type === 'CallExpression' &&
node.expression.callee.type === 'MemberExpression' &&
node.expression.callee.object.name === 'console' &&
node.expression.callee.property.name === 'log'

function instrument (node) {
// TODO can also handle individual value
// console.log(node.type, node.end, node.source())
// TODO can we handle individual value?
if (node.type === 'ExpressionStatement' ||
node.type === 'Identifier') {
// console.log(node.type, node.end, node.source())
// console.log(node.source(), node)
node.type === 'Identifier' ||
node.type === 'CallExpression') {
if (node.type === 'CallExpression') {
// console.log(node.source(), node)
// ignore top level "console.log(...)",
// we only care about the arguments
if (isConsoleLog(node)) {
return
}
}

if (endsBeforeInstrumentedComment(node) && hasNotSeen(node)) {
// console.log('need to instrument', node.type, node.source())
const comment = findComment(node)
debug('will instrument "%s" for comment "%s"', node.source(), comment.text)
comment.instrumented = true
const reference = 'global.__instrumenter.comments[' + comment.index + '].value'
if (isConsoleLog(node)) {
if (isConsoleLogExpression(node)) {
debug('instrumenting console.log', node.source())
// instrument inside the console.log (the first argument)
const value = node.expression.arguments[0].source()
Expand Down Expand Up @@ -143,7 +158,6 @@ function instrumentSource (source, filename) {
if (!commentStart) {
return
}
// console.log('comment', arguments)
const index = __instrumenter.comments.length
const comment = {
value: undefined,
Expand Down
4 changes: 2 additions & 2 deletions test/compose/ramda-compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ yellGreeting('James', 'Bond'); //=> "THE NAME'S BOND, JAMES BOND"

R.compose(
Math.abs, //=> 7
R.add(1), //=> undefined
R.multiply(2) //> undefined
R.add(1), //=> -7
R.multiply(2) //=> -8
)(-4) //=> 7
2 changes: 1 addition & 1 deletion test/compose/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('compose with curried functions', () => {
la(R.equals(['> ??'], R.map(R.prop('text'), comments)), comments)
})

it.skip('wraps the entire expression', () => {
it('wraps the entire expression', () => {
const wrapped = []
emitter.on('wrap', wrapped.push.bind(wrapped))
instrument(source)
Expand Down
29 changes: 29 additions & 0 deletions test/console-log/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const la = require('lazy-ass')
const is = require('check-more-types')
const instrument = require('../../src/instrument-source')
const read = require('fs').readFileSync
const inFolder = require('path').join.bind(null, __dirname)
const R = require('ramda')

describe('console.log is special', () => {
const source = read(inFolder('index.js'), 'utf8')
let emitter

beforeEach(() => {
emitter = global.instrument
})

it('finds the comment', () => {
const comments = []
emitter.on('comment', comments.push.bind(comments))
instrument(source)
la(R.equals(['> 42'], R.map(R.prop('text'), comments)), comments)
})

it('wraps the first argument only', () => {
const wrapped = []
emitter.on('wrap', wrapped.push.bind(wrapped))
instrument(source)
la(R.equals(['2 + 40'], wrapped), wrapped)
})
})
2 changes: 1 addition & 1 deletion test/property/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const read = require('fs').readFileSync
const inFolder = require('path').join.bind(null, __dirname)
const R = require('ramda')

describe('compose with curried functions', () => {
describe('property functions', () => {
const source = read(inFolder('index.js'), 'utf8')
let emitter

Expand Down

0 comments on commit 57e114f

Please sign in to comment.