Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
setIsDefaultExport(value: boolean) {
if (value === this.isDefaultExport())
return this;
if (value && !TypeGuards.isSourceFile(this.getParentOrThrow()))
throw new errors.InvalidOperationError("The parent must be a source file in order to set this node as a default export.");
// remove any existing default export
const sourceFile = this.getSourceFile();
const fileDefaultExportSymbol = sourceFile.getDefaultExportSymbol();
if (fileDefaultExportSymbol != null)
sourceFile.removeDefaultExport(fileDefaultExportSymbol);
if (!value)
return this;
// set this node as the one to default export
if (TypeGuards.hasName(this) && shouldWriteAsSeparateStatement.call(this)) {
const parentSyntaxList = this.getFirstAncestorByKindOrThrow(SyntaxKind.SyntaxList);
const name = this.getName();
handleNode(currentNode: Node, newNode: ts.Node, newSourceFile: ts.SourceFile) {
/* istanbul ignore next */
if (!Node.isSourceFile(currentNode))
throw new errors.InvalidOperationError(`Can only use a ${nameof(TryOrForgetNodeHandler)} with a source file.`);
try {
this.handler.handleNode(currentNode, newNode, newSourceFile);
} catch (ex) {
currentNode._context.logger.warn("Could not replace tree, so forgetting all nodes instead. Message: " + ex);
// forget all the source file's nodes
currentNode.getChildSyntaxListOrThrow().forget();
// replace the source file with the temporary source file
currentNode._context.compilerFactory.replaceCompilerNode(currentNode, newNode);
}
}
}
private _throwIfForgotten() {
if (this._compilerObject != null)
return;
const message = "Attempted to get a text range that was forgotten. "
+ "Text ranges are forgotten after a manipulation has occurred. "
+ "Please re-request the text range after manipulations.";
throw new errors.InvalidOperationError(message);
}
}
const replacementSourceFile = sourceFile._context.compilerFactory.createCompilerSourceFileFromText(
newFilePath || sourceFile.getFilePath(),
newFileText,
sourceFile.getScriptKind()
);
nodeHandler.handleNode(sourceFile, replacementSourceFile, replacementSourceFile);
} catch (err) {
const diagnostics = getSyntacticDiagnostics(sourceFile, newFileText);
const errorDetails = err.message + "\n\n"
+ `-- Details --\n`
+ "Path: " + sourceFile.getFilePath() + "\n"
+ "Text: " + JSON.stringify(textManipulator.getTextForError(newFileText)) + "\n"
+ "Stack: " + err.stack;
if (diagnostics.length > 0) {
throw new errors.InvalidOperationError(
"Manipulation error: " + "A syntax error was inserted." + "\n\n"
+ sourceFile._context.project.formatDiagnosticsWithColorAndContext(diagnostics, { newLineChar: "\n" })
+ "\n" + errorDetails
);
}
throw new errors.InvalidOperationError("Manipulation error: " + errorDetails);
}
}
function verifyInRange(i: number) {
if (i >= validRange[0] && i <= validRange[1])
return;
throw new errors.InvalidOperationError(`Cannot insert or replace text outside the bounds of the node. `
+ `Expected a position between [${validRange[0]}, ${validRange[1]}], but received ${i}.`);
}
}
function getSourceFileFromNode(compilerNode: ts.Node) {
if (compilerNode.kind === SyntaxKind.SourceFile)
return compilerNode as ts.SourceFile;
if (compilerNode.parent == null)
throw new errors.InvalidOperationError("Please ensure the node was created from a source file with 'setParentNodes' set to 'true'.");
let parent = compilerNode;
while (parent.parent != null)
parent = parent.parent;
if (parent.kind !== SyntaxKind.SourceFile)
throw new errors.NotImplementedError("For some reason the top parent was not a source file.");
return parent as ts.SourceFile;
}
}
private getToolRequiredError(name: string) {
return new errors.InvalidOperationError(`A ${name} is required for this operation. `
+ "This might occur when manipulating or getting type information from a node that was not added "
+ `to a Project object and created via ${nameof(createWrappedNode)}. `
+ `Please submit a bug report if you don't believe a ${name} should be required for this operation.`);
}
}