Skip to content

Commit

Permalink
Fix case when raws.after/before could be undefined or null (#4313)
Browse files Browse the repository at this point in the history
* Fix case when raws.after/before could be undefined or null

* test: add correct test cases with css-in-js
  • Loading branch information
vankop authored and hudochenkov committed Oct 9, 2019
1 parent febfbb6 commit 2f272bc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
25 changes: 25 additions & 0 deletions lib/rules/no-eol-whitespace/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -668,3 +668,28 @@ testRule(rule, {
},
],
});

testRule(rule, {
ruleName,
syntax: 'css-in-js',
config: [true],

accept: [
{
code: `
export const a = styled.div\`
a {}
\`;
export const b = styled.div\`
a {}
\`;
`,
},
{
code: `export default <a style={{
width: 'calc(100% - 80px)'
}} />;`,
},
],
});
16 changes: 9 additions & 7 deletions lib/rules/no-eol-whitespace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,16 @@ const rule = function(on, options, context) {
isRootFirst,
);

const lastEOL = Math.max(
root.raws.after.lastIndexOf('\n'),
root.raws.after.lastIndexOf('\r'),
);
if (typeof root.raws.after === 'string') {
const lastEOL = Math.max(
root.raws.after.lastIndexOf('\n'),
root.raws.after.lastIndexOf('\r'),
);

if (lastEOL !== root.raws.after.length - 1) {
root.raws.after =
root.raws.after.slice(0, lastEOL + 1) + fixString(root.raws.after.slice(lastEOL + 1));
if (lastEOL !== root.raws.after.length - 1) {
root.raws.after =
root.raws.after.slice(0, lastEOL + 1) + fixString(root.raws.after.slice(lastEOL + 1));
}
}
}

Expand Down

0 comments on commit 2f272bc

Please sign in to comment.