Skip to content

Commit a90961b

Browse files
authored
Fix shorthand-property-sorting crashing when styles not initialised (#1765)
* Fix shorthand-property-sorting crashing when styles not initialised * Add changeset * Added changeset reminder
1 parent 88d8836 commit a90961b

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

.changeset/purple-parrots-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/eslint-plugin': patch
3+
---
4+
5+
Fix shorthand-property-sorting crashing when variable in css prop is not initialised

.github/pull_request_template.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ I have...
2020

2121
- [ ] Updated or added applicable tests
2222
- [ ] Updated the documentation in `website/`
23+
- [ ] Added a changeset (if making any changes that affect Compiled's behaviour)

packages/eslint-plugin/src/rules/shorthand-property-sorting/__tests__/rule.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,30 @@ tester.run('shorthand-property-sorting', shorthandFirst, {
286286
});
287287
`,
288288
},
289+
290+
//
291+
// styles is not defined
292+
//
293+
294+
{
295+
// People really should not be doing this, so no point in handling this case.
296+
//
297+
// This test case is just to make sure the rule does not crash when it encounters
298+
// this kind of edge case.
299+
name: 'styles is not defined',
300+
code: `
301+
import { css, jsx } from '@compiled/react';
302+
303+
let styles;
304+
const someCondition = true;
305+
if (someCondition) {
306+
styles = css({ top: 0 });
307+
} else {
308+
styles = css({ top: '2px' });
309+
}
310+
export const EmphasisText = ({ children }) => <span css={styles}>{children}</span>;
311+
`,
312+
},
289313
]),
290314

291315
invalid: includedImports.flatMap((imp) => [

packages/eslint-plugin/src/rules/shorthand-property-sorting/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const getVariableDefinition = (
8585
functionCall &&
8686
functionCall.defs.length &&
8787
functionCall.defs[0].node.type === 'VariableDeclarator' &&
88-
functionCall.defs[0].node.init.type === 'CallExpression'
88+
functionCall.defs[0].node.init?.type === 'CallExpression'
8989
) {
9090
return functionCall.defs[0].node.init as CallExpression;
9191
}

0 commit comments

Comments
 (0)