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

Function getTokenTypesMapping

babel.config.ts:510–548  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

508const tokenTypeSourcePath = "./packages/babel-parser/src/tokenizer/types.ts";
509
510function getTokenTypesMapping() {
511 if (tokenTypesMapping.size === 0) {
512 const tokenTypesAst = parseSync(
513 fs.readFileSync(tokenTypeSourcePath, {
514 encoding: "utf-8",
515 }),
516 {
517 configFile: false,
518 parserOpts: { attachComment: false, plugins: ["typescript"] },
519 }
520 )!;
521
522 let typesDeclaration;
523 for (const n of tokenTypesAst.program.body) {
524 if (n.type === "ExportNamedDeclaration" && n.exportKind === "value") {
525 // @ts-expect-error checked by the if
526 const declarations = n.declaration!.declarations;
527 if (declarations !== undefined) typesDeclaration = declarations[0];
528 if (
529 typesDeclaration !== undefined &&
530 typesDeclaration.id.name === "types"
531 ) {
532 break;
533 }
534 }
535 }
536 if (typesDeclaration === undefined) {
537 throw new Error(
538 "The plugin can not find TokenType definition in " + tokenTypeSourcePath
539 );
540 }
541
542 const tokenTypesDefinition = typesDeclaration.init.expression.properties;
543 for (let i = 0; i < tokenTypesDefinition.length; i++) {
544 tokenTypesMapping.set(tokenTypesDefinition[i].key.name, i);
545 }
546 }
547 return tokenTypesMapping;
548}
549
550function pluginBabelParserTokenType({
551 types: { isIdentifier, numericLiteral },

Callers 1

Calls 2

parseSyncFunction · 0.90
setMethod · 0.65

Tested by

no test coverage detected