Skip to content
This repository was archived by the owner on Apr 19, 2021. It is now read-only.
This repository was archived by the owner on Apr 19, 2021. It is now read-only.

Minifier #9

@thosakwe

Description

@thosakwe
public class MinifierTransformer extends FrayTransformer {
  @Override
  public FrayAsset transform(FrayAsset asset) throws FrayException {
    final String src = asset.readAsString();
    final FrayParser parser = parse(src);
    final UsageTracker usageTracker = new UsageTracker();
    final Map<ParserRuleContext, String> replace = new HashMap<>();

    for (TrackedSymbol symbol : usageTracker.visitCompilationUnit(parser.compilationUnit())) {
      final String originalName = symbol.getName();
      final String newName = getUniqueIdentifier();

      for (SymbolUsage usage : symbol.getUsages()) {
        if (usage.getSource() instanceof FrayParser.IdentifierExpressionContext) {
          // Replace ID's with smaller variants
          replace.put(usage.getSource(), newName);
        }
      }
    }

    String transformed = src;

     for (ParserRuleContext ctx : replace.keySet()) {
       transformed = replaceNode(ctx, transformed, replace.get(ctx));
     }

    return asset.changeText(transformed);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions