Skip to content

Commit

Permalink
feat(string mutations): don't mutate Symbol descriptions (#4407)
Browse files Browse the repository at this point in the history
Don't mutate `Symbol('foo')` (but still mutate `Symbol('foo' + bar)`).
  • Loading branch information
lex-funy committed Sep 22, 2023
1 parent f589ca8 commit bdd0d5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/instrumenter/src/mutators/string-literal-mutator.ts
Expand Up @@ -31,6 +31,7 @@ function isValidParent(child: NodePath<babel.types.StringLiteral>): boolean {
types.isObjectMethod(parent) ||
(types.isObjectProperty(parent) && parent.key === child.node) ||
(types.isClassProperty(parent) && parent.key === child.node) ||
(types.isCallExpression(parent) && types.isIdentifier(parent.callee, { name: 'require' }))
(types.isCallExpression(parent) && types.isIdentifier(parent.callee, { name: 'require' })) ||
(types.isCallExpression(parent) && types.isIdentifier(parent.callee, { name: 'Symbol' }))
);
}
Expand Up @@ -39,6 +39,20 @@ describe(sut.name, () => {
expectJSMutation(sut, '"use strict";"use asm";');
expectJSMutation(sut, 'function a() {"use strict";"use asm";}');
});

it('should not mutate string literals in symbols with descriptions', () => {
expectJSMutation(sut, "const a = Symbol('foo');");
expectJSMutation(sut, 'const a = Symbol("foo");');
});

it('should not mutate symbols without description', () => {
expectJSMutation(sut, 'const a = Symbol();');
});

it('should mutate template literals in symbols with descriptions', () => {
expectJSMutation(sut, 'const a = Symbol(`foo`);', 'const a = Symbol(``);');
expectJSMutation(sut, "const a = Symbol('foo' + 'bar');", 'const a = Symbol(\'foo\' + "");', 'const a = Symbol("" + \'bar\');');
});
});

describe('imports/exports', () => {
Expand Down

0 comments on commit bdd0d5c

Please sign in to comment.