cannabis
Version:
TypeScript AST Query library
132 lines • 5.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var misc_utils_of_mine_generic_1 = require("misc-utils-of-mine-generic");
var ts_simple_ast_extra_1 = require("ts-simple-ast-extra");
var typescript_1 = require("typescript");
var astNode_1 = require("../node/astNode");
var config_1 = require("./config");
var getDiagnosticMessages_1 = require("./getDiagnosticMessages");
var _project;
var reuseProject = true;
/**
* Creates a new file with given code. If there is a project loaded, the new file won't be associated with any directory.
* If fileName is passed, make sure is unique, if not it will throw.
*/
function getFile(codeOrNode, fileName) {
var node;
if (typeof codeOrNode === 'string') {
node = getProject().createSourceFile(fileName || getNewFileName(), codeOrNode);
}
else if (astNode_1.isASTNode(codeOrNode)) {
if (config_1.getConfig('verifyProjectRegistered')) {
if (ts_simple_ast_extra_1.isNode(codeOrNode)) {
if (!getProject().getSourceFiles().find(function (f) { return f === codeOrNode.getSourceFile(); })) {
throw new Error('Strange node detected and not supported.\nSeems you have created this node in an independent ts-morph project. You must call loadProject() or setProject() before!');
// node = getProject().createSourceFile(codeOrNode.getSourceFile().getFilePath(), codeOrNode)
}
}
else {
if (!getProject().getDirectories().find(function (d) { return d === codeOrNode; })) {
throw new Error('Strange Directory detected and not supported.\nSeems you have created this node in an independent ts-morph project. You must call loadProject() or setProject() before!');
}
}
}
node = codeOrNode;
}
else {
node = getFile(codeOrNode.getText());
}
return node;
}
exports.getFile = getFile;
function getProject() {
if (!reuseProject || !_project) {
_project = new ts_simple_ast_extra_1.tsMorph.Project({
compilerOptions: {
target: ts_simple_ast_extra_1.ts.ScriptTarget.ES2015,
module: ts_simple_ast_extra_1.ts.ModuleKind.CommonJS,
lib: ['lib.es2015.d.ts'],
jsx: ts_simple_ast_extra_1.ts.JsxEmit.React,
rootDir: ".",
}
});
_astRoot = new ASTRootImpl(_project);
}
return _project;
}
function getNewFileName() {
return misc_utils_of_mine_generic_1.unique('cannabis_test_file_') + ".tsx";
}
/**
* Returns an object representing the project that gives access to the root directories using
* [[getRootDirectories]] which are queriable nodes.
*/
function loadProject(tsConfigFilePath) {
if (_project) {
_project.getSourceFiles().forEach(function (f) { return f.forget(); });
}
_project = new ts_simple_ast_extra_1.tsMorph.Project({ tsConfigFilePath: tsConfigFilePath, addFilesFromTsConfig: true });
return _astRoot = new ASTRootImpl(_project);
}
exports.loadProject = loadProject;
var _astRoot;
/**
* Allows to load an existing ts-morph project instance.
*/
function setProject(project) {
if (_project) {
_project.getSourceFiles().forEach(function (f) { return f.forget(); });
}
_project = project;
return _astRoot = new ASTRootImpl(_project);
}
exports.setProject = setProject;
function getASTRoot() {
if (!_astRoot) {
_astRoot = new ASTRootImpl(getProject());
}
return _astRoot;
}
exports.getASTRoot = getASTRoot;
var ASTRootImpl = /** @class */ (function () {
function ASTRootImpl(_project) {
this._project = _project;
}
ASTRootImpl.prototype.getDiagnostics = function () {
return getDiagnosticMessages_1.getDiagnosticMessages(this._project);
};
/**
* Returns all project's root directories, including those in node_modules project dependencies. The first
* one will be thir project's source directory alghouth you can force omitting node_modules ones with
* [[getRootDirectory]] .
*/
ASTRootImpl.prototype.getRootDirectories = function () {
var d = this._project.getRootDirectories().filter(function (d) { return config_1.getConfig('includeFilesInNodeModules') || !d.getPath().includes('node_modules'); });
if (!d.length) {
var dirs = this._project.getDirectories().filter(function (d) { return config_1.getConfig('includeFilesInNodeModules') || !d.getPath().includes('node_modules'); });
if (typescript_1.displayPartsToString.length) {
return dirs;
}
else {
var dir = this._project.createDirectory('src');
return [dir];
}
}
else {
return d;
}
};
/**
* [[getRootDirectories]] could return many folders since it will include also the ones in node_modules
* project dependencies. Use [[getRootDirectory]] to ignore those.
*/
ASTRootImpl.prototype.getRootDirectory = function () {
var filtered = this.getRootDirectories().filter(function (f) { return config_1.getConfig('includeFilesInNodeModules') || !astNode_1.getASTNodeFilePath(f).includes('node_modules'); });
return filtered.length ? filtered[0] : this.getRootDirectories()[0];
};
ASTRootImpl.prototype.getSourceFiles = function () {
return this._project.getSourceFiles();
};
return ASTRootImpl;
}());
//# sourceMappingURL=file.js.map