|
| 1 | +/** |
| 2 | + * @file Functional Tests - getIdentifiers |
| 3 | + * @module estree-util-unassert/utils/tests/functional/getIdentifiers |
| 4 | + */ |
| 5 | + |
| 6 | +import type { |
| 7 | + ArrayPattern, |
| 8 | + AssignmentExpression, |
| 9 | + AssignmentPattern, |
| 10 | + Identifier, |
| 11 | + ImportDeclaration, |
| 12 | + ImportDefaultSpecifier, |
| 13 | + ImportNamespaceSpecifier, |
| 14 | + ImportSpecifier, |
| 15 | + MemberExpression, |
| 16 | + ObjectPattern, |
| 17 | + Property, |
| 18 | + RestElement |
| 19 | +} from 'estree' |
| 20 | +import testSubject from '../get-identifiers' |
| 21 | + |
| 22 | +describe('functional:utils/getIdentifiers', () => { |
| 23 | + let identifiers: Set<string> |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + identifiers.clear() |
| 27 | + }) |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + identifiers = new Set() |
| 31 | + }) |
| 32 | + |
| 33 | + describe('ArrayPattern', () => { |
| 34 | + let identifier: Identifier |
| 35 | + let node: ArrayPattern |
| 36 | + |
| 37 | + beforeAll(() => { |
| 38 | + identifier = { name: 'result', type: 'Identifier' } |
| 39 | + node = { elements: [null, identifier], type: 'ArrayPattern' } |
| 40 | + }) |
| 41 | + |
| 42 | + it('should collect identifier names from node.elements', () => { |
| 43 | + // Act |
| 44 | + testSubject(node, identifiers) |
| 45 | + |
| 46 | + // Expect |
| 47 | + expect(identifiers.has(identifier.name)).to.be.true |
| 48 | + }) |
| 49 | + }) |
| 50 | + |
| 51 | + describe('AssignmentExpression', () => { |
| 52 | + let left: Identifier |
| 53 | + let node: AssignmentExpression |
| 54 | + |
| 55 | + beforeAll(() => { |
| 56 | + left = { name: 'message', type: 'Identifier' } |
| 57 | + |
| 58 | + node = { |
| 59 | + left, |
| 60 | + operator: '=', |
| 61 | + right: { raw: '\'\'', type: 'Literal', value: '' }, |
| 62 | + type: 'AssignmentExpression' |
| 63 | + } |
| 64 | + }) |
| 65 | + |
| 66 | + it('should collect identifier names from node.left', () => { |
| 67 | + // Act |
| 68 | + testSubject(node, identifiers) |
| 69 | + |
| 70 | + // Expect |
| 71 | + expect(identifiers.has(left.name)).to.be.true |
| 72 | + }) |
| 73 | + }) |
| 74 | + |
| 75 | + describe('AssignmentPattern', () => { |
| 76 | + let left: Identifier |
| 77 | + let node: AssignmentPattern |
| 78 | + |
| 79 | + beforeAll(() => { |
| 80 | + left = { name: 'fn', type: 'Identifier' } |
| 81 | + |
| 82 | + node = { |
| 83 | + left, |
| 84 | + right: { name: 'noop', type: 'Identifier' }, |
| 85 | + type: 'AssignmentPattern' |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + it('should collect identifier names from node.left', () => { |
| 90 | + // Act |
| 91 | + testSubject(node, identifiers) |
| 92 | + |
| 93 | + // Expect |
| 94 | + expect(identifiers.has(left.name)).to.be.true |
| 95 | + }) |
| 96 | + }) |
| 97 | + |
| 98 | + describe('Identifier', () => { |
| 99 | + let node: Identifier |
| 100 | + |
| 101 | + beforeAll(() => { |
| 102 | + node = { name: 'devlop', type: 'Identifier' } |
| 103 | + }) |
| 104 | + |
| 105 | + it('should collect identifier name from node.name', () => { |
| 106 | + // Act |
| 107 | + testSubject(node, identifiers) |
| 108 | + |
| 109 | + // Expect |
| 110 | + expect(identifiers.has(node.name)).to.be.true |
| 111 | + }) |
| 112 | + }) |
| 113 | + |
| 114 | + describe('ImportDeclaration', () => { |
| 115 | + let local1: Identifier |
| 116 | + let local2: Identifier |
| 117 | + let node: ImportDeclaration |
| 118 | + |
| 119 | + beforeAll(() => { |
| 120 | + local1 = { name: 'eq', type: 'Identifier' } |
| 121 | + local2 = { name: 'assert', type: 'Identifier' } |
| 122 | + |
| 123 | + node = { |
| 124 | + source: { raw: '\'devlop\'', type: 'Literal', value: 'devlop' }, |
| 125 | + specifiers: [ |
| 126 | + { |
| 127 | + imported: { name: 'equal', type: 'Identifier' }, |
| 128 | + local: local1, |
| 129 | + type: 'ImportSpecifier' |
| 130 | + }, |
| 131 | + { |
| 132 | + imported: { name: 'ok', type: 'Identifier' }, |
| 133 | + local: local2, |
| 134 | + type: 'ImportSpecifier' |
| 135 | + } |
| 136 | + ], |
| 137 | + type: 'ImportDeclaration' |
| 138 | + } |
| 139 | + }) |
| 140 | + |
| 141 | + it('should collect identifier names from node.specifiers', () => { |
| 142 | + // Act |
| 143 | + testSubject(node, identifiers) |
| 144 | + |
| 145 | + // Expect |
| 146 | + expect(identifiers.has(local1.name)).to.be.true |
| 147 | + expect(identifiers.has(local2.name)).to.be.true |
| 148 | + }) |
| 149 | + }) |
| 150 | + |
| 151 | + describe('ImportDefaultSpecifier', () => { |
| 152 | + let local: Identifier |
| 153 | + let node: ImportDefaultSpecifier |
| 154 | + |
| 155 | + beforeAll(() => { |
| 156 | + local = { name: 'strictAssert', type: 'Identifier' } |
| 157 | + node = { local, type: 'ImportDefaultSpecifier' } |
| 158 | + }) |
| 159 | + |
| 160 | + it('should collect identifier name from node.local', () => { |
| 161 | + // Act |
| 162 | + testSubject(node, identifiers) |
| 163 | + |
| 164 | + // Expect |
| 165 | + expect(identifiers.has(local.name)).to.be.true |
| 166 | + }) |
| 167 | + }) |
| 168 | + |
| 169 | + describe('ImportNamespaceSpecifier', () => { |
| 170 | + let local: Identifier |
| 171 | + let node: ImportNamespaceSpecifier |
| 172 | + |
| 173 | + beforeAll(() => { |
| 174 | + local = { name: 'invariant', type: 'Identifier' } |
| 175 | + node = { local, type: 'ImportNamespaceSpecifier' } |
| 176 | + }) |
| 177 | + |
| 178 | + it('should collect identifier name from node.local', () => { |
| 179 | + // Act |
| 180 | + testSubject(node, identifiers) |
| 181 | + |
| 182 | + // Expect |
| 183 | + expect(identifiers.has(local.name)).to.be.true |
| 184 | + }) |
| 185 | + }) |
| 186 | + |
| 187 | + describe('ImportSpecifier', () => { |
| 188 | + let local: Identifier |
| 189 | + let node: ImportSpecifier |
| 190 | + |
| 191 | + beforeAll(() => { |
| 192 | + local = { name: 'eq', type: 'Identifier' } |
| 193 | + |
| 194 | + node = { |
| 195 | + imported: { name: 'equal', type: 'Identifier' }, |
| 196 | + local, |
| 197 | + type: 'ImportSpecifier' |
| 198 | + } |
| 199 | + }) |
| 200 | + |
| 201 | + it('should collect identifier name from node.local', () => { |
| 202 | + // Act |
| 203 | + testSubject(node, identifiers) |
| 204 | + |
| 205 | + // Expect |
| 206 | + expect(identifiers.has(local.name)).to.be.true |
| 207 | + }) |
| 208 | + }) |
| 209 | + |
| 210 | + describe('MemberExpression', () => { |
| 211 | + let node: MemberExpression |
| 212 | + let object: Identifier |
| 213 | + |
| 214 | + beforeAll(() => { |
| 215 | + object = { name: 'devlop', type: 'Identifier' } |
| 216 | + |
| 217 | + node = { |
| 218 | + computed: false, |
| 219 | + object, |
| 220 | + optional: false, |
| 221 | + property: { name: 'unreachable', type: 'Identifier' }, |
| 222 | + type: 'MemberExpression' |
| 223 | + } |
| 224 | + }) |
| 225 | + |
| 226 | + it('should collect identifier names from node.object', () => { |
| 227 | + // Act |
| 228 | + testSubject(node, identifiers) |
| 229 | + |
| 230 | + // Expect |
| 231 | + expect(identifiers.has(object.name)).to.be.true |
| 232 | + }) |
| 233 | + }) |
| 234 | + |
| 235 | + describe('ObjectPattern', () => { |
| 236 | + let node: ObjectPattern |
| 237 | + let value1: Identifier |
| 238 | + let value2: Identifier |
| 239 | + |
| 240 | + beforeAll(() => { |
| 241 | + value1 = { name: 'type', type: 'Identifier' } |
| 242 | + value2 = { name: 'position', type: 'Identifier' } |
| 243 | + |
| 244 | + node = { |
| 245 | + properties: [ |
| 246 | + { |
| 247 | + computed: false, |
| 248 | + key: { raw: '0', type: 'Literal', value: 0 }, |
| 249 | + kind: 'init', |
| 250 | + method: false, |
| 251 | + shorthand: true, |
| 252 | + type: 'Property', |
| 253 | + value: value1 |
| 254 | + }, |
| 255 | + { |
| 256 | + computed: false, |
| 257 | + key: { raw: '1', type: 'Literal', value: 1 }, |
| 258 | + kind: 'init', |
| 259 | + method: false, |
| 260 | + shorthand: true, |
| 261 | + type: 'Property', |
| 262 | + value: value2 |
| 263 | + } |
| 264 | + ], |
| 265 | + type: 'ObjectPattern' |
| 266 | + } |
| 267 | + }) |
| 268 | + |
| 269 | + it('should collect identifier names from node.properties', () => { |
| 270 | + // Act |
| 271 | + testSubject(node, identifiers) |
| 272 | + |
| 273 | + // Expect |
| 274 | + expect(identifiers.has(value1.name)).to.be.true |
| 275 | + expect(identifiers.has(value2.name)).to.be.true |
| 276 | + }) |
| 277 | + }) |
| 278 | + |
| 279 | + describe('Property', () => { |
| 280 | + let node: Property |
| 281 | + let value: Identifier |
| 282 | + |
| 283 | + beforeAll(() => { |
| 284 | + value = { name: 'eq', type: 'Identifier' } |
| 285 | + |
| 286 | + node = { |
| 287 | + computed: false, |
| 288 | + key: { name: 'equal', type: 'Identifier' }, |
| 289 | + kind: 'init', |
| 290 | + method: false, |
| 291 | + shorthand: true, |
| 292 | + type: 'Property', |
| 293 | + value |
| 294 | + } |
| 295 | + }) |
| 296 | + |
| 297 | + it('should collect identifier names from node.value', () => { |
| 298 | + // Act |
| 299 | + testSubject(node, identifiers) |
| 300 | + |
| 301 | + // Expect |
| 302 | + expect(identifiers.has(value.name)).to.be.true |
| 303 | + }) |
| 304 | + }) |
| 305 | + |
| 306 | + describe('RestElement', () => { |
| 307 | + let argument: Identifier |
| 308 | + let node: RestElement |
| 309 | + |
| 310 | + beforeAll(() => { |
| 311 | + argument = { name: 'rest', type: 'Identifier' } |
| 312 | + node = { argument, type: 'RestElement' } |
| 313 | + }) |
| 314 | + |
| 315 | + it('should collect identifier names from node.argument', () => { |
| 316 | + // Act |
| 317 | + testSubject(node, identifiers) |
| 318 | + |
| 319 | + // Expect |
| 320 | + expect(identifiers.has(argument.name)).to.be.true |
| 321 | + }) |
| 322 | + }) |
| 323 | +}) |
0 commit comments