Skip to content

Commit

Permalink
✨ add es/no-import-meta rule (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Jun 19, 2020
1 parent 062b723 commit 52ce781
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ There is no config which enables all rules in this category.
| [es/no-bigint](./no-bigint.md) | disallow `bigint` syntax and built-ins. | |
| [es/no-dynamic-import](./no-dynamic-import.md) | disallow `import()` syntax. | |
| [es/no-global-this](./no-global-this.md) | disallow the `globalThis` variable. | |
| [es/no-import-meta](./no-import-meta.md) | disallow `new.target` meta property. | |
| [es/no-nullish-coalescing-operators](./no-nullish-coalescing-operators.md) | disallow nullish coalescing operators. | |
| [es/no-promise-all-settled](./no-promise-all-settled.md) | disallow `Promise.allSettled` function. | |

Expand Down
16 changes: 16 additions & 0 deletions docs/rules/no-import-meta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# disallow `import.meta` meta property (es/no-import-meta)

This rule reports ES2020 [import.meta](https://github.com/tc39/proposal-import-meta) as errors.

## Examples

⛔ Examples of **incorrect** code for this rule:

<eslint-playground type="bad" code="/*eslint es/no-import-meta: error */
import.meta
" />

## 📚 References

- [Rule source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/lib/rules/no-import-meta.js)
- [Test source](https://github.com/mysticatea/eslint-plugin-es/blob/v3.0.1/tests/lib/rules/no-import-meta.js)
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ module.exports = {
"no-for-of-loops": require("./rules/no-for-of-loops"),
"no-generators": require("./rules/no-generators"),
"no-global-this": require("./rules/no-global-this"),
"no-import-meta": require("./rules/no-import-meta"),
"no-json-superset": require("./rules/no-json-superset"),
"no-json": require("./rules/no-json"),
"no-keyword-properties": require("./rules/no-keyword-properties"),
Expand Down
30 changes: 30 additions & 0 deletions lib/rules/no-import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
"use strict"

module.exports = {
meta: {
docs: {
description: "disallow `import.meta` meta property.",
category: "ES2020",
recommended: false,
url:
"http://mysticatea.github.io/eslint-plugin-es/rules/no-import-meta.html",
},
fixable: null,
messages: {
forbidden: "ES2020 'import.meta' meta property is forbidden.",
},
schema: [],
type: "problem",
},
create(context) {
return {
"MetaProperty[meta.name='import'][property.name='meta']"(node) {
context.report({ node, messageId: "forbidden" })
},
}
},
}
26 changes: 26 additions & 0 deletions tests/lib/rules/no-import-meta.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @author Yosuke Ota <https://github.com/ota-meshi>
* See LICENSE file in root directory for full license.
*/
"use strict"

const RuleTester = require("../../tester")
const rule = require("../../../lib/rules/no-import-meta.js")

if (!RuleTester.isSupported(2020)) {
//eslint-disable-next-line no-console
console.log("Skip the tests of no-import-meta.")
return
}

new RuleTester({
parserOptions: { sourceType: "module" },
}).run("no-import-meta", rule, {
valid: ["import * as Foo from 'foo'", "import('foo')"],
invalid: [
{
code: "import.meta",
errors: ["ES2020 'import.meta' meta property is forbidden."],
},
],
})

0 comments on commit 52ce781

Please sign in to comment.