MCPcopy Create free account
hub / github.com/babel/babel / stmtToDirective

Function stmtToDirective

packages/babel-parser/src/parser/statement.ts:152–175  ·  view source on GitHub ↗

* cast a Statement to a Directive. This method mutates input statement.

(stmt: N.ExpressionStatement)

Source from the content-addressed store, hash-verified

150 * cast a Statement to a Directive. This method mutates input statement.
151 */
152 stmtToDirective(stmt: N.ExpressionStatement): N.Directive {
153 const directive = this.castNodeTo(stmt, "Directive");
154
155 const directiveLiteral = this.castNodeTo(
156 stmt.expression,
157 "DirectiveLiteral",
158 );
159 const expressionValue = directiveLiteral.value;
160 const raw = this.input.slice(
161 this.offsetToSourcePos(directiveLiteral.start!),
162 this.offsetToSourcePos(directiveLiteral.end!),
163 );
164 const val = (directiveLiteral.value = raw.slice(1, -1)); // remove quotes
165
166 this.addExtra(directiveLiteral, "raw", raw);
167 this.addExtra(directiveLiteral, "rawValue", val);
168 this.addExtra(directiveLiteral, "expressionValue", expressionValue);
169
170 directive.value = directiveLiteral;
171 // @ts-expect-error delete non-optional properties
172 delete stmt.expression;
173
174 return directive;
175 }
176
177 parseInterpreterDirective(): N.InterpreterDirective | null {
178 if (!this.match(tt.interpreterDirective)) {

Callers

nothing calls this directly

Calls 2

castNodeToMethod · 0.80
offsetToSourcePosMethod · 0.80

Tested by

no test coverage detected