kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/typescript/testdata/function.ts (about)

     1  export {}
     2  
     3  // Declare a type for the function to return, to check indexing through
     4  // the return type.
     5  //- @Num defines/binding Num
     6  interface Num {
     7    //- @#0num defines/binding _NumAttr
     8    num: number;
     9  }
    10  
    11  //- TestDef defines F
    12  //- TestDef.node/kind anchor
    13  //- @test defines/binding F
    14  //- F.node/kind function
    15  //- @a defines/binding ParamA
    16  //- ParamA.node/kind variable
    17  //- F param.0 ParamA
    18  //- @#0Num ref Num
    19  //- @#1Num ref Num
    20  //- TestDef.loc/start @^function
    21  function test(a: number, num: Num): Num {
    22    // TODO(evanm): it would be nice for @num ref NumAttr.  TypeScript seems to
    23    // know they're linked, in that if you rename 'num' above it knows to rename
    24    // this one.  However, it may be the case that they are not linked beyond
    25    // renaming -- experimentally, I found that if you have some doc comment on
    26    // the def'n of 'num' above, it doesn't show up when you hover 'num' below.
    27    //- @a ref ParamA
    28    return {num: a};
    29    //- TestDef.loc/end @$"}"
    30  }
    31  
    32  //- @test ref F
    33  test(
    34      3,
    35      {num: 3});
    36  
    37  // Test computed function names by creating a constant string and checking that
    38  // a name computed from it is properly referenced to its computed definition.
    39  //- @#0"fn" defines/binding OFnStr
    40  const fn = 'fn';
    41  let o = {
    42    //- !{@"[fn]" defines/binding _}
    43    //- @fn ref OFnStr
    44    [fn]() {},
    45  };
    46  
    47  //- @fn ref OFnStr
    48  o[fn]();
    49  
    50  // Test arrow functions
    51  //- @x defines/binding X
    52  //- X.node/kind variable
    53  let x: 3;
    54  
    55  //- @#0x defines/binding ArrowX
    56  //- ArrowX.node/kind variable
    57  //- @#1x ref ArrowX
    58  //- !{@#1x ref X}
    59  test((x => x + 1)(3), undefined);
    60  
    61  // Test default function arguments
    62  //- @x ref X
    63  function defaultArgument(a: number = x) {}
    64  
    65  //- @NestedArray defines/binding NestedArray
    66  type NestedArray = number[][];
    67  
    68  //- @bindingTest defines/binding BF
    69  //- @aa defines/binding AA
    70  //- AA.node/kind variable
    71  //- AA childof BF
    72  //- BF param.0 AA
    73  //- !{ @bb defines/binding _ }
    74  //- @cc defines/binding CC
    75  //- BF param.1 CC
    76  //- @dd defines/binding DD
    77  //- BF param.2 DD
    78  //- @ee defines/binding EE
    79  //- BF param.3 EE
    80  //- @ff defines/binding FF
    81  //- BF param.4 FF
    82  //- @NestedArray ref NestedArray
    83  function bindingTest({aa, bb: {cc, dd}}, [ee, [ff]]: NestedArray) {
    84    //- @dd ref/writes DD
    85    //- @ff ref FF
    86    dd = ff;
    87  }
    88  
    89  // Test function expressions introducing an Anon scope
    90  //- @ev defines/binding Ev
    91  //- @an defines/binding An
    92  let ev, an;
    93  (function() {
    94  //- !{ @ev defines/binding _Ev2=Ev }
    95  let ev;
    96  //- @an ref An
    97  an;
    98  })();
    99  
   100  //- AnonArrowDef defines _
   101  //- AnonArrowDef.node/kind anchor
   102  //- AnonArrowDef.loc/start @^"arg"
   103  //- AnonArrowDef.loc/end @$"42"
   104  (arg => 42)();
   105  
   106  //- AnonFunctionDef defines _
   107  //- AnonFunctionDef.node/kind anchor
   108  //- AnonFunctionDef.loc/start @^"function"
   109  (function() {
   110    return 42;
   111  //- AnonFunctionDef.loc/end @$"}"
   112  })();