MCPcopy Index your code
hub / github.com/nodejs/node / getLiteralText

Function getLiteralText

test/fixtures/snapshot/typescript.js:14899–14951  ·  view source on GitHub ↗
(node, sourceFile, flags)

Source from the content-addressed store, hash-verified

14897 GetLiteralTextFlags[GetLiteralTextFlags["AllowNumericSeparator"] = 8] = "AllowNumericSeparator";
14898 })(GetLiteralTextFlags = ts.GetLiteralTextFlags || (ts.GetLiteralTextFlags = {}));
14899 function getLiteralText(node, sourceFile, flags) {
14900 var _a;
14901 // If we don't need to downlevel and we can reach the original source text using
14902 // the node's parent reference, then simply get the text as it was originally written.
14903 if (sourceFile && canUseOriginalText(node, flags)) {
14904 return getSourceTextOfNodeFromSourceFile(sourceFile, node);
14905 }
14906 // If we can't reach the original source text, use the canonical form if it's a number,
14907 // or a (possibly escaped) quoted form of the original text if it's string-like.
14908 switch (node.kind) {
14909 case 10 /* SyntaxKind.StringLiteral */: {
14910 var escapeText = flags & 2 /* GetLiteralTextFlags.JsxAttributeEscape */ ? escapeJsxAttributeString :
14911 flags & 1 /* GetLiteralTextFlags.NeverAsciiEscape */ || (getEmitFlags(node) & 16777216 /* EmitFlags.NoAsciiEscaping */) ? escapeString :
14912 escapeNonAsciiString;
14913 if (node.singleQuote) {
14914 return "'" + escapeText(node.text, 39 /* CharacterCodes.singleQuote */) + "'";
14915 }
14916 else {
14917 return '"' + escapeText(node.text, 34 /* CharacterCodes.doubleQuote */) + '"';
14918 }
14919 }
14920 case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
14921 case 15 /* SyntaxKind.TemplateHead */:
14922 case 16 /* SyntaxKind.TemplateMiddle */:
14923 case 17 /* SyntaxKind.TemplateTail */: {
14924 // If a NoSubstitutionTemplateLiteral appears to have a substitution in it, the original text
14925 // had to include a backslash: `not \${a} substitution`.
14926 var escapeText = flags & 1 /* GetLiteralTextFlags.NeverAsciiEscape */ || (getEmitFlags(node) & 16777216 /* EmitFlags.NoAsciiEscaping */) ? escapeString :
14927 escapeNonAsciiString;
14928 var rawText = (_a = node.rawText) !== null && _a !== void 0 ? _a : escapeTemplateSubstitution(escapeText(node.text, 96 /* CharacterCodes.backtick */));
14929 switch (node.kind) {
14930 case 14 /* SyntaxKind.NoSubstitutionTemplateLiteral */:
14931 return "`" + rawText + "`";
14932 case 15 /* SyntaxKind.TemplateHead */:
14933 return "`" + rawText + "${";
14934 case 16 /* SyntaxKind.TemplateMiddle */:
14935 return "}" + rawText + "${";
14936 case 17 /* SyntaxKind.TemplateTail */:
14937 return "}" + rawText + "`";
14938 }
14939 break;
14940 }
14941 case 8 /* SyntaxKind.NumericLiteral */:
14942 case 9 /* SyntaxKind.BigIntLiteral */:
14943 return node.text;
14944 case 13 /* SyntaxKind.RegularExpressionLiteral */:
14945 if (flags & 4 /* GetLiteralTextFlags.TerminateUnterminatedLiterals */ && node.isUnterminated) {
14946 return node.text + (node.text.charCodeAt(node.text.length - 1) === 92 /* CharacterCodes.backslash */ ? " /" : "/");
14947 }
14948 return node.text;
14949 }
14950 return ts.Debug.fail("Literal kind '".concat(node.kind, "' not accounted for."));
14951 }
14952 ts.getLiteralText = getLiteralText;
14953 function canUseOriginalText(node, flags) {
14954 if (nodeIsSynthesized(node) || !node.parent || (flags & 4 /* GetLiteralTextFlags.TerminateUnterminatedLiterals */ && node.isUnterminated)) {

Callers

nothing calls this directly

Calls 6

canUseOriginalTextFunction · 0.85
getEmitFlagsFunction · 0.85
concatMethod · 0.80
failMethod · 0.45

Tested by

no test coverage detected