Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce
_foo.default || _foo
to import lib files in tests (#14673)
- Loading branch information
1 parent
a32306d
commit f0ec180
Showing
50 changed files
with
154 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 2 additions & 3 deletions
5
eslint/babel-eslint-plugin-development-internal/src/index.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...nt/babel-eslint-plugin-development-internal/src/rules/require-default-import-fallback.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module.exports = { | ||
meta: { | ||
type: "suggestion", | ||
docs: { | ||
description: "", | ||
}, | ||
fixable: "code", | ||
}, | ||
create(ctx) { | ||
return { | ||
ImportDeclaration(node) { | ||
const filename = ctx.getFilename(); | ||
const src = node.source.value; | ||
if ( | ||
// Not a source file in the monorepo | ||
(!src.includes("/lib/") && !src.startsWith("@babel/")) || | ||
// This package already always uses the ESM entrypoint. | ||
src === "@babel/helper-plugin-test-runner" || | ||
// Unclear if we'll convert this or it will stay as JSON. | ||
src.startsWith("@babel/compat-data") || | ||
// This will not become an ESM file. | ||
src.endsWith(".cjs") || | ||
// @babel/core automatically unwraps .default from plugins/presets. | ||
/^@babel\/(plugin|preset)-/.test(src) || | ||
(src.endsWith("./lib/index.js") && | ||
/babel-(plugin|preset)-/.test(filename)) | ||
) { | ||
return; | ||
} | ||
|
||
const defaultSpecifier = node.specifiers.find( | ||
spec => spec.type === "ImportDefaultSpecifier", | ||
); | ||
if (!defaultSpecifier) return; | ||
|
||
const scope = ctx.getScope(); | ||
|
||
const { name: local } = defaultSpecifier.local; | ||
const { references } = scope.variables.find(v => v.name === local); | ||
|
||
const isRef = node => node.type === "Identifier" && node.name === local; | ||
const isRefDefault = node => | ||
node.type === "MemberExpression" && | ||
!node.computed && | ||
node.property.name === "default" && | ||
isRef(node.object); | ||
|
||
for (const { identifier } of references) { | ||
const base = | ||
identifier.parent.type === "MemberExpression" | ||
? identifier.parent.parent | ||
: identifier.parent; | ||
|
||
if ( | ||
base.type !== "LogicalExpression" || | ||
base.operator !== "||" || | ||
!isRefDefault(base.left) || | ||
!isRef(base.right) | ||
) { | ||
const expected = `${local}.default || ${local}`; | ||
ctx.report({ | ||
node: identifier, | ||
message: `Default imports of source files must be used with an explicit fallback: \`${expected}\``, | ||
fix(fixer) { | ||
const base = isRefDefault(identifier.parent) | ||
? identifier.parent | ||
: identifier; | ||
return fixer.replaceText(base, `(${expected})`); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import _cjs from "./lib/index.js"; | ||
const cjs = _cjs.default || _cjs; | ||
|
||
const adapter = _cjs.default.bind(); | ||
const adapter = cjs.bind(); | ||
|
||
// For backward compatibility with the CJS-only version, this makes | ||
// import _x from "@babel/helper-plugin-test-runner" | ||
// const x = _x.default | ||
// still work. | ||
adapter.default = _cjs.default; | ||
adapter.default = cjs; | ||
|
||
export default adapter; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
packages/babel-preset-env/test/get-option-specific-excludes.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.