Skip to content

Commit 6ecf426

Browse files
fix: use declaration end. (#4)
1 parent a2eeafb commit 6ecf426

4 files changed

Lines changed: 31 additions & 4 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@knighted/display-name",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0-alpha.2",
44
"description": "Codemod to add React displayName properties to function components.",
55
"type": "module",
66
"exports": {

src/displayName.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const modify = async (source: string, options: Options = defaultOptions) => {
101101
(!opts.requirePascal || pascal.test(name))
102102
) {
103103
code.appendRight(
104-
declarator.end + 1,
104+
declaration.end,
105105
`\n${name}.displayName = '${name}'${opts.insertSemicolon ? ';' : ''}`,
106106
)
107107
}

test/displayName.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,4 +256,31 @@ describe('@knighted/displayName', () => {
256256
`.replace(/\s/g, ''),
257257
)
258258
})
259+
260+
it('works with multiple declarations', async () => {
261+
const src = `
262+
const A = () => <>a</>,
263+
B = () => <>b</>;
264+
265+
const C = function() {
266+
return <>c</>
267+
}
268+
`
269+
const code = await modify(src)
270+
271+
assert.equal(
272+
code.replace(/\s/g, ''),
273+
`
274+
const A = () => <>a</>,
275+
B = () => <>b</>;
276+
A.displayName = 'A';
277+
B.displayName = 'B';
278+
279+
const C = function() {
280+
return <>c</>
281+
}
282+
C.displayName = 'C';
283+
`.replace(/\s/g, ''),
284+
)
285+
})
259286
})

0 commit comments

Comments
 (0)