Rewriter

Rewriter

Rewriter is the top level namespace in a synvert snippet. One rewriter checks if the dependency version matches, and it can contain one or many Instances, which define the behavior what files need to find and what codes need to rewrite.

Constructor

new Rewriter(group, name, func)

Create a Rewriter
Source:
Parameters:
Name Type Description
group string group name
name string snippet name
func function a function defines the behaviors of the rewriter

Members

(static) rewriters

Store all rewriters grouped by group name, e.g. `{ jquery: { 'deprecate-event-shorthand': } }`
Source:

Methods

(static) clear()

Clear all registered rewriters.
Source:

(static) fetch(group, name) → {Rewriter}

Fetch a rewriter by group and name.
Source:
Parameters:
Name Type Description
group string rewriter group.
name string rewriter name.
Returns:
Type:
Rewriter
the matching rewriter.

(static) register(group, name, rewriter)

Register a rewriter with its group and name.
Source:
Parameters:
Name Type Description
group string the rewriter group.
name string the unique rewriter name.
rewriter Rewriter the rewriter to register.

addAffectedFile(filePath)

Add an affected file.
Source:
Parameters:
Name Type Description
filePath string file path

(async) addFile(fileName, content)

Async to add a new file.
Source:
Parameters:
Name Type Description
fileName string file name
content string file body

addFileSync(fileName, content)

Sync to add a new file.
Source:
Parameters:
Name Type Description
fileName string file name
content string file body

(async) addSnippet(group, name)

Async to call anther snippet.
Source:
Parameters:
Name Type Description
group string group of another rewriter, if there's no name parameter, the group can be http url, file path or snippet name.
name string name of another rewriter.
Example
new Synvert.Rewriter("jquery", "migrate", async () => {
  await this.addSnippet("jquery", "deprecate-event-shorthand");
  await this.addSnippet("jquery", "deprecate-ready-event");
  await this.addSnippet("https://github.com/synvert-hq/synvert-snippets-javascript/blob/main/lib/javascript/no-useless-constructor.js")
  await this.addSnippet("/Users/flyerhzm/.synvert-javascript/lib/javascript/no-useless-constructor.js")
  await this.addSnippet("javascript/no-useless-constructor")
});

addSnippetSync(group, name)

Sync to call anther snippet.
Source:
Parameters:
Name Type Description
group string group of another rewriter, if there's no name parameter, the group can be http url, file path or snippet name.
name string name of another rewriter.
Example
new Synvert.Rewriter("jquery", "migrate", () => {
  this.addSnippetSync("jquery", "deprecate-event-shorthand");
  this.addSnippetSync("jquery", "deprecate-ready-event");
  this.addSnippetSync("https://github.com/synvert-hq/synvert-snippets-javascript/blob/main/lib/javascript/no-useless-constructor.js")
  this.addSnippetSync("/Users/flyerhzm/.synvert-javascript/lib/javascript/no-useless-constructor.js")
  this.addSnippetSync("javascript/no-useless-constructor")
});

configure(options)

Configure the rewriter.
Source:
Parameters:
Name Type Description
options RewriterOptions
Name Type Attributes Description
sourceType string <optional>
script or module
parser string <optional>
typescript or espree
Example
configure({ parser: "typescript" })

ifNode(version)

Check if node version is greater than or equal to the specified node version.
Source:
Parameters:
Name Type Description
version string specified node version.
Example
ifNode("10.14.0");

ifNpm(name, version)

Compare version of the specified npm.
Source:
Parameters:
Name Type Description
name string npm name.
version string equal, less than or greater than specified version, e.g. '>= 2.0.0',
Example
ifNpm("react", ">= 18.0");

mergeTestResults(results)

Merge test results.
Source:
Parameters:
Name Type Description
results Array.<TestResultExt> test results to be merged

(async) process()

Async to process the rewriter.
Source:

processSync()

Sync to process the rewriter.
Source:

(async) processWithSandbox()

Async to process rewriter with sandbox mode. It will run the func but doesn't change any file.
Source:

processWithSandboxSync()

Sync to process rewriter with sandbox mode. It will run the func but doesn't change any file.
Source:

(async) removeFile(fileName)

Async to remove a file.
Source:
Parameters:
Name Type Description
fileName string file name

removeFileSync(fileName)

Sync to remove a file.
Source:
Parameters:
Name Type Description
fileName string file name

renameFile(filePattern, convertFunc)

Rename filepath to new filepath.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. *.scss
convertFunc string | function new file path string or function to convert file path to new file path.

renameFileSync(filePattern, convertFunc)

Sync to rename filepath to new filepath.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. *.scss
convertFunc string | function new file path string or function to convert file path to new file path.

(async) test() → {Array.<TestResultExt>}

Async to test the rewriter.
Source:
Returns:
Type:
Array.<TestResultExt>
test results

testSync() → {Array.<TestResultExt>}

Sync to test the rewriter.
Source:
Returns:
Type:
Array.<TestResultExt>
test results

(async) withinFile(filePattern, func)

Async to find specified files. It creates an Instance to rewrite code.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. lib/*.js
func function a function rewrites code in the matching files.
Example
new Synvert.Rewriter("javascript", "no-unused-imports", async () => {
  await this.withinFiles('**\/*.js', async function () {
  })
})

(async) withinFile(filePattern, func)

Async to find specified files. It creates an Instance to rewrite code.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. lib/*.js
func function a function rewrites code in the matching files.
Example
new Synvert.Rewriter("javascript", "no-unused-imports", async () => {
  await this.withinFiles('**\/*.js', async function () {
  })
})

(async) withinFiles(filePattern, func)

Async to find specified files. It creates an Instance to rewrite code.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. lib/*.js
func function a function rewrites code in the matching files.
Example
new Synvert.Rewriter("javascript", "no-unused-imports", async () => {
  await this.withinFiles('**\/*.js', async function () {
  })
})

withinFilesSync(filePattern, func)

Sync to find specified files. It creates an Instance to rewrite code.
Source:
Parameters:
Name Type Description
filePattern string pattern to find files, e.g. lib/*.js
func function a function rewrites code in the matching files.
Example
new Synvert.Rewriter("javascript", "no-unused-imports", () => {
  this.withinFilesSync('**\/*.js', function () {
  })
})