enter: function visitProgram()

in packages/eui/scripts/babel/proptypes-from-ts-props/index.js [1368:1425]


        enter: function visitProgram(programPath, state) {
          // only process typescript files
          if (
            path.extname(state.file.opts.filename) !== '.ts' &&
            path.extname(state.file.opts.filename) !== '.tsx'
          )
            return;

          // Extract any of the imported variables from 'react' (SFC, ReactNode, etc)
          // we do this here instead of resolving when the imported values are used
          // as the babel typescript preset strips type-only imports before babel visits their usages
          const importsFromReact = new Set();
          programPath.traverse(
            {
              ImportDeclaration: ({ node }) => {
                if (node.source.value === 'react') {
                  node.specifiers.forEach((specifier) => {
                    if (specifier.type === 'ImportSpecifier') {
                      importsFromReact.add(specifier.local.name);
                    }
                  });
                }
              },
            },
            state
          );
          state.set('importsFromReact', importsFromReact);

          const { opts = {} } = state;
          const typeDefinitions = {};
          state.set('typeDefinitions', typeDefinitions);
          state.set('types', types);

          // extraction options are used to further resolve types imported from other files
          const extractionOptions = {
            state,
            sourceFilename: path.resolve(
              process.cwd(),
              this.file.opts.filename
            ),
            fs: opts.fs || fs,
            parse: (code) => babelCore.parse(code, state.file.opts),
          };

          // collect named TS type definitions for later reference
          for (let i = 0; i < programPath.node.body.length; i++) {
            const bodyNode = programPath.node.body[i];
            const extractedDefinitions =
              extractTypeDefinition(bodyNode, extractionOptions) || [];
            for (let i = 0; i < extractedDefinitions.length; i++) {
              const typeDefinition = extractedDefinitions[i];
              if (typeDefinition) {
                typeDefinitions[typeDefinition.name] =
                  typeDefinition.definition;
              }
            }
          }
        },