Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #326 from rambleraptor/unquote
Lint rule to check for unquote() receiving an unquoted string
- Loading branch information
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
src/rules/function-unquote-no-unquoted-strings-inside/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# function-unquote-no-unquoted-strings-inside | ||
|
||
Disallow unquoted strings inside the [unquote function](https://sass-lang.com/documentation/functions/string#unquote) | ||
|
||
```scss | ||
p { | ||
font-family: unquote(Helvetica); | ||
/** ↑ ↑ | ||
* This function call is unnecessary | ||
*/ | ||
} | ||
``` | ||
|
||
## Options | ||
|
||
### `true` | ||
|
||
The following patterns are considered violations: | ||
|
||
```scss | ||
a { | ||
font-family: unquote(Helvetica); | ||
} | ||
``` | ||
|
||
```scss | ||
$font: Helvetica; | ||
p { | ||
font-family: quote($font); | ||
} | ||
``` | ||
|
||
The following patterns are _not_ considered violations: | ||
|
||
```scss | ||
a { | ||
color: unquote("blue"); | ||
} | ||
``` | ||
|
||
```scss | ||
$font: "Helvetica"; | ||
p { | ||
font-family: unquote($font); | ||
} | ||
``` |
64 changes: 64 additions & 0 deletions
64
src/rules/function-unquote-no-unquoted-strings-inside/__tests__/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import rule, { ruleName, messages } from ".."; | ||
|
||
// always-intermediate | ||
testRule(rule, { | ||
ruleName, | ||
config: [true], | ||
syntax: "scss", | ||
fix: true, | ||
|
||
accept: [ | ||
{ | ||
code: ` | ||
p { | ||
font-family: unquote("Helvetica"); | ||
} | ||
`, | ||
description: "accepts strings with quotes" | ||
}, | ||
{ | ||
code: ` | ||
$font: "Helvetica"; | ||
p { | ||
font-family: unquote($font); | ||
} | ||
`, | ||
description: "accepts variables representing strings that are quoted." | ||
} | ||
], | ||
|
||
reject: [ | ||
{ | ||
code: ` | ||
p { | ||
font-family: unquote(Helvetica); | ||
} | ||
`, | ||
description: "does not accept strings without quotes", | ||
message: messages.rejected, | ||
line: 3, | ||
fixed: ` | ||
p { | ||
font-family: Helvetica; | ||
} | ||
` | ||
}, | ||
{ | ||
code: ` | ||
$font: Helvetica; | ||
p { | ||
font-family: unquote($font); | ||
} | ||
`, | ||
description: | ||
"does not accept variables representing strings that are quoted.", | ||
line: 4, | ||
fixed: ` | ||
$font: Helvetica; | ||
p { | ||
font-family: $font; | ||
} | ||
` | ||
} | ||
] | ||
}); |
76 changes: 76 additions & 0 deletions
76
src/rules/function-unquote-no-unquoted-strings-inside/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { utils } from "stylelint"; | ||
import { namespace, isNativeCssFunction } from "../../utils"; | ||
import valueParser from "postcss-value-parser"; | ||
|
||
export const ruleName = namespace( | ||
"function-unquote-no-unquoted-strings-inside" | ||
); | ||
|
||
export const messages = utils.ruleMessages(ruleName, { | ||
rejected: "Unquote function used with an already-unquoted string" | ||
}); | ||
|
||
function rule(primary, _, context) { | ||
return (root, result) => { | ||
const validOptions = utils.validateOptions(result, ruleName, { | ||
actual: primary | ||
}); | ||
|
||
if (!validOptions) { | ||
return; | ||
} | ||
|
||
// Setup variable naming. | ||
const vars = {}; | ||
|
||
root.walkDecls(decl => { | ||
if (decl.prop[0] !== "$") { | ||
return; | ||
} | ||
|
||
valueParser(decl.value).walk(node => { | ||
vars[decl.prop] = node.type; | ||
}); | ||
}); | ||
|
||
root.walkDecls(decl => { | ||
valueParser(decl.value).walk(node => { | ||
// Verify that we're only looking at functions. | ||
if ( | ||
node.type !== "function" || | ||
isNativeCssFunction(node.value) || | ||
node.value === "" | ||
) { | ||
return; | ||
} | ||
|
||
// Verify we're only looking at quote() calls. | ||
if (node.value !== "unquote") { | ||
return; | ||
} | ||
|
||
// Report error if first character is a quote. | ||
// postcss-value-parser represents quoted strings as type 'string' (as opposed to word) | ||
if ( | ||
(!node.nodes[0].quote && node.nodes[0].value[0] !== "$") || | ||
vars[node.nodes[0].value] === "word" | ||
) { | ||
if (context.fix) { | ||
const contents = /unquote\((.*)\)/.exec(decl.value); | ||
|
||
decl.value = contents[1]; | ||
} else { | ||
utils.report({ | ||
message: messages.rejected, | ||
node: decl, | ||
result, | ||
ruleName | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
}; | ||
} | ||
|
||
export default rule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters