Created
July 20, 2021 02:51
-
-
Save brianneisler/40c1d8c90415dffecb5aef1d6ae5c212 to your computer and use it in GitHub Desktop.
How to rename a function in firemin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { assocNodePath, findNodeInTree, findNodePathInTree } from 'firetree' | |
import { createIdentifier } from 'firetree/parser/pipes' | |
import { Identifier } from 'firetree/parser/tokens' | |
import { List } from 'immutable' | |
import { propEq } from 'ramda' | |
import { measure } from '../../utils' | |
const renameFunction = measure( | |
(context, ast, functionId) => `renameFunction:${functionId}`, | |
(context, ast, functionId, newName) => { | |
const functionDeclaration = findNodeInTree(propEq('id', functionId), ast) | |
const { identifier } = functionDeclaration | |
const identifierPath = findNodePathInTree(propEq('id', identifier.id), ast) | |
// NOTE: The assocNotePath method automatically reidentifies all nodes in the tree | |
// within that path that was modified. This will automatically regenerate all of the | |
// FunctionDeclaration metadata. So only the target identifer that represents the | |
// function's name needs to be changed and assocNodePath will do the rest. | |
return assocNodePath( | |
context, | |
identifierPath, | |
createIdentifier({ | |
id: identifier.id, | |
tokenList: List([Identifier.parse(context, newName)]) | |
}), | |
ast | |
) | |
} | |
) | |
export default renameFunction |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { generateString, setupContext, tokenize } from 'firetree' | |
import { FunctionDeclaration } from 'firetree/parser/nodes' | |
import renameFunction from './renameFunction' | |
describe('renameFunction', () => { | |
test('should rename empty function with name', async () => { | |
const context = setupContext({ | |
logger: console | |
}) | |
const declaration = FunctionDeclaration.parse( | |
context, | |
await tokenize(context, { string: 'function test() { return true }' }) | |
) | |
const result = renameFunction(context, declaration, declaration.id, 'f1') | |
expect(generateString(context, { ast: result })).toBe('function f1() { return true }') | |
}) | |
test('should rename function but NOT rename parameter with same name', async () => { | |
const context = setupContext({ | |
logger: console | |
}) | |
const declaration = FunctionDeclaration.parse( | |
context, | |
await tokenize(context, { string: 'function test(test) { return test }' }) | |
) | |
const result = renameFunction(context, declaration, declaration.id, 'f1') | |
expect(generateString(context, { ast: result })).toBe('function f1(test) { return test }') | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment