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

[cli] Core definitions #4255

Merged
merged 24 commits into from Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 46 additions & 26 deletions CONTRIBUTING.md
Expand Up @@ -12,6 +12,7 @@ Contributing library definitions is as easy as sending a pull request!
* [Create tests](#4-write-a-test-file-whose-name-starts-with-test_-in-the-flow-version-directory)
* [Run tests](#5-run-your-tests)
* [Raise pull request](#6-send-a-pull-request)
* [Environment definitions](environment-definitions)
* [Writing libdefs best practices](#writing-libdefs-best-practices)
* [Read flow docs](#read-flow-docs)
* [Don't import types from other libdefs](#dont-import-types-from-other-libdefs)
Expand All @@ -34,35 +35,48 @@ directory. They all must follow the following directory structure and naming
format:

```
└ definitions/npm/
├ yargs_v4.x.x/ # <-- The name of the library, followed by _v<VERSION>
| |
| ├ flow_v0.83.x/ # <-- A folder containing libdefs tested against the
| | | # specified version(s) of Flow (v0.83.x in this
| | | # case).
| | |
| | └ yargs_v4.x.x.js # <-- The libdef file meant for the Flow version
| | # specified by the containing directory's name.
| | # Must be named `<LIB>_v<VERSION>.js`.
| |
| ├ flow_v0.85.x-v0.91.x/ # <-- A folder containing libdefs tested against a
| | | # different range of Flow versions:
| | | # Anything from v0.85.x to v0.91.x (inclusive)
| | |
| | ├ yargs_v4.x.x.js # <-- The libdef file for versions of Flow from
| | | # v0.85.x to v0.91.x (inclusive)
| | |
| | └ test_yargs.js # <-- Tests in this directory only apply to the
| | # adjacent libdef (and thus, are specific to
| | # the libdefs for this specific Flow version)
| |
| └ test_yargs.js # <-- Tests in this directory apply to libdefs for
| # all versions of Flow.
├ color_v0.7.x/
└ definitions/
├ npm/
├ yargs_v4.x.x/ # <-- The name of the library, followed by _v<VERSION>
| |
| ├ flow_v0.83.x/ # <-- A folder containing libdefs tested against the
| | | # specified version(s) of Flow (v0.83.x in this
| | | # case).
| | |
| | └ yargs_v4.x.x.js # <-- The libdef file meant for the Flow version
| | # specified by the containing directory's name.
| | # Must be named `<LIB>_v<VERSION>.js`.
| |
| ├ flow_v0.85.x-v0.91.x/ # <-- A folder containing libdefs tested against a
| | | # different range of Flow versions:
| | | # Anything from v0.85.x to v0.91.x (inclusive)
| | |
| | ├ yargs_v4.x.x.js # <-- The libdef file for versions of Flow from
| | | # v0.85.x to v0.91.x (inclusive)
| | |
| | └ test_yargs.js # <-- Tests in this directory only apply to the
| | # adjacent libdef (and thus, are specific to
| | # the libdefs for this specific Flow version)
| |
| └ test_yargs.js # <-- Tests in this directory apply to libdefs for
| # all versions of Flow.
├ color_v0.7.x/
├ ...
|
├ environments/
├ jsx/ # <-- The name of the env environment
| |
| ├ flow_v0.83.x-/ # <-- A folder containing definition tested against the
| | | # specified version(s) of Flow (v0.83.x and onwards
| | | # in this case).
| | |
| | └ jsx.js # <-- The environment definition file meant for the Flow version
| ├ ...
├ ...
├ ...
```

Versions are semantically versioned (semver) with some restrictions:
Flow versions are semantically versioned (semver) with some restrictions:

* All of MAJOR, MINOR, and PATCH versions must be specified. It's acceptable to
specify `x` in place of a number for MINOR and PATCH, but MAJOR cannot be `x`.
Expand Down Expand Up @@ -180,6 +194,12 @@ node cli/dist/cli.js run-tests

You know how to do it.

---

#### Environment definitions

The above are instructions on how to submit a library definition against npm packages though the process is similar if contributing environment definitions. Except instead of a package directory you just have an environment package that is the name of the environment without the need of versions.

## Libdef best practices

### Read flow docs
Expand Down
@@ -0,0 +1,3 @@
declare type jsx$HTMLElementProps = {|
test: boolean,
|};
@@ -0,0 +1 @@
declare type jsx$HTMLElementProps = {||};
194 changes: 194 additions & 0 deletions cli/src/commands/__tests__/install-test.js
Expand Up @@ -1232,6 +1232,200 @@ describe('install (command)', () => {
).toEqual(true);
});
});

describe('env defs', () => {
it('installs env definitions if it exists in flow-typed.config.json', () => {
return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
await Promise.all([
mkdirp(path.join(FLOWPROJ_DIR, 'src')),
writePkgJson(path.join(FLOWPROJ_DIR, 'package.json'), {
name: 'test',
devDependencies: {
'flow-bin': '^0.140.0',
},
}),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'flow-bin')),
]);

await touchFile(path.join(FLOWPROJ_DIR, 'src', '.flowconfig'));
await mkdirp(path.join(FLOWPROJ_DIR, 'src', 'flow-typed'));
await touchFile(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
);
await fs.writeJson(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
{env: ['jsx']},
);

// Run the install command
await run({
...defaultRunProps,
rootDir: path.join(FLOWPROJ_DIR, 'src'),
});

// Installs env definitions
expect(
await fs.exists(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
),
).toEqual(true);
});
});

it('does not install new version of definition if it has been overridden', () => {
const installedDef = `// flow-typed signature: fa26c13e83581eea415de59d5f03e123
// flow-typed version: /jsx/flow_>=v0.83.x

declare type jsx$HTMLElementProps = {||}`;

return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
await Promise.all([
mkdirp(path.join(FLOWPROJ_DIR, 'src')),
writePkgJson(path.join(FLOWPROJ_DIR, 'package.json'), {
name: 'test',
devDependencies: {
'flow-bin': '^0.140.0',
},
}),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'flow-bin')),
]);

await touchFile(path.join(FLOWPROJ_DIR, 'src', '.flowconfig'));
await mkdirp(path.join(FLOWPROJ_DIR, 'src', 'flow-typed'));
await mkdirp(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed', 'environments'),
);
await touchFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
);
await fs.writeFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
installedDef,
);
await touchFile(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
);
await fs.writeJson(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
{env: ['jsx']},
);

// Run the install command
await run({
...defaultRunProps,
rootDir: path.join(FLOWPROJ_DIR, 'src'),
});

// Installs env definitions
expect(
await fs.readFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
'utf-8',
),
).toEqual(installedDef);
});
});

it('overrides the env definition if overwrite arg is passed in', () => {
const installedDef = `// flow-typed signature: fa26c13e83581eea415de59d5f03e123
// flow-typed version: /jsx/flow_>=v0.83.x

declare type jsx$HTMLElementProps = {||}`;

return fakeProjectEnv(async FLOWPROJ_DIR => {
// Create some dependencies
await Promise.all([
mkdirp(path.join(FLOWPROJ_DIR, 'src')),
writePkgJson(path.join(FLOWPROJ_DIR, 'package.json'), {
name: 'test',
devDependencies: {
'flow-bin': '^0.140.0',
},
}),
mkdirp(path.join(FLOWPROJ_DIR, 'node_modules', 'flow-bin')),
]);

await touchFile(path.join(FLOWPROJ_DIR, 'src', '.flowconfig'));
await mkdirp(path.join(FLOWPROJ_DIR, 'src', 'flow-typed'));
await mkdirp(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed', 'environments'),
);
await touchFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
);
await fs.writeFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
installedDef,
);
await touchFile(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
);
await fs.writeJson(
path.join(FLOWPROJ_DIR, 'src', 'flow-typed.config.json'),
{env: ['jsx']},
);

// Run the install command
await run({
...defaultRunProps,
rootDir: path.join(FLOWPROJ_DIR, 'src'),
overwrite: true,
});

// Installs env definitions
expect(
await fs.readFile(
path.join(
FLOWPROJ_DIR,
'src',
'flow-typed',
'environments',
'jsx.js',
),
'utf-8',
),
).not.toEqual(installedDef);
});
});
});
});

describe('workspace tests', () => {
Expand Down