Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Name the functions found at a particular line number in some JavaScript source

Notifications You must be signed in to change notification settings

testdouble/function-names-at-line

Repository files navigation

function-names-at-line

I was working on this run test by line number feature for teenytest, and needed a way to tell what functions might be described by a given line number.

For example, say you have this file:

                        // 1
var foo = function () { // 2
  var baz = 'lol'       // 3
  function bar () {}    // 4
}                       // 5
                        // 6

And read the file into a string named someSource, then you can:

var functionNamesAtLine = require('function-names-at-line')

functionNamesAtLine(someSource, 1) // []
functionNamesAtLine(someSource, 2) // ['foo']
functionNamesAtLine(someSource, 3) // ['foo']
functionNamesAtLine(someSource, 4) // ['bar', 'foo']
functionNamesAtLine(someSource, 5) // ['foo']
functionNamesAtLine(someSource, 6) // []

This is a pretty naive implementation, focused just on getting basic (not nested) names of functions.

About

Name the functions found at a particular line number in some JavaScript source

Resources

Stars

Watchers

Forks

Packages

No packages published