TypescriptAdapter

TypescriptAdapter

Typescript Adapter

Constructor

new TypescriptAdapter()

Source:

Extends

  • Adapter

Methods

childNodeRange(node, childName) → {Object}

Get the source range of child node.
Source:
Parameters:
Name Type Description
node Node The node.
childName string The name to find child node.
Throws:
if we can't get the range.
Type
NotSupportedError
Returns:
Type:
Object
The range of the child node, e.g. { start: 0, end: 10 }
Example
const node = ts.createSourceFile("code.ts", "function foobar(foo, bar) {}")
childNodeRange(node, "name") // { start: "function ".length, end: "function foobar".length }

// node array
const node = ts.createSourceFile("code.ts", "function foobar(foo, bar) {}")
childNodeRange(node, "parameters") // { start: "function foobar".length, end: "function foobar(foo, bar)".length }

// index for node array
const node = ts.createSourceFile("code.ts", "function foobar(foo, bar) {}")
childNodeRange(node, "parameters.1") // { start: "function foobar(foo, ".length, end: "function foobar(foo, bar".length }

// {name}Property for node who has properties
const node = ts.createSourceFile("code.ts", "const foobar = { foo: 'foo', bar: 'bar' }")
childNodeRange(node, "declarationList.declarations.0.initializer.fooProperty") // { start: "const foobar = { ".length, end: 'const foobar = { foo: "foo"'.length }

// {name}Value for node who has properties
const node = ts.createSourceFile("code.ts", "const foobar = { foo: 'foo', bar: 'bar' }")
childNodeRange(node, "declarationList.declarations.0.initializer.fooValue") // { start: "const foobar = { foo: ".length, end: 'const foobar = { foo: "foo"'.length }

// {name}Attribute for jsx node who has properties
const node = ts.createSourceFile("code.tsx", '<Field name="email" autoComplete="email" />')
childNodeRange(node, "expression.attriutes.autoCompleteAttribute") // { start: '<Field name="email" '.length, end: '<Field name="email" autoComplete="email"'.length }

// semicolon for PropertyAssignment node
const node = ts.createSourceFile("code.ts", "{ foo: bar }");
childNodeRange(node, "semicolon") // { start: "{ foo", end: "{ foo:".length }

// dot for PropertyAccessExpression node
const node = ts.createSourceFile("code.ts", "foo.bar")
childNodeRange(node, "dot") // { start: "foo".length, end: "foo.".length }

childNodeValue(node, childName) → {any}

Get the value of child node.
Source:
Parameters:
Name Type Description
node Node The node to evaluate.
childName string The name to find child node.
Returns:
Type:
any
The value of child node, it can be a node, an array, a string or a number.
Example
const node = ts.createSourceFile("code.ts", "foobar(foo, bar)")
childNodeValue(node, "expression.arguments.0") // node["expression"]["arguments"][0]

// node array
const node = ts.createSourceFile("code.ts", 'foobar("foo", "bar")')
childNodeValue(node, "expression.arguments") // node["expression"]["arguments"]

// {name}Property for node who has properties
const node = ts.createSourceFile("code.ts", "const foobar = { foo: 'foo', bar: 'bar' }")
childNodeValue(node, 'declarationList.declarations.0.initializer.fooProperty')) // node["declarationList"]["declarations"][0]["initializer"]["properties"][0]

// {name}Initializer for node who has properties
const node = ts.createSourceFile("code.ts", "const foobar = { foo: 'foo', bar: 'bar' }")
childNodeValue(node, 'declarationList.declarations.0.initializer.fooInitializer')) // node["declarationList"]["declarations"][0]["initializer"]["properties"][0]["initalizer"]

// {name}Attribute for jsx node who has properties
const node = ts.createSourceFile("code.tsx", '<Field name="email" autoComplete="email" />')
childNodeValue(node, 'expression.attributes.autoCompleteAttribute')) // node["exression"]["attributes"]["properties"][1]