Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.8.1

* Variable renaming now also renames variables defined by the `@property` rule.

## 0.8.0

* Added variable renaming capabilities. Users who are interested in using the
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postcss-rename",
"version": "0.8.0",
"version": "0.8.1",
"description": "A PostCSS plugin to replace CSS names based on a customizable renaming scheme.",
"keywords": [
"postcss",
Expand Down
20 changes: 16 additions & 4 deletions src/variable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

import postcss, {Declaration} from 'postcss';
import postcss, {AtRule, Declaration} from 'postcss';
import valueParser from 'postcss-value-parser';
import {type RenamingMap, type VariableRenamingOptions} from './options';
import {type SkipPredicate, createSkipPredicate} from './skip';
Expand Down Expand Up @@ -109,19 +109,31 @@ function plugin({
return parsed.toString();
}

const alreadyProcessedNodes = new Set<Declaration>();
const alreadyProcessedDeclarations = new Set<Declaration>();
function renameDeclaration(declarationNode: Declaration): void {
if (alreadyProcessedNodes.has(declarationNode)) {
if (alreadyProcessedDeclarations.has(declarationNode)) {
return;
}

alreadyProcessedNodes.add(declarationNode);
alreadyProcessedDeclarations.add(declarationNode);

declarationNode.prop = renameVariable(declarationNode.prop);
declarationNode.value = renameValue(declarationNode.value);
}

const alreadyProcessedAtRules = new Set<AtRule>();
function renameAtRuleProperty(atRuleNode: AtRule): void {
if (alreadyProcessedAtRules.has(atRuleNode)) {
return;
}

alreadyProcessedAtRules.add(atRuleNode);

atRuleNode.params = renameVariable(atRuleNode.params);
}

return {
AtRule: {property: renameAtRuleProperty},
Declaration: renameDeclaration,
OnceExit() {
if (outputMapCallback) {
Expand Down
151 changes: 150 additions & 1 deletion test/variable.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -469,14 +469,33 @@ describe('with strategy "none"', () => {
);
});
});

describe('with @property', () => {
const input = `
@property --test-property {
syntax: "<angle>";
inherits: false;
initial-value: 45deg;
}
`;

it('does nothing with no options', () => {
assertPostcss(run(input), input);
});

it('does nothing with an explicit strategy', () => {
assertPostcss(run(input, {strategy: 'none'}), input);
});
});
});

describe('with strategy "debug"', () => {
describe('with no variables', () => {
const input = `
.no-variables-here {
absolutely: "nothing";
}`;
}
`;

const options: plugin.Options = {
strategy: 'debug',
Expand Down Expand Up @@ -946,4 +965,134 @@ describe('with strategy "debug"', () => {
);
});
});

describe('with @property', () => {
const input = `
@property --test-property {
syntax: "<angle>";
inherits: false;
initial-value: 45deg;
}
`;

const expected = `
@property --test-property_ {
syntax: "<angle>";
inherits: false;
initial-value: 45deg;
}
`;

it('adds an underscore after every name', () => {
assertPostcss(run(input, {strategy: 'debug'}), expected);
});

it('emits an output map', () => {
assertMapEquals(
input,
{
'test-property': 'test-property_',
},
{
strategy: 'debug',
},
);
});

it('includes the prefix in the output map', () => {
assertMapEquals(
input,
{
'test-property': 'pf-test-property_',
},
{
strategy: 'debug',
prefix: 'pf',
},
);
});

it('omits excluded names from the output map', () => {
assertMapEquals(
input,
{},
{
strategy: 'debug',
except: ['test-property'],
},
);
});
});
});

describe('with strategy "minimal"', () => {
describe('with declaration + @property', () => {
const input = `
--test-property: 45deg;

@property --test-property {
syntax: "*";
inherits: false;
}`;
Comment on lines +1033 to +1036
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either set syntax: "*" or give this an initial value; otherwise it's invalid per spec.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to syntax: "*".


const expected = `
--a: 45deg;

@property --a {
syntax: "*";
inherits: false;
}`;

it('maps names to the shortest possible strings', () => {
assertPostcss(run(input, {strategy: 'minimal'}), expected);
});

it('includes the prefix in the output map', () => {
assertMapEquals(
input,
{
'test-property': 'pf-a',
},
{
strategy: 'minimal',
prefix: 'pf',
},
);
});
});

describe('with @property + declaration', () => {
const input = `
@property --test-property {
syntax: "*";
inherits: false;
}

--test-property: 45deg;`;

const expected = `
@property --a {
syntax: "*";
inherits: false;
}

--a: 45deg;`;

it('maps names to the shortest possible strings', () => {
assertPostcss(run(input, {strategy: 'minimal'}), expected);
});

it('includes the prefix in the output map', () => {
assertMapEquals(
input,
{
'test-property': 'pf-a',
},
{
strategy: 'minimal',
prefix: 'pf',
},
);
});
});
});
Loading