Skip to content

Commit

Permalink
Allow method calls of import (fixes #135)
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsk committed Oct 1, 2020
1 parent 347d61a commit 62209dd
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
6 changes: 6 additions & 0 deletions lib/ruleHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ RuleHelper.prototype = {
const normalizedMethodCall = this.normalizeMethodCall(node.callee);
let matched = false;

// Allow methods named "import":
if (normalizedMethodCall.methodName == "import"
&& node.callee && node.callee.type == "MemberExpression") {
return false;
}

// If objectMatches isn't present we should match all
if (!objectMatches) {
return true;
Expand Down
60 changes: 59 additions & 1 deletion tests/rules/method.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,27 @@ eslintTester.run("method", rule, {
]
},

// Issue 135: Check literal imports in all parsers:
{
code: "import('lodash')",
parserOptions: { ecmaVersion: 2020 },
},

// Issue 83: Support import() expressions as parsed by babel-eslint
{
code: "import('lodash')",
parser: PATH_TO_BABEL_ESLINT
},

// Issue 135: Check literal imports in all parsers:
{
code: "import('lodash')",
parser: PATH_TO_TYPESCRIPT_ESLINT,
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
},
},
{ // issue 108: adding tests for custom escaper
code: "range.createContextualFragment(templateEscaper`<em>${evil}</em>`);",
parserOptions: { ecmaVersion: 6 },
Expand Down Expand Up @@ -246,6 +262,24 @@ eslintTester.run("method", rule, {
sourceType: "module",
}
},

// Issue 135: method calls to import should not warn.
{
code: "foo.import(bar)",
parserOptions: { ecmaVersion: 2020 },
},
{
code: "foo.import(bar)",
parser: PATH_TO_BABEL_ESLINT,
},
{
code: "foo.import(bar)",
parser: PATH_TO_TYPESCRIPT_ESLINT,
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
}
},
],

// Examples of code that should trigger the rule
Expand Down Expand Up @@ -404,7 +438,17 @@ eslintTester.run("method", rule, {
]
},

// Issue NN: Disallow import() with non-literal params
// Issue 135: Disallow import() with non-literal params
{
code: "import(foo)",
parserOptions: { ecmaVersion: 2020 },
errors: [
{
message: "Unsafe call to import for argument 0",
type: "ImportExpression"
}
]
},
{
code: "import(foo)",
parser: PATH_TO_BABEL_ESLINT,
Expand All @@ -415,6 +459,20 @@ eslintTester.run("method", rule, {
}
]
},
{
code: "import(foo)",
parser: PATH_TO_TYPESCRIPT_ESLINT,
parserOptions: {
ecmaVersion: 2021,
sourceType: "module",
},
errors: [
{
message: "Unsafe call to import for argument 0",
type: "ImportExpression"
}
]
},
{ // basic support for SequenceExpressions, which always return the last item - fixes #113
code: "(0, node.insertAdjacentHTML)('beforebegin', evil);",
parserOptions: { ecmaVersion: 6 },
Expand Down

0 comments on commit 62209dd

Please sign in to comment.