{"id":295583,"date":"2019-12-17T16:20:24","date_gmt":"2019-12-17T16:20:24","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=295583"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=295583","title":{"rendered":"\u0412\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u0432 jscodeshift \u0438 TypeScript"},"content":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/480304\/\">\n<h1 id=\"vyvod-tipov-v-jscodeshift-i-typescript\">\u0412\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u0432 jscodeshift \u0438 TypeScript<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 6.0 <a href=\"https:\/\/github.com\/facebook\/jscodeshift\" rel=\"nofollow\">jscodeshift<\/a> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 TypeScript (\u0434\u0430\u043b\u0435\u0435 TS). \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f codemode-\u043e\u0432 (\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439), \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u0437\u043d\u0430\u0442\u044c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u044f\u0432\u043d\u043e\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, jscodeshift \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<p>\u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440. \u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u044f\u0432\u043d\u044b\u0439 \u0442\u0438\u043f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432. \u0422.\u0435. \u0438\u043c\u0435\u044f \u043d\u0430 \u0432\u0445\u043e\u0434\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">function foo(x: number) {     return x; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">function foo(x: number): number {     return x; }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, \u0432 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0442\u0430\u043a\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u043e\u0447\u0435\u043d\u044c \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u043e. \u0412\u043e\u0442 \u043b\u0438\u0448\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">function toString(x: number) {     return '' + x; }  function toInt(str: string) {     return parseInt(str); }  function toIntArray(strings: string[]) {     return strings.map(Number.parseInt); }  class Foo1 {     constructor(public x = 0) { }      getX() {         return this.x;     } }  class Foo2 {     x: number;      constructor(x = 0) {         this.x = x;     }      getX() {         return this.x;     } }  function foo1(foo: Foo1) {     return foo.getX(); }  function foo2(foo: Foo2) {     return foo.getX(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u0437\u0430\u0434\u0430\u0447\u0430 \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u0443\u0436\u0435 \u0440\u0435\u0448\u0435\u043d\u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 TS. <a href=\"https:\/\/github.com\/microsoft\/TypeScript\/wiki\/Using-the-Compiler-API\" rel=\"nofollow\">API \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430<\/a> \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0438 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c TS, \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0432 \u043f\u0430\u0440\u0441\u0435\u0440 jscodeshift, \u043d\u0435\u043b\u044c\u0437\u044f. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e jscodeshift \u043e\u0436\u0438\u0434\u0430\u0435\u0442 \u043e\u0442 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u043f\u0430\u0440\u0441\u0435\u0440\u043e\u0432 \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u043e\u0435 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0447\u0435\u0441\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e (AST) \u0432 \u0444\u043e\u0440\u043c\u0430\u0442\u0435 <a href=\"https:\/\/github.com\/estree\/estree\" rel=\"nofollow\">ESTree<\/a>. \u0410 AST \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 TS \u0442\u0430\u043a\u043e\u0432\u044b\u043c \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u043e\u043c TS \u0438 \u0431\u0435\u0437 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f jscodeshift, \u043d\u0430\u043f\u0438\u0441\u0430\u0432 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u00ab\u0441 \u043d\u0443\u043b\u044f\u00bb. \u041b\u0438\u0431\u043e \u0436\u0435 \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0434\u043d\u0438\u043c \u0438\u0437 \u0441\u0440\u0435\u0434\u0441\u0442\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0442 \u0432 \u043a\u043e\u043c\u044c\u044e\u043d\u0438\u0442\u0438 TS, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, <a href=\"https:\/\/github.com\/dsherret\/ts-morph\" rel=\"nofollow\">ts-morph<\/a>. \u041d\u043e \u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 jscodeshift \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u043c \u0438 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e, \u043a\u0430\u043a \u043e\u0431\u043e\u0439\u0442\u0438 \u044d\u0442\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<p>\u0418\u0434\u0435\u044f \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 AST \u043f\u0430\u0440\u0441\u0435\u0440\u0430 jscodeshift (\u0434\u0430\u043b\u0435\u0435 ESTree) \u0432 AST \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 TS (\u0434\u0430\u043b\u0435\u0435 TSTree), \u0438 \u0437\u0430\u0442\u0435\u043c \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0430\u043c\u0438 \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 TS. \u0414\u0430\u043b\u0435\u0435 \u0431\u0443\u0434\u0443\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0434\u0432\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u044d\u0442\u043e\u0439 \u0438\u0434\u0435\u0438.<\/p>\n<p>  <\/p>\n<h2 id=\"otobrazhenie-s-ispolzovaniem-nomerov-strok-i-stolbcov\">\u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043d\u043e\u043c\u0435\u0440\u043e\u0432 \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h2>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u043f\u043e\u0441\u043e\u0431 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043d\u043e\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432 (\u043f\u043e\u0437\u0438\u0446\u0438\u0438) \u0443\u0437\u043b\u043e\u0432, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 TSTree \u0432 ESTree. \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0443\u0437\u043b\u043e\u0432 \u043c\u043e\u0433\u0443\u0442 \u043d\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c, \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u043d\u0443\u0436\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<p>  <\/p>\n<p>\u0418\u0442\u0430\u043a, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u044f\u0432\u043d\u044b\u0445 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0439. \u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">function toString(x: number): number {     return '' + x; }  function toInt(str: string): number {     return parseInt(str); }  function toIntArray(strings: string[]): number[] {     return strings.map(Number.parseInt); }  class Foo1 {     constructor(public x = 0) { }      getX(): number {         return this.x;     } }  class Foo2 {     x: number;      constructor(x = 0) {         this.x = x;     }      getX(): number {         return this.x;     } }  function foo1(foo: Foo1): number {     return foo.getX(); }  function foo2(foo: Foo2): number {     return foo.getX(); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0421\u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c TSTree \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c <code>typeChecker<\/code> \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0430 TS:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const compilerOptions = {     target: ts.ScriptTarget.Latest };  const program = ts.createProgram([path], compilerOptions); const sourceFile = program.getSourceFile(path); const typeChecker = program.getTypeChecker();<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u043c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 ESTree \u0432 TSTree \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u0432\u0443\u0445\u0443\u0440\u043e\u0432\u043d\u0435\u0432\u044b\u0439 <code>Map<\/code> (\u043f\u0435\u0440\u0432\u044b\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u2013 \u0434\u043b\u044f \u0441\u0442\u0440\u043e\u043a, \u0432\u0442\u043e\u0440\u043e\u0439 \u0443\u0440\u043e\u0432\u0435\u043d\u044c \u2013 \u0434\u043b\u044f \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432, \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u2013 \u0443\u0437\u0435\u043b TSTree):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const locToTSNodeMap = new Map();  const esTreeNodeToTSNode = ({ loc: { start: { line, column } } }) =&gt; locToTSNodeMap.has(line) ? locToTSNodeMap.get(line).get(column) : undefined;  (function buildLocToTSNodeMap(node) {     const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));     const nextLine = line + 1;     if (!locToTSNodeMap.has(nextLine))         locToTSNodeMap.set(nextLine, new Map());     locToTSNodeMap.get(nextLine).set(character, node);     ts.forEachChild(node, buildLocToTSNodeMap); }(sourceFile));<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0441\u0442\u0440\u043e\u043a\u0438, \u0442.\u043a. \u0432 TSTree \u043d\u043e\u043c\u0435\u0440\u0430 \u0441\u0442\u0440\u043e\u043a \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0442\u0441\u044f \u0441 \u043d\u0443\u043b\u044f, \u0430 \u0432 ESTree \u2013 \u0441 \u0435\u0434\u0438\u043d\u0438\u0446\u044b.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0431\u043e\u0439\u0442\u0438 \u0432\u0441\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432, \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0442\u0438\u043f \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d \u0440\u0430\u0432\u0435\u043d <code>null<\/code>, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u0442\u0438\u043f\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const ast = j(source); ast     .find(j.FunctionDeclaration)     .forEach(({ value }) =&gt; {         if (value.returnType === null)             value.returnType = getReturnType(esTreeNodeToTSNode(value));     }); ast     .find(j.ClassMethod, { kind: 'method' })     .forEach(({ value }) =&gt; {         if (value.returnType === null)             value.returnType = getReturnType(esTreeNodeToTSNode(value).parent);     }); return ast.toSource();<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0443\u0437\u043b\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u043a\u043b\u0430\u0441\u0441\u0430, \u0442.\u043a. \u043f\u043e \u0441\u0442\u0430\u0440\u0442\u043e\u0432\u043e\u0439 \u043f\u043e\u0437\u0438\u0446\u0438\u0438 \u0443\u0437\u043b\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 ESTree \u0432 TSTree \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0443\u0437\u0435\u043b \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440\u0430 \u043c\u0435\u0442\u043e\u0434\u0430 (\u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c <code>parent<\/code>-\u0430).<\/p>\n<p>  <\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043a\u043e\u0434 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u0442\u0438\u043f\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">function getReturnTypeFromString(typeString) {     let ret;     j(`function foo(): ${typeString} { }`)         .find(j.FunctionDeclaration)         .some(({ value: { returnType } }) =&gt; ret = returnType);     return ret; }  function getReturnType(node) {     return getReturnTypeFromString(         typeChecker.typeToString(             typeChecker.getReturnTypeOfSignature(                 typeChecker.getSignatureFromDeclaration(node)             )         )     ); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import * as ts from 'typescript';  export default function transform({ source, path }, { j }) {     const compilerOptions = {         target: ts.ScriptTarget.Latest     };      const program = ts.createProgram([path], compilerOptions);     const sourceFile = program.getSourceFile(path);     const typeChecker = program.getTypeChecker();      const locToTSNodeMap = new Map();      const esTreeNodeToTSNode = ({ loc: { start: { line, column } } }) =&gt; locToTSNodeMap.has(line) ? locToTSNodeMap.get(line).get(column) : undefined;      (function buildLocToTSNodeMap(node) {         const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile));         const nextLine = line + 1;         if (!locToTSNodeMap.has(nextLine))             locToTSNodeMap.set(nextLine, new Map());         locToTSNodeMap.get(nextLine).set(character, node);         ts.forEachChild(node, buildLocToTSNodeMap);     }(sourceFile));      function getReturnTypeFromString(typeString) {         let ret;         j(`function foo(): ${typeString} { }`)             .find(j.FunctionDeclaration)             .some(({ value: { returnType } }) =&gt; ret = returnType);         return ret;     }      function getReturnType(node) {         return getReturnTypeFromString(             typeChecker.typeToString(                 typeChecker.getReturnTypeOfSignature(                     typeChecker.getSignatureFromDeclaration(node)                 )             )         );     }      const ast = j(source);     ast         .find(j.FunctionDeclaration)         .forEach(({ value }) =&gt; {             if (value.returnType === null)                 value.returnType = getReturnType(esTreeNodeToTSNode(value));         });     ast         .find(j.ClassMethod, { kind: 'method' })         .forEach(({ value }) =&gt; {             if (value.returnType === null)                 value.returnType = getReturnType(esTreeNodeToTSNode(value).parent);         });     return ast.toSource(); }  export const parser = 'ts';<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"ispolzovanie-parsera-typescript-eslint\">\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 typescript-eslint<\/h2>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u0431\u044b\u043b\u043e \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043e \u0432\u044b\u0448\u0435, \u0445\u043e\u0442\u044c \u0438 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0437\u0438\u0446\u0438\u0439 \u0443\u0437\u043b\u043e\u0432 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043e\u043d\u043e \u043d\u0435 \u0434\u0430\u0435\u0442 \u0442\u043e\u0447\u043d\u043e\u0433\u043e \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430 \u0438 \u0438\u043d\u043e\u0433\u0434\u0430 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u00ab\u0440\u0443\u0447\u043d\u043e\u0439 \u0434\u043e\u0432\u043e\u0434\u043a\u0438\u00bb. \u0411\u043e\u043b\u0435\u0435 \u043e\u0431\u0449\u0438\u043c \u0440\u0435\u0448\u0435\u043d\u0438\u0435\u043c \u0431\u044b\u043b\u043e \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044f\u0432\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 ESTree \u0432 TSTree. \u0418\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0430\u0440\u0441\u0435\u0440 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <a href=\"https:\/\/github.com\/typescript-eslint\/typescript-eslint\" rel=\"nofollow\">typescript-eslint<\/a>. \u0412\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0438\u043c.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0439 <a href=\"https:\/\/github.com\/facebook\/jscodeshift#parser\" rel=\"nofollow\">\u043f\u0430\u0440\u0441\u0435\u0440 jscodeshift<\/a> \u043d\u0430 <a href=\"https:\/\/github.com\/typescript-eslint\/typescript-eslint\/tree\/master\/packages\/typescript-estree\" rel=\"nofollow\">\u043f\u0430\u0440\u0441\u0435\u0440 typescript-eslint<\/a>. \u0412 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u0434 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">export const parser = {     parse(source) {         return typescriptEstree.parse(source);     } };<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0434\u043d\u0430\u043a\u043e, \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0443\u0441\u043b\u043e\u0436\u043d\u0438\u0442\u044c \u043a\u043e\u0434, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 ESTree \u0432 TSTree \u0438 <code>typeChecker<\/code>. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 typescript-eslint \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>parseAndGenerateServices<\/code>. \u0427\u0442\u043e\u0431\u044b \u0432\u0441\u0435 \u0437\u0430\u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432 \u043d\u0435\u0435 \u043f\u0443\u0442\u044c \u043a <code>.ts<\/code> \u0444\u0430\u0439\u043b\u0443 \u0438 \u043f\u0443\u0442\u044c \u043a \u0444\u0430\u0439\u043b\u0443 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 <code>tsconfig.json<\/code>. \u0422\u0430\u043a \u043a\u0430\u043a \u043f\u0440\u044f\u043c\u043e\u0433\u043e \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e\u0433\u043e \u043d\u0435\u0442, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0439 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 (\u043e\u0445!):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const parserState = {};  function parseWithServices(j, source, path, projectPath) {     parserState.options = { filePath: path, project: projectPath };     return {         ast: j(source),         services: parserState.services     }; }  export const parser = {     parse(source) {         if (parserState.options !== undefined) {             const options = parserState.options;             delete parserState.options;             const { ast, services } = typescriptEstree.parseAndGenerateServices(source, options);             parserState.services = services;             return ast;         }         return typescriptEstree.parse(source);     } };<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 typescript-eslint, \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>parseWithServices<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b (\u0432 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u043c\u044b \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>j<\/code>):<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">const { ast, services: { program, esTreeNodeToTSNodeMap } } = parseWithServices(j, source, path, tsConfigPath);  const typeChecker = program.getTypeChecker(); const esTreeNodeToTSNode = ({ original }) =&gt; esTreeNodeToTSNodeMap.get(original);<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u043e\u0434 \u043e\u0431\u0445\u043e\u0434\u0430 \u0438 \u043c\u043e\u0434\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0438 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u043a\u043b\u0430\u0441\u0441\u043e\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">ast     .find(j.FunctionDeclaration)     .forEach(({ value }) =&gt; {         if (value.returnType === null)             value.returnType = getReturnType(esTreeNodeToTSNode(value));     }); ast     .find(j.MethodDefinition, { kind: 'method' })     .forEach(({ value }) =&gt; {         if (value.value.returnType === null)             value.value.returnType = getReturnType(esTreeNodeToTSNode(value));     }); return ast.toSource();<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u0434\u043e \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0441\u0435\u043b\u0435\u043a\u0442\u043e\u0440 <code>ClassMethod<\/code> \u043d\u0430 <code>MethodDefinition<\/code>, \u0447\u0442\u043e\u0431\u044b \u043e\u0431\u043e\u0439\u0442\u0438 \u043c\u0435\u0442\u043e\u0434\u044b \u043a\u043b\u0430\u0441\u0441\u043e\u0432 (\u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0441\u044f \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u043c\u0443 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044e \u043c\u0435\u0442\u043e\u0434\u0430). \u042d\u0442\u043e \u0441\u043f\u0435\u0446\u0438\u0444\u0438\u043a\u0430 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 typescript-eslint. \u041a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>getReturnType<\/code> \u0438\u0434\u0435\u043d\u0442\u0438\u0447\u0435\u043d \u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0440\u0430\u043d\u0435\u0435.<\/p>\n<p>  <\/p>\n<p>\u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">import * as typescriptEstree from '@typescript-eslint\/typescript-estree';  export default function transform({ source, path }, { j }, { tsConfigPath }) {     const { ast, services: { program, esTreeNodeToTSNodeMap } } = parseWithServices(j, source, path, tsConfigPath);      const typeChecker = program.getTypeChecker();     const esTreeNodeToTSNode = ({ original }) =&gt; esTreeNodeToTSNodeMap.get(original);      function getReturnTypeFromString(typeString) {         let ret;         j(`function foo(): ${typeString} { }`)             .find(j.FunctionDeclaration)             .some(({ value: { returnType } }) =&gt; ret = returnType);         return ret;     }      function getReturnType(node) {         return getReturnTypeFromString(             typeChecker.typeToString(                 typeChecker.getReturnTypeOfSignature(                     typeChecker.getSignatureFromDeclaration(node)                 )             )         );     }      ast         .find(j.FunctionDeclaration)         .forEach(({ value }) =&gt; {             if (value.returnType === null)                 value.returnType = getReturnType(esTreeNodeToTSNode(value));         });     ast         .find(j.MethodDefinition, { kind: 'method' })         .forEach(({ value }) =&gt; {             if (value.value.returnType === null)                 value.value.returnType = getReturnType(esTreeNodeToTSNode(value));         });     return ast.toSource(); }  const parserState = {};  function parseWithServices(j, source, path, projectPath) {     parserState.options = { filePath: path, project: projectPath };     return {         ast: j(source),         services: parserState.services     }; }  export const parser = {     parse(source) {         if (parserState.options !== undefined) {             const options = parserState.options;             delete parserState.options;             const { ast, services } = typescriptEstree.parseAndGenerateServices(source, options);             parserState.services = services;             return ast;         }         return typescriptEstree.parse(source);     } };<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"plyusy-i-minusy-podhodov\">\u041f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b \u043f\u043e\u0434\u0445\u043e\u0434\u043e\u0432<\/h2>\n<p>  <\/p>\n<h3 id=\"podhod-s-nomerami-strok-i-stolbcov\">\u041f\u043e\u0434\u0445\u043e\u0434 \u0441 \u043d\u043e\u043c\u0435\u0440\u0430\u043c\u0438 \u0441\u0442\u0440\u043e\u043a \u0438 \u0441\u0442\u043e\u043b\u0431\u0446\u043e\u0432<\/h3>\n<p>  <\/p>\n<p>\u041f\u043b\u044e\u0441\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u041d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0441\u0435\u0440\u0430 jscodeshift.<\/li>\n<li>\u0413\u0438\u0431\u043a\u043e\u0441\u0442\u044c \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432 (\u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u0430\u043a \u0444\u0430\u0439\u043b\u044b, \u0442\u0430\u043a \u0438 \u0441\u0442\u0440\u043e\u043a\u0438\/\u043e\u0431\u044a\u0435\u043a\u0442\u044b \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0441\u043c. \u043d\u0438\u0436\u0435).<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041c\u0438\u043d\u0443\u0441\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u041e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u043f\u043e \u043f\u043e\u0437\u0438\u0446\u0438\u044f\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435\u0442\u043e\u0447\u043d\u044b\u043c \u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043b\u0443\u0447\u0430\u044f\u0445 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/li>\n<\/ul>\n<p>  <\/p>\n<h3 id=\"podhod-s-parserom-typescript-eslint\">\u041f\u043e\u0434\u0445\u043e\u0434 \u0441 \u043f\u0430\u0440\u0441\u0435\u0440\u043e\u043c typescript-eslint<\/h3>\n<p>  <\/p>\n<p>\u041f\u043b\u044e\u0441\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0422\u043e\u0447\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e AST \u0432 \u0434\u0440\u0443\u0433\u043e\u0435.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041c\u0438\u043d\u0443\u0441\u044b:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 AST \u043f\u0430\u0440\u0441\u0435\u0440\u0430 typescript-eslint \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u0430\u0440\u0441\u0435\u0440\u0430 jscodeshift.<\/li>\n<li>\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 TS \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0445 \u0442\u0435\u043a\u0441\u0442\u043e\u0432.<\/li>\n<\/ul>\n<p>  <\/p>\n<h2 id=\"zaklyuchenie\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043b\u0435\u0433\u043a\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u043f\u0440\u043e\u0435\u043a\u0442\u044b, \u0442.\u043a. \u043e\u043d \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u0440\u0441\u0435\u0440\u0430, \u043d\u043e \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0443\u0437\u043b\u043e\u0432 AST, \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e, \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u043a\u0438.<\/p>\n<p>  <\/p>\n<p>\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u043e \u0432\u0442\u043e\u0440\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043b\u0443\u0447\u0448\u0435 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u0440\u0430\u043d\u0435\u0435, \u0438\u043d\u0430\u0447\u0435, \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e, \u043f\u0440\u0438\u0434\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u0442\u0438\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043d\u0430 \u043e\u0442\u043b\u0430\u0434\u043a\u0443 \u043a\u043e\u0434\u0430 \u0438\u0437-\u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0438\u0432\u0448\u0435\u0439\u0441\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b AST. \u0421 \u0434\u0440\u0443\u0433\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u044b, \u0443 \u0432\u0430\u0441 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u043e\u0435 \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0434\u043d\u0438\u0445 \u0443\u0437\u043b\u043e\u0432 \u043d\u0430 \u0434\u0440\u0443\u0433\u0438\u0435 (\u0438 \u043e\u0431\u0440\u0430\u0442\u043d\u043e).<\/p>\n<p>  <\/p>\n<h2 id=\"ps\">P.S.<\/h2>\n<p>  <\/p>\n<p>\u0412\u044b\u0448\u0435 \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e\u0441\u044c, \u0447\u0442\u043e \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043f\u0430\u0440\u0441\u0435\u0440\u0430 TS, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0442\u0435\u043a\u0441\u0442\u044b \u043a\u0430\u043a \u0432 \u0432\u0438\u0434\u0435 \u0444\u0430\u0439\u043b\u043e\u0432, \u0442\u0430\u043a \u0438 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0432 \u043f\u0430\u043c\u044f\u0442\u0438. \u041f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u0438 \u0432 \u0432\u0438\u0434\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0447\u0430 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0435\u043a\u0441\u0442\u0430 \u0432 \u0432\u0438\u0434\u0435 \u0444\u0430\u0439\u043b\u0430 \u0431\u044b\u043b\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u044b \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435. \u0414\u0430\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a\u043e\u0434 \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0438\u0437 \u0444\u0430\u0439\u043b\u0430:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">class TsDiagnosticError extends Error {     constructor(err) {         super(Array.isArray(err) ? err.map(e =&gt; e.messageText).join('\\n') : err.messageText);         this.diagnostic = err;     } }  function tsGetCompilerOptionsFromConfigFile(tsConfigPath, basePath = '.') {     const { config, error } = ts.readConfigFile(tsConfigPath, ts.sys.readFile);     if (error)         throw new TsDiagnosticError(error);     const { options, errors } = ts.parseJsonConfigFileContent(config, tsGetCompilerOptionsFromConfigFile.host, basePath);     if (errors.length !== 0)         throw new TsDiagnosticError(errors);     return options; }  tsGetCompilerOptionsFromConfigFile.host = {     fileExists: ts.sys.fileExists,     readFile: ts.sys.readFile,     readDirectory: ts.sys.readDirectory,     useCaseSensitiveFileNames: true };<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0441\u043e\u0437\u0434\u0430\u0442\u044c TS-\u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a\u0438:<\/p>\n<p>  <\/p>\n<pre><code class=\"javascript\">function tsCreateStringSourceCompilerHost(mockPath, source, compilerOptions, setParentNodes) {     const host = ts.createCompilerHost(compilerOptions, setParentNodes);      const getSourceFileOriginal = host.getSourceFile.bind(host);     const readFileOriginal = host.readFile.bind(host);     const fileExistsOriginal = host.fileExists.bind(host);      host.getSourceFile = (fileName, languageVersion, onError, shouldCreateNewSourceFile) =&gt; {         return fileName === mockPath ?             ts.createSourceFile(fileName, source, languageVersion) :             getSourceFileOriginal(fileName, languageVersion, onError, shouldCreateNewSourceFile);     };     host.readFile = (fileName) =&gt; {         return fileName === mockPath ?             source :             readFileOriginal(fileName);     };     host.fileExists = (fileName) =&gt; {         return fileName === mockPath ?             true :             fileExistsOriginal(fileName);     };      return host; }  function tsCreateStringSourceProgram(source, compilerOptions, mockPath = '_source.ts') {     return ts.createProgram([mockPath], compilerOptions, tsCreateStringSourceCompilerHost(mockPath, source, compilerOptions)); }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"ssylki\">\u0421\u0441\u044b\u043b\u043a\u0438<\/h2>\n<p>  <\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/facebook\/jscodeshift\" rel=\"nofollow\">jscodeshift<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/microsoft\/TypeScript\" rel=\"nofollow\">TypeScript<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/microsoft\/TypeScript\/wiki\/Using-the-Compiler-API\" rel=\"nofollow\">Using the Compiler API<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/typescript-eslint\/typescript-eslint\" rel=\"nofollow\">typescript-eslint<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/53804566\/how-to-get-compileroptions-from-tsconfig-json\" rel=\"nofollow\"><code>How to get CompilerOptions<\/code> from <code>tsconfig.json<\/code><\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/53733138\/how-do-i-type-check-a-snippet-of-typescript-code-in-memory\" rel=\"nofollow\">How do I type check a snippet of TypeScript code in memory?<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/49355257\/use-compiler-api-for-type-inference\" rel=\"nofollow\">Use compiler API for type inference<\/a><\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/47215069\/how-to-use-typescript-compiler-api-to-get-normal-function-info-eg-returntype-p\" rel=\"nofollow\">how to use typescript Compiler API to get normal function info, eg: returnType\/parameters?<\/a><\/li>\n<\/ul>\n<\/div>\n<p>               <script class=\"js-mediator-script\">!function(e){function t(t,n){if(!(n in e)){for(var r,a=e.document,i=a.scripts,o=i.length;o--;)if(-1!==i[o].src.indexOf(t)){r=i[o];break}if(!r){r=a.createElement(\"script\"),r.type=\"text\/javascript\",r.async=!0,r.defer=!0,r.src=t,r.charset=\"UTF-8\";var d=function(){var e=a.getElementsByTagName(\"script\")[0];e.parentNode.insertBefore(r,e)};\"[object Opera]\"==e.opera?a.addEventListener?a.addEventListener(\"DOMContentLoaded\",d,!1):e.attachEvent(\"onload\",d):d() } } }t(\"\/\/mediator.mail.ru\/script\/2820404\/\",\"_mediator\")}(window);<\/script>      <br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/480304\/\"> https:\/\/habr.com\/ru\/post\/480304\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html js-mediator-article\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/480304\/\">\n<h1 id=\"vyvod-tipov-v-jscodeshift-i-typescript\">\u0412\u044b\u0432\u043e\u0434 \u0442\u0438\u043f\u043e\u0432 \u0432 jscodeshift \u0438 TypeScript<\/h1>\n<p>  <\/p>\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u0432\u0435\u0440\u0441\u0438\u0438 6.0 <a href=\"https:\/\/github.com\/facebook\/jscodeshift\" rel=\"nofollow\">jscodeshift<\/a> \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 TypeScript (\u0434\u0430\u043b\u0435\u0435 TS). \u0412 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f codemode-\u043e\u0432 (\u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0439), \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0443\u0437\u043d\u0430\u0442\u044c \u0442\u0438\u043f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u044f\u0432\u043d\u043e\u0439 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438. \u041a \u0441\u043e\u0436\u0430\u043b\u0435\u043d\u0438\u044e, jscodeshift \u043d\u0435 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0440\u0435\u0434\u0441\u0442\u0432 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0442\u0438\u043f\u043e\u0432 \u00ab\u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438\u00bb.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-295583","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=295583"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/295583\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=295583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=295583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=295583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}