node-introspect icon indicating copy to clipboard operation
node-introspect copied to clipboard

Added es6 classes constructor introspection

Open orzarchi opened this issue 9 years ago • 7 comments

This allows introspection of the new native es6 classes. Running the tests now requires a node version that allows es6 classes, so v0.12+ with the --harmony flag.

orzarchi avatar Oct 16 '15 22:10 orzarchi

@orzarchi thanks for contributing.

Are you aware of any way that we can toString only the constructor dropping the rest of the class? Ideally given:

'use strict'

class Polygon {
  constructor(height, width) {
    this._height = height
    this._width = width
  }

  get height() {
    return this._height
  }
}

I would like to have a way to inspect both Polygon.prototype.constructor and Polygon.prototype.height instead of passing the entire class assuming I want to introspect the ctor.

Thoughts?

kilianc avatar Oct 17 '15 17:10 kilianc

also I just gave this a try and it works:

'use strict'

const introspect = require('introspect')

class Polygon {
  constructor(height, width) {
    this._height = height
    this._width = width
  }

  height(foo, bar) {
    return this._height
  }
}

console.log(introspect(Polygon))
console.log(introspect(Polygon.prototype.height))

yields

[ 'height', 'width' ]
[ 'foo', 'bar' ]

on the other side this fails


'use strict'

const introspect = require('introspect')

class Polygon {
  height(foo, bar) {
    return this._height
  }

  constructor(height, width) {
    this._height = height
    this._width = width
  }
}

console.log(introspect(Polygon))
console.log(introspect(Polygon.prototype.height))

yields

[ 'foo', 'bar' ]
[ 'foo', 'bar' ]

can you provide a failing test case as well?

kilianc avatar Oct 17 '15 17:10 kilianc

Ok, so @orzarchi I think I have a grip of the problem here and I think I know how to fix it. First of all just testing for /class/ doesn't work. I could call my function _class or have this.classFoo in there and it would fail. First step we need to test for /^class /and then get the first ctor and then execute the normal introspect code. I would keep the two functions separate for testability.

is class ? getProto && introspect : introspect

I would then create a switch function that "gets" correctly what's to inspect and then calls introspect internally.

module.exports would look something like:

  • check if fn is class or function.
  • if function return introspect(fn)
  • if class fn = getCtor(fn)
  • return introspect(fn)

the argumentsRegExp needs to be adjusted to look for constructor too otherwise declaration order will matter.

Thoughts?

kilianc avatar Oct 17 '15 18:10 kilianc

Hi, glad to see you like my suggestion. Before I implement your idea, I first tried reproducing your Polygon class bug and couldn't. I've uploaded a passing test to prove it, can you please run it? I'm testing on Node v4.1.1 BTW, maybe that's the problem.

orzarchi avatar Oct 17 '15 19:10 orzarchi

@orzarchi I am running v4.2.1 so that shouldn't be the problem. What about this, I will write failing test suite on a new branch and then we can work together to make it pass?

kilianc avatar Oct 17 '15 20:10 kilianc

Sure, I'm keeping an eye out for your tests.

orzarchi avatar Oct 18 '15 21:10 orzarchi

Any news?

orzarchi avatar Nov 28 '15 18:11 orzarchi