License detector for UglifyJS that identifies and preserves license comments during minification
Overall
score
98%
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.
Your implementation must provide a filter function that evaluates individual comment tokens and decides whether they should be preserved. The function should handle:
License Pattern Detection: Preserve comments containing license-related keywords including:
@preserve, @cc_on(c), "License", "Copyright"Special Comment Formats: Automatically preserve block comments that start with ! (bang comments)
Position-Based Preservation: Preserve comments that appear on the first line of a file
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.
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 pathvalue: String containing the comment texttype: String indicating comment type ('comment1' for line comments, 'comment2' for block comments)line: Number indicating the line where the comment appearsThe function should return a boolean: true to preserve the comment, false to remove it.
Your implementation must pass the following test cases:
@generates
/**
* 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;Reference implementation for license comment preservation during JavaScript minification.
@satisfied-by
Install with Tessl CLI
npx tessl i tessl/npm-uglify-save-licensedocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10