Skip to content
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

Fix false negatives for isStandardSyntaxDeclaration #3933

Merged
merged 1 commit into from Apr 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 41 additions & 0 deletions lib/rules/property-no-unknown/__tests__/index.js
Expand Up @@ -226,6 +226,9 @@ testRule(rule, {
{
code:
"import styled from 'styled-components';\nexport default styled.div` margin-${/* sc-custom 'left' */ rtlSwitch}: 12.5px; `;"
},
{
code: "const Component = styled.a`\n\t${rule}: 1;\n`"
}
],

Expand All @@ -236,6 +239,44 @@ testRule(rule, {
message: messages.rejected("colr"),
line: 2,
column: 35
},
{
code: "const Component = styled.a`\n\trule: 1;\n`",
message: messages.rejected("rule"),
line: 2,
column: 2
},
{
code: "const Component = styled.a`\n\trule: ${1};\n`",
message: messages.rejected("rule"),
line: 2,
column: 2
}
]
});

testRule(rule, {
ruleName,
config: [true],
syntax: "html",
accept: [
{
code: '<a style="{{rule}}: 1">'
}
],

reject: [
{
code: '<a style="rule: 1">',
message: messages.rejected("rule"),
line: 1,
column: 11
},
{
code: '<a style="rule: {{1}}">',
message: messages.rejected("rule"),
line: 1,
column: 11
}
]
});
8 changes: 4 additions & 4 deletions lib/utils/__tests__/isStandardSyntaxUrl.test.js
Expand Up @@ -107,16 +107,16 @@ describe("isStandardSyntaxUrl", () => {
expect(isStandardSyntaxUrl("some/@url")).toBeTruthy();
});
it("url with less interpolation without quotes", () => {
expect(isStandardSyntaxUrl("@{less-variable}")).toBeTruthy();
expect(isStandardSyntaxUrl("@{less-variable}")).toBeFalsy();
});
it("url with less interpolation at the start without quotes", () => {
expect(isStandardSyntaxUrl("@{less-variable}/path")).toBeTruthy();
expect(isStandardSyntaxUrl("@{less-variable}/path")).toBeFalsy();
});
it("url with less interpolation in the middle without quotes", () => {
expect(isStandardSyntaxUrl("some/@{less-variable}/path")).toBeTruthy();
expect(isStandardSyntaxUrl("some/@{less-variable}/path")).toBeFalsy();
});
it("url with less interpolation at the end without quotes", () => {
expect(isStandardSyntaxUrl("some/@{less-variable}")).toBeTruthy();
expect(isStandardSyntaxUrl("some/@{less-variable}")).toBeFalsy();
Copy link
Member

Choose a reason for hiding this comment

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

Looks like a regression

Copy link
Member

Choose a reason for hiding this comment

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

Actually, it's a bug fix. Interpolation makes URL non-standard. There are tests for Sass interpolation and they all negative.

Copy link
Member

Choose a reason for hiding this comment

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

👍

});

it("sass interpolation at the start", () => {
Expand Down
4 changes: 2 additions & 2 deletions lib/utils/hasTplInterpolation.js
Expand Up @@ -2,11 +2,11 @@
"use strict";

/**
* Check whether a string has template literal interpolation
* Check whether a string has JS template literal interpolation or HTML-like template
*
* @param {string} string
* @return {boolean} If `true`, a string has template literal interpolation
*/
module.exports = function(string /*: string*/) /*: boolean*/ {
return /\${.+?}/.test(string);
return /{.+?}/.test(string);
hudochenkov marked this conversation as resolved.
Show resolved Hide resolved
};
14 changes: 11 additions & 3 deletions lib/utils/isStandardSyntaxDeclaration.js
@@ -1,16 +1,24 @@
/* @flow */
"use strict";

function isStandardSyntaxLang(lang) {
return (
lang &&
(lang === "css" ||
lang === "custom-template" ||
lang === "template-literal")
);
}

/**
* Check whether a declaration is standard
*/
module.exports = function(decl /*: Object*/) /*: boolean*/ {
const prop = decl.prop;
const parent = decl.parent;

// Declarations belong in a declaration block

if (parent.type === "root") {
// Declarations belong in a declaration block or standard CSS source
if (parent.type === "root" && !isStandardSyntaxLang(parent.source.lang)) {
return false;
}

Expand Down