{"size": 768, "ext": "js", "max_stars_repo_path": "src/fixExports.js", "max_stars_repo_name": "kentandersen/js-unused-exports", "max_stars_repo_stars_event_min_datetime": "2019-05-08T05:47:48.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-03T23:56:31.000Z", "max_issues_repo_path": "src/fixExports.js", "max_issues_repo_name": "kentandersen/js-unused-exports", "max_issues_repo_issues_event_min_datetime": "2020-02-07T13:42:05.000Z", "max_issues_repo_issues_event_max_datetime": "2021-12-29T09:32:11.000Z", "max_forks_repo_path": "src/fixExports.js", "max_forks_repo_name": "kentandersen/js-unused-exports", "max_forks_repo_forks_event_min_datetime": "2020-02-07T13:39:03.000Z", "max_forks_repo_forks_event_max_datetime": "2021-12-04T08:22:56.000Z"}
{"max_stars_count": 45, "max_issues_count": 15, "max_forks_count": 7, "avg_line_length": 26.4827586207, "max_line_length": 73, "alphanum_fraction": 0.6901041667}
import fs from 'fs';
import { escapeRegExp } from './utils';

export default function fixExports(unusedExports, config) {
  Object.values(unusedExports).forEach((unused) => {
    const indentifierNames = unused.unusedExports.map((exp) => exp.name);
    const sourceBefore = fs.readFileSync(unused.sourcePath, 'utf8');

    const sourceAfter = removeExportDeclarations(
      sourceBefore,
      indentifierNames,
      config
    );

    fs.writeFileSync(unused.sourcePath, sourceAfter);
  });
}

export function removeExportDeclarations(source, identifierNames) {
  const identifiers = identifierNames.map(escapeRegExp).join('|');

  const re = new RegExp(
    `export\\s+((const|function)\\s+(${identifiers}))\\b`,
    'g'
  );

  return source.replace(re, '$1');
}
