Skip to content

Commit

Permalink
[PR feedback] lint *all* template literals, not just css``
Browse files Browse the repository at this point in the history
- since we have several Emotion utilities that pass back raw ``s instead of css``
  • Loading branch information
cee-chen committed Aug 18, 2022
1 parent a0b5625 commit 172b3b8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
16 changes: 7 additions & 9 deletions scripts/eslint-plugin/css_logical_properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ module.exports = {
},
create: function (context) {
return {
TaggedTemplateExpression(node) {
if (node.tag?.name !== 'css') return; // We only want to check Emotion css`` template literals

const templateContents = node.quasi?.quasis || [];
templateContents.forEach((cssNode) => {
const stringLiteral = cssNode?.value?.raw;
TemplateLiteral(node) {
const templateContents = node.quasis || [];
templateContents.forEach((quasi) => {
const stringLiteral = quasi?.value?.raw;
if (!stringLiteral) return;

findOccurrences(regex, stringLiteral).forEach(
({ match, lineNumber, column }) => {
const property = match.groups.property || match.groups.value;
const whitespace = match.groups.whitespace?.length || 0;

const lineStart = cssNode.loc.start.line + lineNumber;
const lineStart = quasi.loc.start.line + lineNumber;
const columnStart = column + whitespace;

context.report({
Expand All @@ -67,8 +65,8 @@ module.exports = {
: 'preferLogicalProperty',
data: { property },
fix: function (fixer) {
const cssNodeStart = cssNode.range[0] + 1; // Account for "css`"
const indexStart = cssNodeStart + match.index + whitespace;
const literalStart = quasi.range[0] + 1; // Account for backtick
const indexStart = literalStart + match.index + whitespace;

return fixer.replaceTextRange(
[indexStart, indexStart + property.length],
Expand Down
12 changes: 5 additions & 7 deletions scripts/eslint-plugin/css_logical_properties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ const valid = [
inline-size: 50px;
inline-start-end: 10px;
\``,
// This is not in css``, so it's fine
'`width: 50px; left: 10px;`',
// Make sure we don't incorrectly catch similar properties that do not have logical equivalents
`css\`
`\`
line-height: 20px;
border-width: 1px;
scrollbar-width: 30px
Expand All @@ -27,8 +25,8 @@ const invalid = [
errors: [{ messageId: 'preferLogicalProperty' }],
},
{
code: 'css`max-height: 50px;`',
output: 'css`max-block-size: 50px;`',
code: '`max-height: 50px;`',
output: '`max-block-size: 50px;`',
errors: [{ messageId: 'preferLogicalProperty' }],
},
{
Expand All @@ -37,8 +35,8 @@ const invalid = [
errors: [{ messageId: 'preferLogicalProperty' }],
},
{
code: 'css`min-width: 50px;`',
output: 'css`min-inline-size: 50px;`',
code: '`min-width: 50px;`',
output: '`min-inline-size: 50px;`',
errors: [{ messageId: 'preferLogicalProperty' }],
},
{
Expand Down

0 comments on commit 172b3b8

Please sign in to comment.