getScopes.spec.js (4969B)
1 /* eslint max-nested-callbacks: ["error", 4]*/ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 5 6 import getScopes from "../getScopes"; 7 import { populateOriginalSource } from "./helpers"; 8 import cases from "jest-in-case"; 9 10 cases( 11 "Parser.getScopes", 12 ({ name, file, type, locations }) => { 13 const source = populateOriginalSource(file, type); 14 15 locations.forEach(([line, column]) => { 16 const scopes = getScopes({ 17 source, 18 line, 19 column, 20 }); 21 22 expect(scopes).toMatchSnapshot( 23 `getScopes ${name} at line ${line} column ${column}` 24 ); 25 }); 26 }, 27 [ 28 { 29 name: "finds scope bindings in fn body with both lex and non-lex items", 30 file: "scopes/fn-body-lex-and-nonlex", 31 locations: [ 32 [4, 0], 33 [10, 0], 34 [16, 0], 35 [22, 0], 36 ], 37 }, 38 { 39 name: "finds scope bindings in a vue file", 40 file: "scopes/vue-sample", 41 type: "vue", 42 locations: [[14, 0]], 43 }, 44 { 45 name: "finds scope bindings in a typescript file", 46 file: "scopes/ts-sample", 47 type: "ts", 48 locations: [ 49 [9, 0], 50 [13, 4], 51 [17, 0], 52 [33, 0], 53 ], 54 }, 55 { 56 name: "finds scope bindings in a typescript-jsx file", 57 file: "scopes/tsx-sample", 58 type: "tsx", 59 locations: [ 60 [9, 0], 61 [13, 4], 62 [17, 0], 63 [33, 0], 64 ], 65 }, 66 { 67 name: "finds scope bindings in a module", 68 file: "scopes/simple-module", 69 locations: [[7, 0]], 70 }, 71 { 72 name: "finds scope bindings in a JSX element", 73 file: "scopes/jsx-component", 74 locations: [[2, 0]], 75 }, 76 { 77 name: "finds scope bindings for complex binding nesting", 78 file: "scopes/complex-nesting", 79 locations: [ 80 [16, 4], 81 [20, 6], 82 ], 83 }, 84 { 85 name: "finds scope bindings for function declarations", 86 file: "scopes/function-declaration", 87 locations: [ 88 [2, 0], 89 [3, 20], 90 [5, 1], 91 [9, 0], 92 ], 93 }, 94 { 95 name: "finds scope bindings for function expressions", 96 file: "scopes/function-expression", 97 locations: [ 98 [2, 0], 99 [3, 23], 100 [6, 0], 101 ], 102 }, 103 { 104 name: "finds scope bindings for arrow functions", 105 file: "scopes/arrow-function", 106 locations: [ 107 [2, 0], 108 [4, 0], 109 [7, 0], 110 [8, 0], 111 ], 112 }, 113 { 114 name: "finds scope bindings for class declarations", 115 file: "scopes/class-declaration", 116 locations: [ 117 [2, 0], 118 [5, 0], 119 [7, 0], 120 ], 121 }, 122 { 123 name: "finds scope bindings for class expressions", 124 file: "scopes/class-expression", 125 locations: [ 126 [2, 0], 127 [5, 0], 128 [7, 0], 129 ], 130 }, 131 { 132 name: "finds scope bindings for for loops", 133 file: "scopes/for-loops", 134 locations: [ 135 [2, 0], 136 [3, 17], 137 [4, 17], 138 [5, 25], 139 [7, 22], 140 [8, 22], 141 [9, 23], 142 [11, 23], 143 [12, 23], 144 [13, 24], 145 ], 146 }, 147 { 148 name: "finds scope bindings for try..catch", 149 file: "scopes/try-catch", 150 locations: [ 151 [2, 0], 152 [4, 0], 153 [7, 0], 154 ], 155 }, 156 { 157 name: "finds scope bindings for out of order declarations", 158 file: "scopes/out-of-order-declarations", 159 locations: [ 160 [2, 0], 161 [5, 0], 162 [11, 0], 163 [14, 0], 164 [17, 0], 165 ], 166 }, 167 { 168 name: "finds scope bindings for block statements", 169 file: "scopes/block-statement", 170 locations: [ 171 [2, 0], 172 [6, 0], 173 ], 174 }, 175 { 176 name: "finds scope bindings for class properties", 177 file: "scopes/class-property", 178 locations: [ 179 [2, 0], 180 [4, 16], 181 [6, 12], 182 [7, 0], 183 ], 184 }, 185 { 186 name: "finds scope bindings and exclude Flowtype", 187 file: "scopes/flowtype-bindings", 188 locations: [ 189 [8, 0], 190 [10, 0], 191 ], 192 }, 193 { 194 name: "finds scope bindings for declarations with patterns", 195 file: "scopes/pattern-declarations", 196 locations: [[1, 0]], 197 }, 198 { 199 name: "finds scope bindings for switch statements", 200 file: "scopes/switch-statement", 201 locations: [ 202 [2, 0], 203 [5, 0], 204 [7, 0], 205 [9, 0], 206 [11, 0], 207 [17, 0], 208 [21, 0], 209 ], 210 }, 211 { 212 name: "finds scope bindings with proper types", 213 file: "scopes/binding-types", 214 locations: [ 215 [5, 0], 216 [9, 0], 217 [18, 0], 218 [23, 0], 219 ], 220 }, 221 { 222 name: "finds scope bindings with expression metadata", 223 file: "scopes/expressions", 224 locations: [[2, 0]], 225 }, 226 ] 227 );