|
| 1 | +/** |
| 2 | + * @fileoverview Tests for JSX reference tracking. |
| 3 | + * @author Nicholas C. Zakas |
| 4 | + */ |
| 5 | + |
| 6 | +import { expect } from "chai"; |
| 7 | +import espree from "./util/espree.js"; |
| 8 | +import { analyze } from "../lib/index.js"; |
| 9 | + |
| 10 | +describe("References:", () => { |
| 11 | + |
| 12 | + describe("JSX References:", () => { |
| 13 | + it("should treat JSX identifiers as references", () => { |
| 14 | + const ast = espree(` |
| 15 | + const MyComponent = () => <div/>; |
| 16 | + const element = <MyComponent />; |
| 17 | + `, "script", true); |
| 18 | + |
| 19 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 20 | + const scope = scopeManager.scopes[0]; |
| 21 | + |
| 22 | + expect(scope.variables).to.have.length(2); // MyComponent, element |
| 23 | + expect(scope.references).to.have.length(3); // MyComponent def, element def, MyComponent use |
| 24 | + |
| 25 | + const myComponentRef = scope.references[2]; |
| 26 | + |
| 27 | + expect(myComponentRef.identifier.name).to.equal("MyComponent"); |
| 28 | + expect(myComponentRef.isRead()).to.be.true; |
| 29 | + expect(myComponentRef.resolved).to.equal(scope.variables[0]); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should handle JSX attributes as references", () => { |
| 33 | + const ast = espree(` |
| 34 | + const value = "test"; |
| 35 | + const MyComponent = () => <div attr={value}/>; |
| 36 | + `, "script", true); |
| 37 | + |
| 38 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 39 | + const scope = scopeManager.scopes[0]; |
| 40 | + |
| 41 | + expect(scope.variables).to.have.length(2); // value, MyComponent |
| 42 | + expect(scope.references).to.have.length(2); // value def, MyComponent def |
| 43 | + expect(scope.variables[0].references).to.have.length(2); // value def, value use |
| 44 | + expect(scope.through).to.have.length(0); // attr should not be a reference |
| 45 | + |
| 46 | + const valueRef = scope.references[0]; |
| 47 | + |
| 48 | + expect(valueRef.identifier.name).to.equal("value"); |
| 49 | + expect(valueRef.isWrite()).to.be.true; |
| 50 | + expect(valueRef.resolved).to.equal(scope.variables[0]); |
| 51 | + }); |
| 52 | + |
| 53 | + it("should handle nested JSX component references", () => { |
| 54 | + const ast = espree(` |
| 55 | + const Child = () => <div/>; |
| 56 | + const Parent = () => ( |
| 57 | + <div> |
| 58 | + <Child/> |
| 59 | + <Child/> |
| 60 | + </div> |
| 61 | + ); |
| 62 | + `, "script", true); |
| 63 | + |
| 64 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 65 | + const scope = scopeManager.scopes[0]; |
| 66 | + |
| 67 | + expect(scope.variables).to.have.length(2); // Child, Parent |
| 68 | + expect(scope.references).to.have.length(2); // Child, Parent |
| 69 | + expect(scope.variables[0].references).to.have.length(3); // Child def, Child use x2 |
| 70 | + |
| 71 | + const childRefs = scope.references.filter(ref => ref.identifier.name === "Child"); |
| 72 | + |
| 73 | + expect(childRefs).to.have.length(1); // 1 def + 2 uses |
| 74 | + childRefs.slice(1).forEach(ref => { |
| 75 | + expect(ref.isRead()).to.be.true; |
| 76 | + expect(ref.resolved).to.equal(scope.variables[0]); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should handle JSX fragment references", () => { |
| 81 | + const ast = espree(` |
| 82 | + const MyComponent = () => ( |
| 83 | + <> |
| 84 | + <div/> |
| 85 | + <div/> |
| 86 | + </> |
| 87 | + ); |
| 88 | + `, "script", true); |
| 89 | + |
| 90 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 91 | + const scope = scopeManager.scopes[0]; |
| 92 | + |
| 93 | + expect(scope.variables).to.have.length(1); // MyComponent |
| 94 | + expect(scope.references).to.have.length(1); // MyComponent |
| 95 | + }); |
| 96 | + |
| 97 | + it("should handle JSX fragments with component children", () => { |
| 98 | + const ast = espree(` |
| 99 | + const Child = () => <div/>; |
| 100 | + const Parent = () => ( |
| 101 | + <> |
| 102 | + <Child/> |
| 103 | + <Child/> |
| 104 | + </> |
| 105 | + ); |
| 106 | + `, "script", true); |
| 107 | + |
| 108 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 109 | + const scope = scopeManager.scopes[0]; |
| 110 | + |
| 111 | + expect(scope.variables).to.have.length(2); // Child, Parent |
| 112 | + expect(scope.references).to.have.length(2); // Child, Parent |
| 113 | + expect(scope.variables[0].references).to.have.length(3); // Child def, Child use x2 |
| 114 | + |
| 115 | + const childRefs = scope.references.filter(ref => ref.identifier.name === "Child"); |
| 116 | + |
| 117 | + expect(childRefs).to.have.length(1); // 1 def + 2 uses |
| 118 | + childRefs.slice(1).forEach(ref => { |
| 119 | + expect(ref.isRead()).to.be.true; |
| 120 | + expect(ref.resolved).to.equal(scope.variables[0]); |
| 121 | + }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("should handle JSX spread attributes", () => { |
| 125 | + const ast = espree(` |
| 126 | + const props = { attr: "value" }; |
| 127 | + const MyComponent = () => <div {...props}/>; |
| 128 | + `, "script", true); |
| 129 | + |
| 130 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 131 | + const scope = scopeManager.scopes[0]; |
| 132 | + |
| 133 | + expect(scope.variables).to.have.length(2); // props, MyComponent |
| 134 | + expect(scope.references).to.have.length(2); // props def, MyComponent def |
| 135 | + expect(scope.variables[0].references).to.have.length(2); // props def, props use |
| 136 | + |
| 137 | + const propsRef = scope.references[0]; |
| 138 | + |
| 139 | + expect(propsRef.identifier.name).to.equal("props"); |
| 140 | + expect(propsRef.isWrite()).to.be.true; |
| 141 | + expect(propsRef.resolved).to.equal(scope.variables[0]); |
| 142 | + }); |
| 143 | + |
| 144 | + it("should handle JSX spread attributes with destructuring", () => { |
| 145 | + const ast = espree(` |
| 146 | + const props = { attr: "value" }; |
| 147 | + const MyComponent = ({ attr }) => <div {...props}/>; |
| 148 | + `, "script", true); |
| 149 | + |
| 150 | + const scopeManager = analyze(ast, { ecmaVersion: 6, jsx: true }); |
| 151 | + const scope = scopeManager.scopes[0]; |
| 152 | + |
| 153 | + expect(scope.variables).to.have.length(2); // props, MyComponent |
| 154 | + expect(scope.references).to.have.length(2); // props def, MyComponent def |
| 155 | + expect(scope.variables[0].references).to.have.length(2); // props def, props use |
| 156 | + |
| 157 | + const propsRef = scope.references[0]; |
| 158 | + |
| 159 | + expect(propsRef.identifier.name).to.equal("props"); |
| 160 | + expect(propsRef.isWrite()).to.be.true; |
| 161 | + expect(propsRef.resolved).to.equal(scope.variables[0]); |
| 162 | + }); |
| 163 | + |
| 164 | + }); |
| 165 | + |
| 166 | + |
| 167 | +}); |
0 commit comments