Skip to content

Commit

Permalink
Fix renameTo false positive for JSX tags
Browse files Browse the repository at this point in the history
  • Loading branch information
henryqdineen authored and Daniel15 committed Oct 3, 2024
1 parent 9355203 commit e7a9a72
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/collections/VariableDeclarator.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,15 @@ const transformMethods = {
return false;
}

if (
(types.JSXOpeningElement.check(parent) || types.JSXClosingElement.check(parent)) &&
parent.name === path.node &&
/^[a-z]/.test(path.node.name)
) {
// <oldName></oldName>
return false;
}

return true;
})
.forEach(function(path) {
Expand Down
33 changes: 33 additions & 0 deletions src/collections/__tests__/VariableDeclarator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,39 @@ describe('VariableDeclarators', function() {
expect(identifiers.length).toBe(1);
});

it('does not rename JSX open and closing tags that start with a lowercase letter', function () {
nodes = [recast.parse([
'var span = useRef(null);',
'var element = <span ref={span}></span>;',
].join('\n'), {parser: getParser()}).program];

Collection.fromNodes(nodes)
.findVariableDeclarators('span')
.renameTo('spanRef');

const identifiers = Collection.fromNodes(nodes)
.find(types.JSXIdentifier, { name: 'spanRef' });

expect(identifiers.length).toBe(0);
});

it('does rename JSX open and closing tags that are capitalized', function () {
nodes = [recast.parse([
'var Span = require("./Span");',
'var span = useRef(null);',
'var element = <Span ref={span}></Span>;',
].join('\n'), {parser: getParser()}).program];

Collection.fromNodes(nodes)
.findVariableDeclarators('Span')
.renameTo('SpanComponent');

const identifiers = Collection.fromNodes(nodes)
.find(types.JSXIdentifier, { name: 'SpanComponent' });

expect(identifiers.length).toBe(2);
});

describe('parsing with bablylon', function() {
it('does not rename object property', function () {
nodes = [
Expand Down

0 comments on commit e7a9a72

Please sign in to comment.