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
Ignore map property name (#4375) #4450
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ea07119
Ignore map property name (#4375)
fanich37 780c68b
Update isMap, add comments
fanich37 619bf0c
Update types
fanich37 33fa00c
Update lib/utils/isMap.js
fanich37 faf53ee
Update lib/utils/isMap.js
fanich37 a25e742
Clarify const purpose
fanich37 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use strict'; | ||
|
||
const isMap = require('../isMap'); | ||
const sass = require('postcss-sass'); | ||
const scss = require('postcss-scss'); | ||
const valueParser = require('postcss-value-parser'); | ||
|
||
describe('isMap', () => { | ||
const simpleMaps = [ | ||
['$map: (prop: "flex");', true], | ||
['$font: (italic bold 10px/8pix)', false], | ||
['$map: (prop: /* comment */ 0);', true], | ||
['$calc: calc(100% / 2px);', false], | ||
['$url: url();', false], | ||
]; | ||
const nestedMaps = [ | ||
['$map: (prop: 0, prop2: (prop3: "normal"), prop4: 2px);', [0, 17]], | ||
['$map: (prop: 0, prop2: (prop3: "normal", prop4: (prop5: "grid")), prop6: 2px);', [0, 17, 42]], | ||
]; | ||
|
||
test.each(simpleMaps)('simple maps', (css, expected) => { | ||
runTests(css, (decl) => { | ||
const parsedValue = valueParser(decl.value).nodes[0]; | ||
|
||
expect(isMap(parsedValue)).toBe(expected); | ||
}); | ||
}); | ||
|
||
test.each(nestedMaps)('nested maps', (css, expected) => { | ||
runTests(css, (decl) => { | ||
const parsedValue = valueParser(decl.value); | ||
|
||
parsedValue.walk(function(valueNode) { | ||
if (expected.includes(valueNode.sourceIndex)) { | ||
expect(isMap(valueNode)).toBeTruthy(); | ||
} else { | ||
expect(isMap(valueNode)).toBeFalsy(); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
it('empty map returns `false`', () => { | ||
const emptyMap = '$map: ();'; | ||
|
||
runTests(emptyMap, (decl) => { | ||
const parsedValue = valueParser(decl.value); | ||
|
||
expect(isMap(parsedValue)).toBeFalsy(); | ||
}); | ||
}); | ||
}); | ||
|
||
function sassDecls(css, cb) { | ||
sass.parse(css).walkDecls(cb); | ||
} | ||
|
||
function scssDecls(css, cb) { | ||
scss.parse(css).walkDecls(cb); | ||
} | ||
|
||
function runTests(css, cb) { | ||
[sassDecls, scssDecls].forEach((fn) => { | ||
fn(css, cb); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
'use strict'; | ||
|
||
/** @typedef {import('postcss-value-parser').Node} ValueNode */ | ||
|
||
/** | ||
* @param {ValueNode | undefined} valueNode | ||
* @returns {boolean} | ||
*/ | ||
module.exports = function(valueNode) { | ||
if (!valueNode) { | ||
return false; | ||
} | ||
|
||
if (valueNode.type !== 'function' || !valueNode.nodes || valueNode.value) { | ||
return false; | ||
} | ||
|
||
// It's necessary to remove comments and spaces if they are present | ||
const cleanNodes = valueNode.nodes.filter( | ||
(node) => node.type !== 'comment' && node.type !== 'space', | ||
); | ||
|
||
// Map without comments and spaces will have the structure like $map (prop: value, prop2: value) | ||
// ↑ ↑ ↑ ↑ | ||
// 0 1 2 3 | ||
if (cleanNodes[0] && cleanNodes[0].type !== 'word' && cleanNodes[0].type !== 'string') { | ||
return false; | ||
} | ||
|
||
if (cleanNodes[1] && cleanNodes[1].value !== ':') { | ||
return false; | ||
} | ||
|
||
// There is no need to check type or value of this node since it could be anything | ||
if (!cleanNodes[2]) { | ||
return false; | ||
} | ||
|
||
if (cleanNodes[3] && cleanNodes[3].value !== ',') { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain what this variable for, please? I don't understand from the following.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The map property name (in map cleared from comments & spaces) always has index that being divided by 4 gives remainder equals 0 (e.g. 0, 4, 8 etc).
The naming of this
const
definitely has to be changed since it's not clear. Any suggestions? Or may be comment would be enough?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think comment in the code would be enough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comment with explanation.