tractor-js-file-transformer
v0.2.0-beta.0
Published
A bunch of AST manipulation utilities for [**tractor**](https://github.com/TradeMe/tractor).
Downloads
16
Readme
tractor-js-file-transformer
A bunch of AST manipulation utilities for tractor.
API:
transformIdentifiers
:
Renames an identifier within an AST
Arguments:
file:
JavaScriptFile
- The JavaScript file to manipulateoldName: string
- the name of the identifiers that you want to changenewName: string
- the new name for the identifierscontextName?: string
- a context for selecting the elements, which should be a esquery query
Usage:
import { transformIdentifiers } from 'tractor-js-file-transformer';
// Replace all instances of
foo
withbar
transformIdentifiers(file, 'foo', 'bar');
// Replace all instances of
var foo
withvar bar
transformIdentifiers(file, 'foo', 'bar', 'VariableDeclarator');
### `transformMetadata`:
> Renames an metadata object stored in the first comment of a JS file
> #### Arguments:
> * `file: `[`JavaScriptFile`](https://github.com/phenomnomnominal/tractor-server#JavaScriptFile) - The JavaScript file to manipulate
> * `oldName: string` - the name of the identifiers that you want to change
> * `newName: string` - the new name for the identifiers
> * `type?: string` - the type of the metadata object
> #### Usage:
> ```javascript
import { transformMetadata } from 'tractor-js-file-transformer';
>// Changes metadata comment from
// /* { "name": "foo" } */
// to
// /* { "name": "bar" } */
transformMetadata(file, 'foo', 'bar');
>// Changes metadata comment from
// /* { "items": [{ "name": "foo" }, { "name": "baz" }] } */
// to
// /* { "items": [{ "name": "bar" }, { "name": "baz" }] } */
transformMetadata(file, 'foo', 'bar', 'items');
transformRequirePaths
:
Updates the path in a
require
call when a file moves.
Arguments:
file:
JavaScriptFile
- The JavaScript file to manipulateoptions:
TransformRequireOptions
Usage:
import { transformRequirePaths } from 'tractor-js-file-transformer';
// Replace //
require('../../../that/goes/to/another/file')
// with //require('../../../that/goes/to/a/new/file')
transformRequirePaths(file, { fromPath: '/some/path/to/a/file', oldToPath: '/some/path/that/goes/to/another/file', newToPath: '/some/path/that/goes/to/a/new/file' });
Interfaces:
TransformRequireOptions:
interface TransformRequireOptions {
oldFromPath?: string;
newFromPath?: string;
fromPath?: string;
oldToPath?: string;
newToPath?: string;
toPath?: string;
}