简体   繁体   中英

How to find the source code in a particular line number in javascript?

I have line numbers for vanilla js files for which i need to find the source code at that particular line and subsequently the parent function.I am using node.js and was wondering if there was a way to extract the source code and possibly know the parent function of that particular line.

function helloworld(){
   var a=5;
   var b=6; //line number 3
}

For the above function i need the function helloworld from only having the line number?

Editing for more clarity:

When code is pushed into the repo we can get the line numbers where the changes have occured.Using these line numbers i want to the find the parent functions as i said above.I want to find the name of the parent function so i can run only the particular jasmine test file.

Example: Lets say i have a jasmine spec file like this:

describe("helloworld",function(){
   it("test1",function(){
      expect(true).toBe(true)
});
});
describe("someotherfun",function(){
   it("test2",function(){
     expect(true).toBe(true)
  })
})

say there is a script file like this:

function helloworld(){ . //line 1
    console.log('hi') .  //line 2
}
function someotherfun(){
    console.log('hi')
} . //line 6

Lets say some one edits line 2 i want to get the name of helloworld so i can later parse the jasmine spec file and add x to all the specs except the one that is changed.This example will explain what i mean:

describe("helloworld",function(){
   it("test1",function(){
      expect(true).toBe(true)
});
});
xdescribe("someotherfun",function(){ //wont run this test because this function was not modified
   it("test2",function(){
     expect(true).toBe(true)
  })
})    

Now i can add parse my jasmine files and add the x before describe but i what i want to know is the parent function for the particular line that was modified.

I am a bit of a traditionalist, so I write shell scripts to get such things done. Try this command in your terminal: $head -<LineNumber> <FullPathToFile> | tac | grep -m 1 "PatternYouWantToMatch" $head -<LineNumber> <FullPathToFile> | tac | grep -m 1 "PatternYouWantToMatch"

For example, head -50 /home/test.js | tac | grep -m 1 "function" head -50 /home/test.js | tac | grep -m 1 "function"

Explanation:

  • head command will fetch all the lines till the line number you have given
  • tac command will reverse the order of the lines, ie, line number one will become the last line and the vice versa.
  • grep -m 1 will match the first pattern in the inverted file.

Not the best solution, but it will get your things done! Hope it's useful.

I do hope there is a magic way of extracting the functions and it's names from a file but there's none that I know of if you're using JS to extract it, I hope I'm wrong.

However, if you require to do it in JS, raw-loader could help you. Loading the raw file gets the job done but you have to manually parse it yourself. It loads any file, obviously, in raw format and you can traverse your file manually. We also use it to extract our JSDoc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM