Skip to content

Commit

Permalink
fix: parent selector linter should check selector which has no style (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MadCcc authored Jan 19, 2023
1 parent a7e184d commit 3dff670
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
15 changes: 8 additions & 7 deletions src/linters/parentSelectorLinter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Linter } from '..';
import type { Linter } from '..';
import { lintWarning } from './utils';

const linter: Linter = (key, value, info) => {
const currentSelector = info.parentSelectors[info.parentSelectors.length - 1];
if (currentSelector) {
const selectors = currentSelector.split(',');
if (selectors.some((selector) => selector.split('&').length > 2)) {
lintWarning('Should not use more than one `&` in a selector.', info);
}
if (
info.parentSelectors.some((selector) => {
const selectors = selector.split(',');
return selectors.some((item) => item.split('&').length > 2);
})
) {
lintWarning('Should not use more than one `&` in a selector.', info);
}
};

Expand Down
4 changes: 1 addition & 3 deletions src/linters/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ export function lintWarning(message: string, info: LinterInfo) {
devWarning(
false,
`[Ant Design CSS-in-JS] ${path ? `Error in ${path}: ` : ''}${message}${
parentSelectors.length
? ` Selector: ${parentSelectors[parentSelectors.length - 1]}`
: ''
parentSelectors.length ? ` Selector: ${parentSelectors.join(' | ')}` : ''
}`,
);
}
32 changes: 31 additions & 1 deletion tests/linter.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ describe('style warning', () => {
};
render(<Demo />);
expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining('Selector info: .demo'),
expect.stringContaining('Selector: .demo'),
);
});

Expand Down Expand Up @@ -388,5 +388,35 @@ describe('style warning', () => {
),
);
});

it('should check selector which has no style', () => {
const genStyle = (): CSSObject => ({
'.ant': {
'&&-test': {
'&-btn': {
color: 'red',
},
},
},
});
const Demo = () => {
const [token] = useCacheToken<DerivativeToken>(theme, []);
useStyleRegister({ theme, token, path: ['parent selector3'] }, () => [
genStyle(),
]);
return <div />;
};
render(
<StyleProvider cache={createCache()} linters={[parentSelectorLinter]}>
<Demo />
</StyleProvider>,
);

expect(errorSpy).toHaveBeenCalledWith(
expect.stringContaining(
`Should not use more than one \`&\` in a selector.`,
),
);
});
});
});

0 comments on commit 3dff670

Please sign in to comment.