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

Update: check template literal in no-script-url #13775

Merged
merged 5 commits into from Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions docs/rules/no-script-url.md
Expand Up @@ -10,6 +10,8 @@ Examples of **incorrect** code for this rule:
/*eslint no-script-url: "error"*/

location.href = "javascript:void(0)";

location.href = `javascript:void(0)`;
```

## Compatibility
Expand Down
28 changes: 19 additions & 9 deletions lib/rules/no-script-url.js
Expand Up @@ -7,6 +7,8 @@

"use strict";

const astUtils = require("./utils/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand All @@ -31,18 +33,26 @@ module.exports = {

create(context) {

/**
* Check whether a node's static value starts with "javascript:" or not.
* And report an error for unexpected script URL.
* @param {ASTNode} node node to check
* @returns {void}
*/
function check(node) {
const value = astUtils.getStaticStringValue(node);

if (typeof value === "string" && value.toLocaleLowerCase().indexOf("javascript:") === 0) {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
context.report({ node, messageId: "unexpectedScriptURL" });
}
}
return {

Literal(node) {
if (node.value && typeof node.value === "string") {
const value = node.value.toLowerCase();
yeonjuan marked this conversation as resolved.
Show resolved Hide resolved

if (value.indexOf("javascript:") === 0) {
context.report({ node, messageId: "unexpectedScriptURL" });
}
Literal: check,
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
TemplateLiteral(node) {
if (node.parent && node.parent.type !== "TaggedTemplateExpression") {
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
check(node);
}
}
};

}
};
28 changes: 27 additions & 1 deletion tests/lib/rules/no-script-url.js
Expand Up @@ -22,7 +22,19 @@ ruleTester.run("no-script-url", rule, {
valid: [
"var a = 'Hello World!';",
"var a = 10;",
"var url = 'xjavascript:'"
"var url = 'xjavascript:'",
{
code: "var url = `xjavascript:`",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var url = `${foo}javascript:`",
parserOptions: { ecmaVersion: 6 }
},
{
code: "var a = foo`javaScript:`;",
parserOptions: { ecmaVersion: 6 }
}
],
invalid: [
{
Expand All @@ -36,6 +48,20 @@ ruleTester.run("no-script-url", rule, {
errors: [
{ messageId: "unexpectedScriptURL", type: "Literal" }
]
},
{
code: "var a = `javascript:`;",
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "unexpectedScriptURL", type: "TemplateLiteral" }
]
},
{
code: "var a = `JavaScript:`;",
parserOptions: { ecmaVersion: 6 },
errors: [
{ messageId: "unexpectedScriptURL", type: "TemplateLiteral" }
]
}
]
});