| 1 | import rule from "../../lib/rules/no-undefined-identifier.cjs"; |
| 2 | import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester.js"; |
| 3 | |
| 4 | const error = |
| 5 | "Use path.scope.buildUndefinedNode() to create an undefined identifier directly."; |
| 6 | |
| 7 | const ruleTester = new RuleTester({ |
| 8 | parserOptions: { sourceType: "module" }, |
| 9 | }); |
| 10 | |
| 11 | ruleTester.run("no-undefined-identifier", rule, { |
| 12 | valid: [ |
| 13 | `_.identifier("undefined")`, |
| 14 | `_.Identifier("undefined")`, |
| 15 | `import * as t from "lib"; t.identifier("undefined");`, |
| 16 | `function f(_) { _.types.identifier("undefined"); }`, |
| 17 | `import * as t from "@babel/types"; t.identifier("not_undefined");`, |
| 18 | `path.scope.buildUndefinedNode();`, |
| 19 | ], |
| 20 | invalid: [ |
| 21 | { |
| 22 | code: `import { identifier } from "@babel/types"; identifier("undefined");`, |
| 23 | errors: [error], |
| 24 | }, |
| 25 | { |
| 26 | code: `import { Identifier } from "@babel/types"; Identifier("undefined");`, |
| 27 | errors: [error], |
| 28 | }, |
| 29 | { |
| 30 | code: `import * as t from "@babel/types"; t.identifier("undefined");`, |
| 31 | errors: [error], |
| 32 | }, |
| 33 | { |
| 34 | code: `import * as t from "@babel/types"; var { identifier } = t; identifier("undefined");`, |
| 35 | errors: [error], |
| 36 | }, |
| 37 | { |
| 38 | code: `import { identifier as id } from "@babel/types"; id("undefined");`, |
| 39 | errors: [error], |
| 40 | }, |
| 41 | { |
| 42 | code: `import * as babel from "@babel/core"; babel.types.identifier("undefined");`, |
| 43 | errors: [error], |
| 44 | }, |
| 45 | { |
| 46 | code: `import { types } from "@babel/core"; types.identifier("undefined");`, |
| 47 | errors: [error], |
| 48 | }, |
| 49 | { |
| 50 | code: `import { types as t } from "@babel/core"; t.identifier("undefined");`, |
| 51 | errors: [error], |
| 52 | }, |
| 53 | { |
| 54 | code: `export default function plugin(babel) { babel.types.identifier("undefined") }`, |
| 55 | errors: [error], |
| 56 | }, |
| 57 | { |
| 58 | code: `export default function plugin({ types }) { types.identifier("undefined") }`, |
| 59 | errors: [error], |
| 60 | }, |
| 61 | { |
| 62 | code: `export default function plugin({ types: t }) { t.identifier("undefined") }`, |
| 63 | errors: [error], |
| 64 | }, |
| 65 | { |
| 66 | code: `export default ({ types }) => { types.identifier("undefined") }`, |
| 67 | errors: [error], |
| 68 | }, |
| 69 | { |
| 70 | code: `module.exports = function plugin({ types }) { types.identifier("undefined") }`, |
| 71 | errors: [error], |
| 72 | }, |
| 73 | { |
| 74 | code: `import { declare } from "@babel/helper-plugin-utils"; declare(({ types }) => { types.identifier("undefined") });`, |
| 75 | errors: [error], |
| 76 | }, |
| 77 | ], |
| 78 | }); |