|
| 1 | +import path from "node:path"; |
| 2 | +import { parseForESLint } from "../lib/index.cjs"; |
| 3 | +import { ESLint } from "eslint"; |
| 4 | +import { itDummy, commonJS } from "$repo-utils"; |
| 5 | + |
| 6 | +const ESLINT_VERSION = ESLint.version; |
| 7 | +const isESLint9 = ESLINT_VERSION.startsWith("9."); |
| 8 | +const { require } = commonJS(import.meta.url); |
| 9 | + |
| 10 | +const PROPS_TO_REMOVE = [ |
| 11 | + // typescript-eslint generates start/end in the loc object, while Babel generate both start/end and loc |
| 12 | + { key: "start", type: "Node" }, |
| 13 | + { key: "end", type: "Node" }, |
| 14 | + |
| 15 | + { key: "importKind", type: "Node" }, |
| 16 | + { key: "exportKind", type: "Node" }, |
| 17 | + { key: "variance", type: "Node" }, |
| 18 | + { key: "typeArguments", type: "Node" }, |
| 19 | + { key: "filename", type: null }, |
| 20 | + { key: "identifierName", type: null }, |
| 21 | + // For legacy estree AST |
| 22 | + { key: "attributes", type: "ImportExpression" }, |
| 23 | +]; |
| 24 | + |
| 25 | +function deeplyRemoveProperties(obj, props) { |
| 26 | + for (const [k, v] of Object.entries(obj)) { |
| 27 | + if ( |
| 28 | + props.some( |
| 29 | + ({ key, type }) => |
| 30 | + key === k && |
| 31 | + ((type === "Node" && obj.type) || type === obj.type || type == null), |
| 32 | + ) |
| 33 | + ) { |
| 34 | + delete obj[k]; |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + if (typeof v === "object") { |
| 39 | + if (Array.isArray(v)) { |
| 40 | + for (const el of v) { |
| 41 | + if (el != null) { |
| 42 | + deeplyRemoveProperties(el, props); |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (v != null) { |
| 48 | + deeplyRemoveProperties(v, props); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// Only ESLint 9 or above will be tested |
| 55 | +(isESLint9 ? describe : describe.skip)( |
| 56 | + "Babel should output the same AST as TypeScript-Estree", |
| 57 | + () => { |
| 58 | + /** |
| 59 | + * @type {import("@typescript-eslint/typescript-estree")} |
| 60 | + */ |
| 61 | + let tsEstree; |
| 62 | + |
| 63 | + /** |
| 64 | + * @type {import("@typescript-eslint/typescript-estree").TSESTreeOptions} |
| 65 | + */ |
| 66 | + const tsEstreeOptions = { |
| 67 | + filePath: "typescript-estree.test.js-test-input.tsx", |
| 68 | + jsx: true, |
| 69 | + tokens: true, |
| 70 | + loc: true, |
| 71 | + range: true, |
| 72 | + comment: true, |
| 73 | + sourceType: "module", |
| 74 | + }; |
| 75 | + |
| 76 | + function parseAndAssertSame(code, babelEcmaFeatures = null) { |
| 77 | + const tsEstreeAST = tsEstree.parse(code, tsEstreeOptions); |
| 78 | + const babelAST = parseForESLint(code, { |
| 79 | + eslintVisitorKeys: true, |
| 80 | + eslintScopeManager: true, |
| 81 | + ecmaFeatures: babelEcmaFeatures, |
| 82 | + requireConfigFile: false, |
| 83 | + babelOptions: { |
| 84 | + configFile: false, |
| 85 | + parserOpts: { |
| 86 | + plugins: ["jsx", "typescript"], |
| 87 | + }, |
| 88 | + }, |
| 89 | + }).ast; |
| 90 | + |
| 91 | + deeplyRemoveProperties(babelAST, PROPS_TO_REMOVE); |
| 92 | + expect(babelAST).toEqual(tsEstreeAST); |
| 93 | + } |
| 94 | + |
| 95 | + beforeAll(() => { |
| 96 | + // Use the version of TS-Espree that is a dependency of |
| 97 | + // the version of typescript-eslint we are testing against. |
| 98 | + const tsEstreePath = require.resolve( |
| 99 | + "@typescript-eslint/typescript-estree", |
| 100 | + { |
| 101 | + paths: [path.dirname(require.resolve("typescript-eslint"))], |
| 102 | + }, |
| 103 | + ); |
| 104 | + |
| 105 | + tsEstree = require(tsEstreePath); |
| 106 | + }); |
| 107 | + |
| 108 | + it.each([ |
| 109 | + ["empty", ""], |
| 110 | + |
| 111 | + ["boolean", "true"], |
| 112 | + ["boolean", "false"], |
| 113 | + |
| 114 | + ["numeric", "0"], |
| 115 | + // ["bigint", "0n"], |
| 116 | + |
| 117 | + ["regexp without flag", `/foo/;`], |
| 118 | + ["regexp u flag", `/foo/dgimsuy;`], |
| 119 | + ["regexp v flag", `/foo/dgimsvy;`], |
| 120 | + |
| 121 | + ["logical NOT", `!0`], |
| 122 | + ["bitwise NOT", `~0`], |
| 123 | + ])("%s: %s", (_, input) => { |
| 124 | + parseAndAssertSame(input); |
| 125 | + }); |
| 126 | + }, |
| 127 | +); |
| 128 | + |
| 129 | +(isESLint9 ? describe.skip : describe)( |
| 130 | + "typescript-estree test for older ESLint versions", |
| 131 | + () => { |
| 132 | + itDummy("is skipped", () => {}); |
| 133 | + }, |
| 134 | +); |
0 commit comments