CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-uglify-save-license

License detector for UglifyJS that identifies and preserves license comments during minification

Overall
score

98%

Overview
Eval results
Files

task.mdevals/scenario-7/

License Comment Filter for Code Minification

Build a comment filtering system for a JavaScript minification tool. The system should analyze JavaScript comment tokens and determine which comments should be preserved during minification based on license detection criteria.

Requirements

Your implementation must provide a filter function that evaluates individual comment tokens and decides whether they should be preserved. The function should handle:

  1. License Pattern Detection: Preserve comments containing license-related keywords including:

    • Preservation directives: @preserve, @cc_on
    • License identifiers: MIT, MPL, GPL, BSD, ISCL
    • Copyright indicators: (c), "License", "Copyright"
    • Pattern matching should be case-insensitive
  2. Special Comment Formats: Automatically preserve block comments that start with ! (bang comments)

  3. Position-Based Preservation: Preserve comments that appear on the first line of a file

  4. Consecutive Comment Handling: When a license comment is preserved, automatically preserve comments on immediately following lines (consecutive license blocks), even if they don't match license patterns. This allows multi-line license statements to be preserved together.

  5. Multi-File Processing: Handle processing of multiple files correctly by tracking which file is being processed and resetting state between files

The function receives two parameters:

  • node: An AST node object (may be unused but part of the signature)
  • comment: An object with properties:
    • file: String representing the current file path
    • value: String containing the comment text
    • type: String indicating comment type ('comment1' for line comments, 'comment2' for block comments)
    • line: Number indicating the line where the comment appears

The function should return a boolean: true to preserve the comment, false to remove it.

Test Cases

Your implementation must pass the following test cases:

  • Given a comment with value "Copyright (c) 2024 Example Corp" on line 5, the function returns true @test
  • Given a comment with value "MIT Licensed software" on line 10, the function returns true @test
  • Given a block comment (type 'comment2') with value "! Important notice" on line 3, the function returns true @test
  • Given a comment with value "Just a regular comment" on line 1 of file "app.js", the function returns true (first line preservation) @test
  • Given two consecutive comments where the first (line 2) contains "Licensed under GPL" and the second (line 3) contains "See LICENSE file for details", both should be preserved @test
  • Given a comment with value "TODO: refactor this" on line 15, the function returns false @test
  • Given comments from file "foo.js" followed by comments from file "bar.js", the state should reset between files correctly @test

Implementation

@generates

API

/**
 * Determines whether a comment should be preserved during minification
 * @param {Object} node - AST node (may be unused)
 * @param {Object} comment - Comment token with file, value, type, and line properties
 * @returns {boolean} - True if comment should be preserved, false otherwise
 */
function saveLicense(node, comment) {
  // Implementation here
}

module.exports = saveLicense;

Dependencies { .dependencies }

uglify-save-license { .dependency }

Reference implementation for license comment preservation during JavaScript minification.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-uglify-save-license

tile.json