Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typecheck much more of the config loading process #5642

Merged
merged 3 commits into from
Apr 18, 2017

Conversation

loganfsmyth
Copy link
Member

Q A
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Deprecations?
Spec Compliancy?
Tests Added/Pass?
Fixed Tickets
License MIT
Doc PR
Dependency Changes

Just adding a bunch of Flowtype annotations to the config folder files. With this, everything in babel-core/src/config has an @flow declaration.

There are still a few places typed generic Object, but this gets rid of most of them except for the actual result options object, which has not changed and is only partially validated.

@mention-bot
Copy link

@loganfsmyth, thanks for your PR! By analyzing the history of the files in this pull request, we identified @jamestalmage, @existentialism and @forivall to be potential reviewers.

@codecov
Copy link

codecov bot commented Apr 18, 2017

Codecov Report

Merging #5642 into 7.0 will decrease coverage by 0.14%.
The diff coverage is 57.29%.

Impacted file tree graph

@@            Coverage Diff             @@
##              7.0    #5642      +/-   ##
==========================================
- Coverage   84.51%   84.36%   -0.15%     
==========================================
  Files         285      285              
  Lines        9737     9761      +24     
  Branches     2723     2737      +14     
==========================================
+ Hits         8229     8235       +6     
- Misses       1003     1010       +7     
- Partials      505      516      +11
Impacted Files Coverage Δ
...bel-core/src/config/loading/files/configuration.js 88.75% <ø> (ø) ⬆️
...bel-core/src/config/loading/files/index-browser.js 0% <ø> (ø) ⬆️
packages/babel-core/src/config/caching.js 95.23% <ø> (ø) ⬆️
...kages/babel-core/src/config/helpers/environment.js 100% <100%> (ø) ⬆️
packages/babel-core/src/config/helpers/merge.js 75% <100%> (ø) ⬆️
packages/babel-core/src/config/index.js 50% <50%> (-50%) ⬇️
...ckages/babel-core/src/config/build-config-chain.js 74.07% <61.7%> (-5.93%) ⬇️
packages/babel-core/src/config/option-manager.js 76.19% <64.51%> (-1.59%) ⬇️
packages/babel-core/src/config/plugin.js 37.5% <9.09%> (-62.5%) ⬇️
...bel-plugin-transform-es2015-classes/src/vanilla.js 90.17% <0%> (+0.42%) ⬆️
... and 3 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d1d812e...71b84db. Read the comment docs.

Copy link
Member

@xtuc xtuc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM, nice work @loganfsmyth!

I'm wondering if we could just all=true in flow config to enable type check on all files.

if (options.env) {
const envOpts = options.env[envKey];
delete options.env;
const envKey = getEnv();
Copy link
Member

@xtuc xtuc Apr 18, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the comment above is not useful, do you mind removing it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call.


if (rawOpts.env != null && (typeof rawOpts.env !== "object" || Array.isArray(rawOpts.env))) {
throw new Error(".env block must be an object, null, or undefined");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should use a helper function for this kind of error messages.
We can also clarify a bit the message:

The `env` key in your Babel configuration must be ..., ... given

And maybe link to the website?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah possibly. I've got more work planned to give more information about the errors, but that's a few PRs from now.

@@ -12,11 +14,11 @@ import clone from "lodash/clone";
import { loadPlugin, loadPreset, loadParser, loadGenerator } from "./loading/files";

type MergeOptions = {
type: "arguments"|"options"|"preset",
options?: Object,
+type: "arguments"|"options"|"preset",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for my information, what is the + sign for?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is property covariance. In this case it essentially allows two types that are mostly-compatible to play nicely together. Take this case:

type ConfigItem = {
  type: "options"|"arguments",
};

type MergeOptions = {
  type: "arguments"|"options"|"preset",
};

var item: ConfigItem = {
  type: "options",
};

var merge: MergeOptions = item;

This will fail, because merge.type = "preset"; would cause item to no longer match its type properly, since ConfigItem objects cant be have type preset.

By adding the +, you are basically saying "don't let me reassign this", so

var item: ConfigItem = {
  type: "options",
};

var merge: MergeOptions = item;

will work, but it will throw an error if you try to reassign merge.type.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks for the explanation

const options = normalizeOptions(config);

if (config.options.plugins != null && !Array.isArray(config.options.plugins)) {
throw new Error(".plugins should be an array, null, or undefined");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment above for this and below

@loganfsmyth loganfsmyth merged commit 6af8e64 into babel:7.0 Apr 18, 2017
@loganfsmyth loganfsmyth deleted the config-static-types branch April 18, 2017 16:28
@hzoo hzoo added the PR: Internal 🏠 A type of pull request used for our changelog categories label Apr 18, 2017
hulkish added a commit to hulkish/babel that referenced this pull request May 2, 2017
* 'master' of github.com:hulkish/babel: (190 commits)
  Fix incorrect property ordering with obj rest spread on nested (babel#5685)
  Fix PathHoister hoisting before a same-scope variable declaration.
  Updated transform-react-display-name for createReactClass addon (babel#5554)
  Fix PathHoister error attaching after export declarations.
  add .mjs to list of well known extensions
  Remove babel-helper-builder-conditional-assignment-operator-visitor, unused in babel [skip ci] (babel#5676)
  use find-cache-dir for babel-register cache (babel#5669)
  Fix operator processing in object super.
  -> parsedAst
  string -> sourceCode, ast -> generatedCode
  back to babylon
  Switch to pirates for babel-register. (babel#3670)
  [skip ci] babylon -> babel, ast -> parsedAst
  [readme] change code -> string
  Add support for object type spread (babel#5525)
  Fix object destructuring in param arrays (babel#5650)
  Remove merge helper and add more type declarations. (babel#5649)
  Typecheck much more of the config loading process (babel#5642)
  update to alpha.9 (babel#5639)
  v7.0.0-alpha.9
  ...
hulkish added a commit to hulkish/babel that referenced this pull request May 2, 2017
* '7.0' of https://github.com/babel/babel: (190 commits)
  Fix incorrect property ordering with obj rest spread on nested (babel#5685)
  Fix PathHoister hoisting before a same-scope variable declaration.
  Updated transform-react-display-name for createReactClass addon (babel#5554)
  Fix PathHoister error attaching after export declarations.
  add .mjs to list of well known extensions
  Remove babel-helper-builder-conditional-assignment-operator-visitor, unused in babel [skip ci] (babel#5676)
  use find-cache-dir for babel-register cache (babel#5669)
  Fix operator processing in object super.
  -> parsedAst
  string -> sourceCode, ast -> generatedCode
  back to babylon
  Switch to pirates for babel-register. (babel#3670)
  [skip ci] babylon -> babel, ast -> parsedAst
  [readme] change code -> string
  Add support for object type spread (babel#5525)
  Fix object destructuring in param arrays (babel#5650)
  Remove merge helper and add more type declarations. (babel#5649)
  Typecheck much more of the config loading process (babel#5642)
  update to alpha.9 (babel#5639)
  v7.0.0-alpha.9
  ...
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Oct 6, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue PR: Internal 🏠 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants