Skip to content

chore: update postcss-selector-parser #12

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

Merged
merged 1 commit into from
Mar 4, 2019
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
18 changes: 15 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,19 @@ function localizeNode(rule, mode, options) {
switch (node.type) {
case 'root': {
let resultingGlobal;

context.hasPureGlobals = false;

newNodes = node.nodes.map(function(n) {
const nContext = {
global: context.global,
lastWasSpacing: true,
hasLocals: false,
explicit: false,
};

n = transform(n, nContext);

if (typeof resultingGlobal === 'undefined') {
resultingGlobal = nContext.global;
} else if (resultingGlobal !== nContext.global) {
Expand All @@ -59,11 +63,14 @@ function localizeNode(rule, mode, options) {
'" (multiple selectors must result in the same mode for the rule)'
);
}

if (!nContext.hasLocals) {
context.hasPureGlobals = true;
}

return n;
});

context.global = resultingGlobal;

node.nodes = normalizeNodeArray(newNodes);
Expand Down Expand Up @@ -160,7 +167,6 @@ function localizeNode(rule, mode, options) {
);
}

const next = node.parent;
const addBackSpacing = !!node.spaces.before;

context.ignoreNextSpacing = context.lastWasSpacing
Expand Down Expand Up @@ -205,6 +211,7 @@ function localizeNode(rule, mode, options) {
context.lastWasSpacing = false;
context.ignoreNextSpacing = false;
context.enforceNoSpacing = false;

return node;
};

Expand All @@ -213,11 +220,10 @@ function localizeNode(rule, mode, options) {
hasPureGlobals: false,
};

const selector = selectorParser(root => {
rootContext.selector = selectorParser(root => {
transform(root, rootContext);
}).processSync(rule, { updateSelector: false, lossless: true });

rootContext.selector = selector;
return rootContext;
}

Expand Down Expand Up @@ -400,6 +406,7 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
if (typeof options !== 'object') {
options = {}; // If options is undefined or not an object the plugin fails
}

if (options && options.mode) {
if (
options.mode !== 'global' &&
Expand All @@ -411,8 +418,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
);
}
}

const pureMode = options && options.mode === 'pure';
const globalMode = options && options.mode === 'global';

return function(css) {
css.walkAtRules(function(atrule) {
if (/keyframes$/i.test(atrule.name)) {
Expand Down Expand Up @@ -450,6 +459,7 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
});
}
});

css.walkRules(function(rule) {
if (
rule.parent.type === 'atrule' &&
Expand Down Expand Up @@ -480,7 +490,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
'(pure selectors must contain at least one local class or id)'
);
}

rule.selector = context.selector;

// Less-syntax mixins parse as rules with no nodes
if (rule.nodes) {
rule.nodes.forEach(decl => localizeDecl(decl, context));
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"dependencies": {
"postcss": "^7.0.6",
"postcss-selector-parser": "^5.0.0",
"postcss-selector-parser": "^6.0.0",
"postcss-value-parser": "^3.3.1"
},
"devDependencies": {
Expand Down
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@ const tests = [
input: '.foobar {}',
expected: ':local(.foobar) {}',
},
{
should: 'scope escaped selectors',
input: '.\\3A \\) {}',
expected: ':local(.\\3A \\)) {}',
},
{
should: 'scope ids',
input: '#foobar {}',
expected: ':local(#foobar) {}',
},
{
should: 'scope escaped ids',
input: '#\\#test {}',
expected: ':local(#\\#test) {}',
},
{
should: 'scope escaped ids (2)',
input: '#u-m\\00002b {}',
expected: ':local(#u-m\\00002b) {}',
},
{
should: 'scope multiple selectors',
input: '.foo, .baz {}',
Expand Down
Loading