Skip to content

Support id selectors #80

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions lib/core/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import gonzales from './gonzales';
import type { JsNode, gASTNode } from '../types';

import {
getRegularClassesMap,
getRegularClassesAndIdsMap,
getComposesClassesMap,
getExtendClassesMap,
getParentSelectorClassesMap,
Expand Down Expand Up @@ -150,13 +150,13 @@ export const getStyleClasses = (ast: gASTNode): ?Object => {
*/
eliminateGlobals(ast);

const classesMap = getRegularClassesMap(ast);
const classesAndIdsMap = getRegularClassesAndIdsMap(ast);
const composedClassesMap = getComposesClassesMap(ast);
const extendClassesMap = getExtendClassesMap(ast);
const parentSelectorClassesMap = getParentSelectorClassesMap(ast);

return {
...classesMap,
...classesAndIdsMap,
...composedClassesMap,
...extendClassesMap,
...parentSelectorClassesMap
Expand Down
4 changes: 2 additions & 2 deletions lib/core/traversalUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const getICSSExportPropsMap = (ast: gASTNode): classMapType => {
)(ruleSets);
};

export const getRegularClassesMap = (ast: gASTNode): classMapType => {
export const getRegularClassesAndIdsMap = (ast: gASTNode): classMapType => {
const ruleSets: Array<gASTNode> = [];
ast.traverseByType('ruleset', node => ruleSets.push(node));

Expand All @@ -51,7 +51,7 @@ export const getRegularClassesMap = (ast: gASTNode): classMapType => {
fp.map('content'),
fp.filter({ type: 'ident' }),
fp.flatMap('content'),
fp.filter({ type: 'class' }),
fp.filter(node => node.is('class') || node.is('id')),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Love the initiative! A small suggestion here: This function is intended to return regular classes. What do you think about creating a new one for retrieving ids or renaming the function?

Copy link
Author

Choose a reason for hiding this comment

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

Renamed the function because it would be a shame to have to go over the ast one more time just for that

fp.flatMap('content'),
fp.filter({ type: 'selector' }),
fp.flatMap('content'),
Expand Down
3 changes: 3 additions & 0 deletions test/files/id.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#scopedId {
color: red;
}
28 changes: 28 additions & 0 deletions test/lib/rules/no-undef-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,19 @@ ruleTester.run('no-undef-class', rule, {
`,
options: [{ camelCase: 'dashes-only' }],
}),

/*
support id selectors
*/
test({
code: `
import s from './id.scss';

export default Foo = () => (
<div id={s.scopedId}></div>
);
`,
}),
Comment on lines +362 to +370
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does this work with no-unused-class as well?

Copy link
Author

Choose a reason for hiding this comment

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

Added tests for no-unused-class as well as an error test for no-undef-class

],
/*
invalid cases
Expand Down Expand Up @@ -656,5 +669,20 @@ ruleTester.run('no-undef-class', rule, {
'Class or exported property \'foo-baz\' not found',
],
}),
/*
support id selectors
*/
test({
code: `
import s from './id.scss';

export default Foo = () => (
<div id={s.badScopedId}></div>
);
`,
errors: [
'Class or exported property \'badScopedId\' not found',
],
}),
],
});
27 changes: 27 additions & 0 deletions test/lib/rules/no-unused-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ ruleTester.run('no-unused-class', rule, {
`,
options: [{ camelCase: 'dashes-only' }],
}),
/*
support id selectors
*/
test({
code: `
import s from './id.scss';

export default Foo = () => (
<div id={s.scopedId}></div>
);
`,
}),
],
/*
invalid cases
Expand Down Expand Up @@ -419,5 +431,20 @@ ruleTester.run('no-unused-class', rule, {
'Unused classes found in noUnusedClass3.scss: foo-bar, alreadyCamelCased, snake_cased',
],
}),
/*
support id selectors
*/
test({
code: `
import s from './id.scss';

export default Foo = () => (
<div>Not using s.scopedId</div>
);
`,
errors: [
'Unused classes found in id.scss: scopedId',
],
}),
],
});