{"action":"created","issue":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/12053","repository_url":"https://api.github.com/repos/Microsoft/TypeScript","labels_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/12053/labels{/name}","comments_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/12053/comments","events_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/12053/events","html_url":"https://github.com/Microsoft/TypeScript/issues/12053","id":187425827,"number":12053,"title":"`getSourceFileOfNode` function implementation","user":{"login":"jmlopez-rod","id":1810397,"avatar_url":"https://avatars.githubusercontent.com/u/1810397?v=3","gravatar_id":"","url":"https://api.github.com/users/jmlopez-rod","html_url":"https://github.com/jmlopez-rod","followers_url":"https://api.github.com/users/jmlopez-rod/followers","following_url":"https://api.github.com/users/jmlopez-rod/following{/other_user}","gists_url":"https://api.github.com/users/jmlopez-rod/gists{/gist_id}","starred_url":"https://api.github.com/users/jmlopez-rod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmlopez-rod/subscriptions","organizations_url":"https://api.github.com/users/jmlopez-rod/orgs","repos_url":"https://api.github.com/users/jmlopez-rod/repos","events_url":"https://api.github.com/users/jmlopez-rod/events{/privacy}","received_events_url":"https://api.github.com/users/jmlopez-rod/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-11-04T20:02:56Z","updated_at":"2016-11-04T20:14:24Z","closed_at":null,"body":"This is more of a question about the internals of typescript. For some reason I ended up with a `Node` object which had no parent. I'm pretty sure this is due to the way I configured the compiler using the API but this led me to the following function in the typescript source:\r\n\r\n```\r\n    function getSourceFileOfNode(node) {\r\n        while (node && node.kind !== 256 /* SourceFile */) {\r\n            node = node.parent;\r\n        }\r\n        return node;\r\n    }\r\n```\r\n\r\nThis function is eventually called when I use \r\n\r\n```\r\nNodeObject.prototype.getStart = function (sourceFile, includeJsDocComment) {\r\n    return ts.getTokenPosOfNode(this, sourceFile, includeJsDocComment);\r\n};\r\n```\r\n\r\n`getStart` has all optional parameters and I never pass in the sourceFile so `getSourceFileOfNode` is always triggered.\r\n\r\n**Question**:\r\n\r\nIs there a reason why a node object doesn't have a reference to the source file object to which it belongs? We already store its `kind`, `parent` and other information, why not a reference to the sourceFile?\r\n\r\nI know machines are fast, but going up the tree up to the source file every time we need to see the start of a node seems inefficient. Things could be faster especially for cases in which we have a tree with many levels. As an example consider this project: https://github.com/buzinas/tslint-eslint-rules\r\n\r\nI've been writing a few rules here and I decided to check out just how many times the function gets called during the build process:\r\n\r\n```\r\n    var count = 0;\r\n    var calls = 0;\r\n    function getSourceFileOfNode(node) {\r\n        calls ++;\r\n        while (node && node.kind !== 256 /* SourceFile */) {\r\n            count++;\r\n            node = node.parent;\r\n        }\r\n        console.log('count: ', [calls, count]);\r\n        return node;\r\n    }\r\n```\r\n\r\nI modified the function to print out the number of calls to the function and the number of times we go up in the tree, the result:\r\n\r\n```\r\ncount:  [ 19505, 78762 ]\r\n```\r\n\r\nSo instead of accessing the source file 19505 times we have to execute 59,257 more checks and assignments. Would it decrease the efficiency of typescript if we were to add a reference to source file?"},"comment":{"url":"https://api.github.com/repos/Microsoft/TypeScript/issues/comments/258536037","html_url":"https://github.com/Microsoft/TypeScript/issues/12053#issuecomment-258536037","issue_url":"https://api.github.com/repos/Microsoft/TypeScript/issues/12053","id":258536037,"user":{"login":"jmlopez-rod","id":1810397,"avatar_url":"https://avatars.githubusercontent.com/u/1810397?v=3","gravatar_id":"","url":"https://api.github.com/users/jmlopez-rod","html_url":"https://github.com/jmlopez-rod","followers_url":"https://api.github.com/users/jmlopez-rod/followers","following_url":"https://api.github.com/users/jmlopez-rod/following{/other_user}","gists_url":"https://api.github.com/users/jmlopez-rod/gists{/gist_id}","starred_url":"https://api.github.com/users/jmlopez-rod/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jmlopez-rod/subscriptions","organizations_url":"https://api.github.com/users/jmlopez-rod/orgs","repos_url":"https://api.github.com/users/jmlopez-rod/repos","events_url":"https://api.github.com/users/jmlopez-rod/events{/privacy}","received_events_url":"https://api.github.com/users/jmlopez-rod/received_events","type":"User","site_admin":false},"created_at":"2016-11-04T20:14:24Z","updated_at":"2016-11-04T20:14:24Z","body":"Anyway, there might be a bug but I'm not reporting it since I don't know yet how to duplicate it with a minimal set of information. The function just caught my eye and I was curious to see if you guys think adding a reference could give us a little performance."}}
{"actor":{"display_login":"jmlopez-rod"}}