@lightweightform/theme-common
Version:
Common utilities for Lightweightform themes
130 lines (129 loc) • 5.86 kB
TypeScript
import { Path } from '@angular-devkit/core';
import { Tree } from '@angular-devkit/schematics';
import ts from '@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript';
import { Change } from '@schematics/angular/utility/change';
/**
* RegExp used to determine whether a string is a valid TS identifier.
*/
export declare const validTsIdRegExp: RegExp;
/**
* Symbol representing a "default binding" (import or export).
*/
export declare const defaultBinding: unique symbol;
/**
* Class used to insert a new import in a TS file.
*/
export declare class TsImport {
name: string | typeof defaultBinding;
location: string;
as?: string | undefined;
/**
* Creates a new TS import.
* @param name Name to import (or the default import symbol). `null` to import
* no name (e.g. `import 'module';`).
* @param location Import location.
* @param as Identifier to import as (use also when importing a default).
*/
constructor(name: string | typeof defaultBinding, location: string, as?: string | undefined);
}
/**
* Source of a given TypeScript file given its path.
* @param host Source tree.
* @param path File path for which to fetch source.
* @returns TypeScript source file.
*/
export declare function getTsSourceFile(host: Tree, path: Path): ts.SourceFile;
/**
* Location used to import a file relatively to another file in a TS import.
* @param sourcePath File path where the import is to be inserted.
* @param importPath File being imported.
* @returns Relative file path from the source file to the one being imported.
*/
export declare function relativeTsImportPath(sourcePath: Path, importPath: Path): string;
/**
* Creates a change that inserts a new TS import.
* @param source TS source.
* @param path Path of the TS file on which to import.
* @param toImport Import object representing import to add.
* @returns Change to apply.
*/
export declare function insertTsImport(source: ts.SourceFile, path: Path, toImport: TsImport): Change;
/**
* Creates a change that removes a TS import.
* @param source TS source.
* @param path Path of the TS file on which to remove the import.
* @param name Import name (or the default import symbol).
* @param location Import location.
*/
export declare function removeTsImport(source: ts.SourceFile, path: Path, name: string | typeof defaultBinding, location: string): Change;
/**
* Obtains the id with which a certain name was imported in a given file,
* `undefined` if the given name hasn't been imported. E.g., let `source`
* contain:
* ```ts
* import {w, x as y} from 'location';
* ```
* Then:
* ```ts
* getImportId(source, 'w', 'location'); // Returns `'w'`
* getImportId(source, 'x', 'location'); // Returns `'y'`
* getImportId(source, 'z', 'location'); // Returns `undefined`
* ```
* @param source TS source.
* @param name Name of the import (or the default import symbol).
* @param location Import location.
* @returns Id of the imported name or `undefined` if the name hasn't been
* imported.
*/
export declare function getTsImportId(source: ts.SourceFile, name: string | typeof defaultBinding, location: string): string | undefined;
/**
* Obtains the declaration exporting the provided name (or default export).
* @param source TS source.
* @param name Name of the exported declaration (or default export name).
* @returns Declaration exported with the provided name (or default export).
*/
export declare function getTsExportedDeclaration(source: ts.SourceFile, name: string | typeof defaultBinding): ts.FunctionDeclaration | ts.ClassDeclaration | ts.VariableDeclaration | undefined;
/**
* Given a TS node, attempts to find the value represented by said node. I.e.,
* if the given node **is not** an identifier, the given node is returned back;
* if the node **is** an identifier, we try to find a declaration within the
* same file matching said identifier and return the declared value; if no such
* declaration is found, we check if the identifier has been imported from
* somewhere, in which case we return a `TsImport` with the name of the imported
* identifier and its location.
* @param source Source code where the node has been found.
* @param node Node for which to get a non-identifier node.
*/
export declare function getTsValue(source: ts.SourceFile, node?: ts.Node): ts.Node | TsImport | undefined;
/**
* Transforms a given string in a string that can be used as the property of
* an object. E.g.:
* ```ts
* stringAsProperty('aaaa'); // Returns `'aaaa'`
* stringAsProperty('1xx'); // Returns `'\'1xx\''`
* ```
* @param string String to transform into a string that can be used as property.
* @returns String that can be used as an object property.
*/
export declare function stringAsProperty(id: string): string;
/**
* Transforms a given string that represents an object property into the actual
* name of the property (removing extra delimiters, etc.). E.g.:
* ```ts
* propertyAsString('aaaa'); // Returns `'aaaa'`
* propertyAsString('\'1xx\''); // Returns `'1xx'`
* ```
* @param property Property (as a string) from which to get the name.
* @returns Name of the property.
*/
export declare function propertyAsString(property: string): string;
/**
* Creates a change that inserts a given element (as a string) in a given array
* of nodes.
* @param filePath Path of file being edited.
* @param nodeArr Array of nodes where to insert the new element.
* @param nodeStr New node to insert (as a string).
* @param separator Separator used between nodes.
* @returns Change to apply.
*/
export declare function insertInNodeArray(filePath: Path, nodeArr: ts.NodeArray<any>, nodeStr: string, separator?: string): Change;