Skip to content

Commit

Permalink
Moves 'compacting' code to 'properties' folder-space.
Browse files Browse the repository at this point in the history
Why:

* All those optimizations are dealing with properties so the new
  name better reflects its role;

Also:

* renames `overrideCompactor` to `overrideProperties`;
* renames `compactorOptimize` to `optimizeProperties`.
* cleans up property optimizing code calling from level 2 optimizations
  due to removal of aggressive merging, see #290.
  • Loading branch information
jakubpawlowicz committed Jan 18, 2017
1 parent 5be24e6 commit 6ff65e5
Show file tree
Hide file tree
Showing 32 changed files with 90 additions and 115 deletions.
2 changes: 1 addition & 1 deletion lib/optimizer/level-2/can-override.js
@@ -1,4 +1,4 @@
var understandable = require('./compacting/understandable');
var understandable = require('./properties/understandable');

function backgroundPosition(validator, value1, value2) {
if (!understandable(validator, value1, value2, 0, true) && !validator.isValidKeywordValue('background-position', value2, true)) {
Expand Down
40 changes: 0 additions & 40 deletions lib/optimizer/level-2/compacting/optimize.js

This file was deleted.

9 changes: 2 additions & 7 deletions lib/optimizer/level-2/merge-adjacent.js
@@ -1,6 +1,6 @@
var isMergeable = require('./is-mergeable');

var compactorOptimize = require('./compacting/optimize');
var optimizeProperties = require('./properties/optimize');

var sortSelectors = require('../level-1/sort-selectors');
var tidyRules = require('../level-1/tidy-rules');
Expand All @@ -12,11 +12,6 @@ var serializeRules = require('../../writer/one-time').rules;

var Token = require('../../tokenizer/token');

var OVERRIDE_OPTIONS = {
enabled: true,
merging: true
};

function mergeAdjacent(tokens, context) {
var lastToken = [null, [], []];
var options = context.options;
Expand All @@ -35,7 +30,7 @@ function mergeAdjacent(tokens, context) {

if (lastToken[0] == Token.RULE && serializeRules(token[1]) == serializeRules(lastToken[1])) {
Array.prototype.push.apply(lastToken[2], token[2]);
compactorOptimize(token[1], lastToken[2], true, OVERRIDE_OPTIONS, context);
optimizeProperties(lastToken[2], true, true, context);
token[2] = [];
} else if (lastToken[0] == Token.RULE && serializeBody(token[2]) == serializeBody(lastToken[2]) &&
isMergeable(serializeRules(token[1]), mergeablePseudoClasses, mergeablePseudoElements) &&
Expand Down
9 changes: 2 additions & 7 deletions lib/optimizer/level-2/merge-non-adjacent-by-selector.js
@@ -1,17 +1,12 @@
var canReorder = require('./reorderable').canReorder;
var extractProperties = require('./extract-properties');

var compactorOptimize = require('./compacting/optimize');
var optimizeProperties = require('./properties/optimize');

var serializeRules = require('../../writer/one-time').rules;

var Token = require('../../tokenizer/token');

var OVERRIDE_OPTIONS = {
enabled: true,
merging: true
};

function mergeNonAdjacentBySelector(tokens, context) {
var specificityCache = context.cache.specificity;
var allSelectors = {};
Expand Down Expand Up @@ -73,7 +68,7 @@ function mergeNonAdjacentBySelector(tokens, context) {
Array.prototype.push.apply(target[2], moved[2]);
}

compactorOptimize(target[1], target[2], true, OVERRIDE_OPTIONS, context);
optimizeProperties(target[2], true, true, context);
moved[2] = [];
}
}
Expand Down
9 changes: 2 additions & 7 deletions lib/optimizer/level-2/optimize.js
Expand Up @@ -8,17 +8,12 @@ var removeDuplicateMediaQueries = require('./remove-duplicate-media-queries');
var removeDuplicates = require('./remove-duplicates');
var restructure = require('./restructure');

var compactorOptimize = require('./compacting/optimize');
var optimizeProperties = require('./properties/optimize');

var OptimizationLevel = require('../../options/optimization-level').OptimizationLevel;

var Token = require('../../tokenizer/token');

var OVERRIDE_OPTIONS = {
enabled: true,
merging: true
};

function removeEmpty(tokens) {
for (var i = 0, l = tokens.length; i < l; i++) {
var token = tokens[i];
Expand Down Expand Up @@ -61,7 +56,7 @@ function recursivelyOptimizeProperties(tokens, context) {

switch (token[0]) {
case Token.RULE:
compactorOptimize(token[1], token[2], true, OVERRIDE_OPTIONS, context);
optimizeProperties(token[2], true, true, context);
break;
case Token.BLOCK:
recursivelyOptimizeProperties(token[2], context);
Expand Down
Expand Up @@ -112,7 +112,7 @@ function invalidateOrCompact(properties, position, candidates, validator) {
}
}

function compactShortands(properties, validator) {
function mergeIntoShorthands(properties, validator) {
var candidates = {};
var descriptor;
var componentOf;
Expand Down Expand Up @@ -154,4 +154,4 @@ function compactShortands(properties, validator) {
invalidateOrCompact(properties, i, candidates, validator);
}

module.exports = compactShortands;
module.exports = mergeIntoShorthands;
39 changes: 39 additions & 0 deletions lib/optimizer/level-2/properties/optimize.js
@@ -0,0 +1,39 @@
var mergeIntoShorthands = require('./merge-into-shorthands');
var overrideProperties = require('./override-properties');
var populateComponents = require('./populate-components');

var restoreWithComponents = require('../restore-with-components');

var wrapForOptimizing = require('../../wrap-for-optimizing').all;
var removeUnused = require('../../remove-unused');
var restoreFromOptimizing = require('../../restore-from-optimizing');

var OptimizationLevel = require('../../../options/optimization-level').OptimizationLevel;

function optimizeProperties(properties, withOverriding, withMerging, context) {
var _properties = wrapForOptimizing(properties, false);
var _property;
var i, l;

populateComponents(_properties, context.validator, context.warnings);

for (i = 0, l = _properties.length; i < l; i++) {
_property = _properties[i];
if (_property.block) {
optimizeProperties(_property.value[0][1], withOverriding, withMerging, context);
}
}

if (withOverriding && context.options.level[OptimizationLevel.Two].mergeIntoShorthands) {
overrideProperties(_properties, withMerging, context.options.compatibility, context.validator);
}

if (withMerging && context.options.level[OptimizationLevel.Two].mergeIntoShorthands) {
mergeIntoShorthands(_properties, context.validator);
}

restoreFromOptimizing(_properties, restoreWithComponents);
removeUnused(_properties);
}

module.exports = optimizeProperties;
Expand Up @@ -215,7 +215,7 @@ function intoLayers(values) {
return layers;
}

function compactOverrides(properties, compatibility, merging, validator) {
function overrideProperties(properties, withMerging, compatibility, validator) {
var mayOverride, right, left, component;
var overriddenComponents;
var overriddenComponent;
Expand Down Expand Up @@ -305,7 +305,7 @@ function compactOverrides(properties, compatibility, merging, validator) {
}

left.unused = true;
} else if (merging && left.shorthand && !right.shorthand && isComponentOf(left, right, true)) {
} else if (withMerging && left.shorthand && !right.shorthand && isComponentOf(left, right, true)) {
// maybe `right` can be pulled into `left` which is a shorthand?
if (right.important && !left.important)
continue;
Expand Down Expand Up @@ -348,7 +348,7 @@ function compactOverrides(properties, compatibility, merging, validator) {
override(component, right);
left.dirty = true;
}
} else if (merging && left.shorthand && right.shorthand && left.name == right.name) {
} else if (withMerging && left.shorthand && right.shorthand && left.name == right.name) {
// merge if all components can be merged

if (!left.multiplex && right.multiplex)
Expand All @@ -375,7 +375,7 @@ function compactOverrides(properties, compatibility, merging, validator) {

overrideShorthand(left, right);
left.dirty = true;
} else if (merging && left.shorthand && right.shorthand && isComponentOf(left, right)) {
} else if (withMerging && left.shorthand && right.shorthand && isComponentOf(left, right)) {
// border is a shorthand but any of its components is a shorthand too

if (!left.important && right.important)
Expand Down Expand Up @@ -435,4 +435,4 @@ function compactOverrides(properties, compatibility, merging, validator) {
}
}

module.exports = compactOverrides;
module.exports = overrideProperties;
15 changes: 5 additions & 10 deletions lib/optimizer/level-2/reduce-non-adjacent.js
@@ -1,6 +1,6 @@
var isMergeable = require('./is-mergeable');

var compactorOptimize = require('./compacting/optimize');
var optimizeProperties = require('./properties/optimize');

var cloneArray = require('../../utils/clone-array');

Expand All @@ -9,11 +9,6 @@ var Token = require('../../tokenizer/token');
var serializeBody = require('../../writer/one-time').body;
var serializeRules = require('../../writer/one-time').rules;

var OVERRIDE_OPTIONS = {
enabled: true,
merging: false
};

function reduceNonAdjacent(tokens, context) {
var options = context.options;
var mergeablePseudoClasses = options.compatibility.selectors.mergeablePseudoClasses;
Expand Down Expand Up @@ -83,7 +78,7 @@ function reduceSimpleNonAdjacentCases(tokens, repeated, candidates, options, con
var selector = repeated[i];
var data = candidates[selector];

reduceSelector(tokens, selector, data, {
reduceSelector(tokens, data, {
filterOut: filterOut,
callback: reduceBody
}, options, context);
Expand Down Expand Up @@ -130,7 +125,7 @@ function reduceComplexNonAdjacentCases(tokens, candidates, options, context) {

localContext.data = data;

reduceSelector(tokens, selector, data, {
reduceSelector(tokens, data, {
filterOut: filterOut,
callback: collectReducedBodies
}, options, context);
Expand All @@ -143,7 +138,7 @@ function reduceComplexNonAdjacentCases(tokens, candidates, options, context) {
}
}

function reduceSelector(tokens, selector, data, context, options, outerContext) {
function reduceSelector(tokens, data, context, options, outerContext) {
var bodies = [];
var bodiesAsList = [];
var processedTokens = [];
Expand All @@ -161,7 +156,7 @@ function reduceSelector(tokens, selector, data, context, options, outerContext)
processedTokens.push(where);
}

compactorOptimize(selector, bodies, false, OVERRIDE_OPTIONS, outerContext);
optimizeProperties(bodies, true, false, outerContext);

var processedCount = processedTokens.length;
var propertyIdx = bodies.length - 1;
Expand Down
2 changes: 1 addition & 1 deletion test/binary-test.js
Expand Up @@ -467,7 +467,7 @@ vows.describe('./bin/cleancss')
})
})
.addBatch({
'shorthand compacting': {
'shorthand merging': {
'of (yet) unmergeable properties': pipedContext('a{background:url(image.png);background-color:red}', '-O2 mergeIntoShorthands:off', {
'gets right result': function (error, stdout) {
assert.equal(stdout, 'a{background:url(image.png);background-color:red}');
Expand Down
2 changes: 1 addition & 1 deletion test/optimizer/level-2/break-up-test.js
Expand Up @@ -2,7 +2,7 @@ var assert = require('assert');
var vows = require('vows');

var wrapForOptimizing = require('../../../lib/optimizer/wrap-for-optimizing').all;
var populateComponents = require('../../../lib/optimizer/level-2/compacting/populate-components');
var populateComponents = require('../../../lib/optimizer/level-2/properties/populate-components');
var validator = require('../../../lib/optimizer/validator');
var compatibility = require('../../../lib/utils/compatibility');

Expand Down
2 changes: 1 addition & 1 deletion test/optimizer/level-2/optimize-test.js
Expand Up @@ -71,7 +71,7 @@ vows.describe('level 2 optimizer')
}, { level: 2 })
)
.addBatch(
optimizerContext('unit compacting', {
optimizerContext('unit merging', {
'font-size': [
'div{font-size:1rem;font-size:16px}',
'div{font-size:16px}'
Expand Down
Expand Up @@ -3,10 +3,10 @@ var vows = require('vows');

var wrapForOptimizing = require('../../../../lib/optimizer/wrap-for-optimizing').all;
var compatibility = require('../../../../lib/utils/compatibility');
var populateComponents = require('../../../../lib/optimizer/level-2/compacting/populate-components');
var populateComponents = require('../../../../lib/optimizer/level-2/properties/populate-components');
var validator = require('../../../../lib/optimizer/validator');

var findComponentIn = require('../../../../lib/optimizer/level-2/compacting/find-component-in');
var findComponentIn = require('../../../../lib/optimizer/level-2/properties/find-component-in');

vows.describe(findComponentIn)
.addBatch({
Expand Down
Expand Up @@ -3,10 +3,10 @@ var vows = require('vows');

var wrapForOptimizing = require('../../../../lib/optimizer/wrap-for-optimizing').all;
var compatibility = require('../../../../lib/utils/compatibility');
var populateComponents = require('../../../../lib/optimizer/level-2/compacting/populate-components');
var populateComponents = require('../../../../lib/optimizer/level-2/properties/populate-components');
var validator = require('../../../../lib/optimizer/validator');

var isComponentOf = require('../../../../lib/optimizer/level-2/compacting/is-component-of');
var isComponentOf = require('../../../../lib/optimizer/level-2/properties/is-component-of');

vows.describe(isComponentOf)
.addBatch({
Expand Down
@@ -1,7 +1,7 @@
var assert = require('assert');
var vows = require('vows');

var optimize = require('../../../../lib/optimizer/level-2/compacting/optimize');
var optimizeProperties = require('../../../../lib/optimizer/level-2/properties/optimize');

var tokenize = require('../../../../lib/tokenizer/tokenize');
var inputSourceMapTracker = require('../../../../lib/reader/input-source-map-tracker');
Expand All @@ -27,11 +27,10 @@ function _optimize(source) {
}
}
};
optimize(
tokens[0][1],
optimizeProperties(
tokens[0][2],
true,
{ enabled: true, merging: true },
true,
{ options: options, validator: validator(compat) }
);

Expand Down Expand Up @@ -103,7 +102,7 @@ function overrideContext(longhands) {
return context;
}

vows.describe(optimize)
vows.describe(optimizeProperties)
.addBatch(
overrideContext({
'background-attachment': ['background'],
Expand Down

0 comments on commit 6ff65e5

Please sign in to comment.