Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中的特定行号中查找源代码?_Javascript_Node.js_Jasmine - Fatal编程技术网

如何在javascript中的特定行号中查找源代码?

如何在javascript中的特定行号中查找源代码?,javascript,node.js,jasmine,Javascript,Node.js,Jasmine,我有香草js文件的行号,我需要在特定的行中找到源代码,然后找到父函数。我正在使用node.js,想知道是否有方法提取源代码,并可能知道该特定行的父函数 function helloworld(){ var a=5; var b=6; //line number 3 } 对于上面的函数,我需要函数helloworld,因为它只包含行号 为更清晰起见,请编辑: 当代码被推送到repo中时,我们可以得到发生更改的行号。使用这些行号,我想找到上面提到的父函数。我想找到父函数的名称,这样我就

我有香草js文件的行号,我需要在特定的行中找到源代码,然后找到父函数。我正在使用node.js,想知道是否有方法提取源代码,并可能知道该特定行的父函数

function helloworld(){
   var a=5;
   var b=6; //line number 3
}
对于上面的函数,我需要函数helloworld,因为它只包含行号

为更清晰起见,请编辑:

当代码被推送到repo中时,我们可以得到发生更改的行号。使用这些行号,我想找到上面提到的父函数。我想找到父函数的名称,这样我就可以只运行特定的jasmine测试文件

例如: 假设我有一个jasmine规范文件,如下所示:

describe("helloworld",function(){
   it("test1",function(){
      expect(true).toBe(true)
});
});
describe("someotherfun",function(){
   it("test2",function(){
     expect(true).toBe(true)
  })
})
function helloworld(){ . //line 1
    console.log('hi') .  //line 2
}
function someotherfun(){
    console.log('hi')
} . //line 6
假设有这样一个脚本文件:

describe("helloworld",function(){
   it("test1",function(){
      expect(true).toBe(true)
});
});
describe("someotherfun",function(){
   it("test2",function(){
     expect(true).toBe(true)
  })
})
function helloworld(){ . //line 1
    console.log('hi') .  //line 2
}
function someotherfun(){
    console.log('hi')
} . //line 6
假设有人编辑了第2行,我想得到helloworld的名称,这样我可以稍后解析jasmine规范文件,并将x添加到所有规范中,但更改的规范除外。此示例将解释我的意思:

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)
  })
})    

现在我可以添加解析我的jasmine文件并在“描述”之前添加x,但我想知道的是被修改的特定行的父函数。

我确实希望有一种神奇的方法可以从文件中提取函数及其名称,但如果您使用JS提取它,我不知道,我希望我错了


但是,如果您需要在JS中执行此操作,可以帮助您。加载原始文件可以完成任务,但您必须自己手动解析它。显然,它以原始格式加载任何文件,您可以手动遍历文件。我们还使用它来提取JSDoc。

我有点传统,所以我编写shell脚本来完成这些事情。 在终端中尝试以下命令:$head-| tac | grep-m1 PatternYouWantToMatch

例如,head-50/home/test.js | tac | grep-m1函数

说明:

head命令将获取所有行,直到您给出行号为止 tac命令将颠倒行的顺序,即第一行将成为最后一行,反之亦然。 grep-m1将匹配反转文件中的第一个模式。
不是最好的解决方案,但它会让你的事情完成!希望有用。

我不知道你想做什么,但这可能有助于我获取函数名,以便我可以使用它们只运行jasmine规范文件中的特定测试。我不想在源代码中的一行中运行整个测试文件进行更改。我可以解析jasmine文件并添加xdescribe,跳过我不想运行的规范。你在问什么?如何解析脚本?找到代码的条件是什么?这太模糊了。为澄清而编辑。如果你能解释一下反对票,我将不胜感激。@AnanthaPadmanaban我知道,你没有足够的代表来做这件事。尽管如此,我希望这个答案对你有所帮助