From 05b857aa62ad75895c3cfb5b056443ee4a36dbb0 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Fri, 9 Oct 2020 07:32:00 -0700 Subject: [PATCH] Return a list of files that were read from loadPartialConfig (#11907) --- .../babel-core/src/config/config-chain.js | 50 +++++++++++++--- packages/babel-core/src/config/full.js | 6 +- packages/babel-core/src/config/partial.js | 36 +++++++++-- packages/babel-core/test/config-chain.js | 59 +++++++++++++++++++ .../config-files/babelrc-extended/.babelrc | 3 + .../babelrc-extended/.babelrc-extended | 3 + 6 files changed, 141 insertions(+), 16 deletions(-) create mode 100644 packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc create mode 100644 packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc-extended diff --git a/packages/babel-core/src/config/config-chain.js b/packages/babel-core/src/config/config-chain.js index 2c897139fb3d..350111a1a41b 100644 --- a/packages/babel-core/src/config/config-chain.js +++ b/packages/babel-core/src/config/config-chain.js @@ -40,6 +40,7 @@ export type ConfigChain = { plugins: Array, presets: Array, options: Array, + files: Set, }; export type PresetInstance = { @@ -71,6 +72,7 @@ export function* buildPresetChain( plugins: dedupDescriptors(chain.plugins), presets: dedupDescriptors(chain.presets), options: chain.options.map(o => normalizeOptions(o)), + files: new Set(), }; } @@ -124,10 +126,13 @@ const loadPresetOverridesEnvDescriptors = makeWeakCacheSync( ), ); +export type FileHandling = "transpile" | "ignored" | "unsupported"; export type RootConfigChain = ConfigChain & { babelrc: ConfigFile | void, config: ConfigFile | void, ignore: IgnoreFile | void, + fileHandling: FileHandling, + files: Set, }; /** @@ -202,6 +207,7 @@ export function* buildRootChain( : null; let ignoreFile, babelrcFile; + let isIgnored = false; const fileChain = emptyChain(); // resolve all .babelrc files if ( @@ -215,14 +221,18 @@ export function* buildRootChain( context.caller, )); + if (ignoreFile) { + fileChain.files.add(ignoreFile.filepath); + } + if ( ignoreFile && shouldIgnore(context, ignoreFile.ignore, null, ignoreFile.dirname) ) { - return null; + isIgnored = true; } - if (babelrcFile) { + if (babelrcFile && !isIgnored) { const validatedFile = validateBabelrcFile(babelrcFile); const babelrcLogger = new ConfigPrinter(); const result = yield* loadFileChain( @@ -231,10 +241,16 @@ export function* buildRootChain( undefined, babelrcLogger, ); - if (!result) return null; - babelRcReport = babelrcLogger.output(); + if (!result) { + isIgnored = true; + } else { + babelRcReport = babelrcLogger.output(); + mergeChain(fileChain, result); + } + } - mergeChain(fileChain, result); + if (babelrcFile && isIgnored) { + fileChain.files.add(babelrcFile.filepath); } } @@ -257,12 +273,14 @@ export function* buildRootChain( ); return { - plugins: dedupDescriptors(chain.plugins), - presets: dedupDescriptors(chain.presets), - options: chain.options.map(o => normalizeOptions(o)), + plugins: isIgnored ? [] : dedupDescriptors(chain.plugins), + presets: isIgnored ? [] : dedupDescriptors(chain.presets), + options: isIgnored ? [] : chain.options.map(o => normalizeOptions(o)), + fileHandling: isIgnored ? "ignored" : "transpile", ignore: ignoreFile || undefined, babelrc: babelrcFile || undefined, config: configFile || undefined, + files: chain.files, }; } @@ -355,7 +373,7 @@ const loadProgrammaticChain = makeChainWalker({ /** * Build a config chain for a given file. */ -const loadFileChain = makeChainWalker({ +const loadFileChainWalker = makeChainWalker({ root: file => loadFileDescriptors(file), env: (file, envName) => loadFileEnvDescriptors(file)(envName), overrides: (file, index) => loadFileOverridesDescriptors(file)(index), @@ -364,6 +382,16 @@ const loadFileChain = makeChainWalker({ createLogger: (file, context, baseLogger) => buildFileLogger(file.filepath, context, baseLogger), }); + +function* loadFileChain(input, context, files, baseLogger) { + const chain = yield* loadFileChainWalker(input, context, files, baseLogger); + if (chain) { + chain.files.add(input.filepath); + } + + return chain; +} + const loadFileDescriptors = makeWeakCacheSync((file: ValidatedFile) => buildRootDescriptors(file, file.filepath, createUncachedDescriptors), ); @@ -622,6 +650,9 @@ function mergeChain(target: ConfigChain, source: ConfigChain): ConfigChain { target.options.push(...source.options); target.plugins.push(...source.plugins); target.presets.push(...source.presets); + for (const file of source.files) { + target.files.add(file); + } return target; } @@ -642,6 +673,7 @@ function emptyChain(): ConfigChain { options: [], presets: [], plugins: [], + files: new Set(), }; } diff --git a/packages/babel-core/src/config/full.js b/packages/babel-core/src/config/full.js index 04fa5b599083..b13b332f676e 100644 --- a/packages/babel-core/src/config/full.js +++ b/packages/babel-core/src/config/full.js @@ -63,7 +63,11 @@ export default gensync<[any], ResolvedConfig | null>(function* loadFullConfig( if (!result) { return null; } - const { options, context } = result; + const { options, context, fileHandling } = result; + + if (fileHandling === "ignored") { + return null; + } const optionDefaults = {}; const passes: Array> = [[]]; diff --git a/packages/babel-core/src/config/partial.js b/packages/babel-core/src/config/partial.js index c0308b6fd507..e69c9cd4c453 100644 --- a/packages/babel-core/src/config/partial.js +++ b/packages/babel-core/src/config/partial.js @@ -5,7 +5,11 @@ import gensync, { type Handler } from "gensync"; import Plugin from "./plugin"; import { mergeOptions } from "./util"; import { createItemFromDescriptor } from "./item"; -import { buildRootChain, type ConfigContext } from "./config-chain"; +import { + buildRootChain, + type ConfigContext, + type FileHandling, +} from "./config-chain"; import { getEnv } from "./helpers/environment"; import { validate, @@ -59,9 +63,11 @@ function* resolveRootMode( type PrivPartialConfig = { options: ValidatedOptions, context: ConfigContext, + fileHandling: FileHandling, ignore: IgnoreFile | void, babelrc: ConfigFile | void, config: ConfigFile | void, + files: Set, }; export default function* loadPrivatePartialConfig( @@ -137,20 +143,30 @@ export default function* loadPrivatePartialConfig( return { options, context, + fileHandling: configChain.fileHandling, ignore: configChain.ignore, babelrc: configChain.babelrc, config: configChain.config, + files: configChain.files, }; } +type LoadPartialConfigOpts = { + showIgnoredFiles?: boolean, + ... +}; + export const loadPartialConfig = gensync<[any], PartialConfig | null>( - function* (inputOpts: mixed): Handler { - const result: ?PrivPartialConfig = yield* loadPrivatePartialConfig( - inputOpts, - ); + function* (inputOpts: LoadPartialConfigOpts): Handler { + const { showIgnoredFiles, ...opts } = inputOpts; + const result: ?PrivPartialConfig = yield* loadPrivatePartialConfig(opts); if (!result) return null; - const { options, babelrc, ignore, config } = result; + const { options, babelrc, ignore, config, fileHandling, files } = result; + + if (fileHandling === "ignored" && !showIgnoredFiles) { + return null; + } (options.plugins || []).forEach(item => { if (item.value instanceof Plugin) { @@ -166,6 +182,8 @@ export const loadPartialConfig = gensync<[any], PartialConfig | null>( babelrc ? babelrc.filepath : undefined, ignore ? ignore.filepath : undefined, config ? config.filepath : undefined, + fileHandling, + files, ); }, ); @@ -181,17 +199,23 @@ class PartialConfig { babelrc: string | void; babelignore: string | void; config: string | void; + fileHandling: FileHandling; + files: Set; constructor( options: ValidatedOptions, babelrc: string | void, ignore: string | void, config: string | void, + fileHandling: FileHandling, + files: Set, ) { this.options = options; this.babelignore = ignore; this.babelrc = babelrc; this.config = config; + this.fileHandling = fileHandling; + this.files = files; // Freeze since this is a public API and it should be extremely obvious that // reassigning properties on here does nothing. diff --git a/packages/babel-core/test/config-chain.js b/packages/babel-core/test/config-chain.js index 2057d996318b..911e813734fd 100644 --- a/packages/babel-core/test/config-chain.js +++ b/packages/babel-core/test/config-chain.js @@ -1271,6 +1271,65 @@ describe("buildConfigChain", function () { loadOptionsAsync({ filename, cwd: path.dirname(filename) }), ).rejects.toThrow(error); }); + + it("loadPartialConfig should return a list of files that were extended", () => { + const filename = fixture("config-files", "babelrc-extended", "src.js"); + + expect( + babel.loadPartialConfig({ filename, cwd: path.dirname(filename) }), + ).toEqual({ + babelignore: fixture("config-files", ".babelignore"), + babelrc: fixture("config-files", "babelrc-extended", ".babelrc"), + config: undefined, + fileHandling: "transpile", + options: { + ...getDefaults(), + filename: filename, + cwd: path.dirname(filename), + root: path.dirname(filename), + comments: true, + }, + files: new Set([ + fixture("config-files", ".babelignore"), + fixture("config-files", "babelrc-extended", ".babelrc-extended"), + fixture("config-files", "babelrc-extended", ".babelrc"), + ]), + }); + }); + + it("loadPartialConfig should return null when ignored", () => { + const filename = fixture("config-files", "babelignore", "src.js"); + + expect( + babel.loadPartialConfig({ filename, cwd: path.dirname(filename) }), + ).toBeNull(); + }); + + it("loadPartialConfig should return a list of files when ignored with showIgnoredFiles option", () => { + const filename = fixture("config-files", "babelignore", "src.js"); + + expect( + babel.loadPartialConfig({ + filename, + cwd: path.dirname(filename), + showIgnoredFiles: true, + }), + ).toEqual({ + babelignore: fixture("config-files", "babelignore", ".babelignore"), + babelrc: undefined, + config: undefined, + fileHandling: "ignored", + options: { + ...getDefaults(), + filename: filename, + cwd: path.dirname(filename), + root: path.dirname(filename), + }, + files: new Set([ + fixture("config-files", "babelignore", ".babelignore"), + ]), + }); + }); }); it("should throw when `test` presents but `filename` is not passed", () => { diff --git a/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc b/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc new file mode 100644 index 000000000000..c1a79810d195 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc @@ -0,0 +1,3 @@ +{ + "extends": "./.babelrc-extended" +} diff --git a/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc-extended b/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc-extended new file mode 100644 index 000000000000..b445e5d66381 --- /dev/null +++ b/packages/babel-core/test/fixtures/config/config-files/babelrc-extended/.babelrc-extended @@ -0,0 +1,3 @@ +{ + "comments": true +}