Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove comments before parsing calc value #171

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"jison-gho": "^0.6.1-216",
"postcss": "^8.2.2",
"postcss": "^8.4.6",
"prettier": "^2.5.1",
"typescript": "^4.5.5",
"uvu": "^0.5.3"
Expand Down
20 changes: 10 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/lib/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,22 @@ const reducer = require('./reducer.js');
const stringifier = require('./stringifier.js');

const MATCH_CALC = /((?:-(moz|webkit)-)?calc)/i;
/**
* @param {valueParser.Node[]} nodes
* @return {valueParser.Node[]}
*/
function removeComments(nodes) {
const newNodes = [];
for (const node of nodes) {
if (node.type !== 'comment') {
if (node.type === 'function') {
node.nodes = removeComments(node.nodes);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I suppose there is no risk of stack overflow as people won't nest function that deeply.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We can remove useful comments...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The issue PostCSS had was with comments that replaced spaces:

margin: 10px/* comment */20px;

that would become

margin:10px20px;

I've had a look at postcss-discard-comment, and interestingly because it looks at node.raws, it would repair the CSS value that PostCSS parssed incorrectly! It's also true that discard-comments does not remove !important comments, so maybe to get comment removal right we would end up duplicating too much logic from postcss-discard-comments

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't mind this improvement, just weird regression and parsing... Let's merge

}
newNodes.push(node);
}
}
return newNodes;
}

/**
* @param {string} value
Expand All @@ -23,8 +39,9 @@ function transformValue(value, options, result, item) {
return;
}

const noComments = removeComments(node.nodes);
// stringify calc expression and produce an AST
const contents = valueParser.stringify(node.nodes);
const contents = valueParser.stringify(noComments);
const ast = parser.parse(contents);

// reduce AST to its simplest form, that is, either to a single value
Expand Down