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-10/

Comment Filter for Code Minification

Build a comment filtering function that determines which comments should be preserved during code minification. The function should apply multiple criteria to intelligently decide whether each comment is important enough to keep.

Requirements

Your function should accept a comment object and return a boolean indicating whether the comment should be preserved.

The comment object has the following structure:

  • file: The file path being processed
  • type: The comment type ('comment1' for line comments, 'comment2' for block comments)
  • value: The text content of the comment
  • line: The line number where the comment appears

Decision Criteria

A comment should be preserved if it meets ANY of the following conditions:

  1. The comment contains license-related keywords (case-insensitive): @preserve, @cc_on, MIT, MPL, GPL, BSD, ISCL, (c), License, or Copyright
  2. The comment is a block comment that starts with ! (bang notation)
  3. The comment appears on line 1 of the file
  4. The comment appears immediately after a previously preserved comment (on the next line) in the same file

State Management

Your function must maintain state across multiple invocations to handle consecutive comments correctly:

  • Track the line number of the last preserved comment
  • Track the current file being processed
  • Reset state when switching to a different file

Test Cases

  • When given a comment with MIT License in the content, the function returns true @test
  • When given a block comment starting with !, the function returns true @test
  • When given a comment on line 1, the function returns true @test
  • When given a comment on line 3 immediately after a preserved comment on line 2, the function returns true @test
  • When given a regular comment with no license keywords, not on line 1, not a bang comment, and not after a preserved comment, the function returns false @test
  • When switching to a new file, the state resets correctly @test

Implementation

@generates

API

/**
 * Determines whether a comment should be preserved during minification.
 *
 * @param {Object} node - AST node from the parser (unused but part of standard signature)
 * @param {Object} comment - Comment information object
 * @param {string} comment.file - Current file path being processed
 * @param {string} comment.type - Comment type ('comment1' for line, 'comment2' for block)
 * @param {string} comment.value - Comment text content
 * @param {number} comment.line - Line number of the comment
 * @returns {boolean} True if the comment should be preserved, false otherwise
 */
function shouldPreserveComment(node, comment) {
  // IMPLEMENTATION HERE
}

module.exports = shouldPreserveComment;

Dependencies { .dependencies }

uglify-save-license { .dependency }

Provides comment preservation logic for minification tools.

Install with Tessl CLI

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

tile.json