facebook / flow

Adds static typing to JavaScript to improve developer productivity and code quality.
https://flow.org/
MIT License
22.09k stars 1.86k forks source link

.flowconfig option module.name_mapper can't map to a lib definition #1322

Open samwgoldman opened 8 years ago

samwgoldman commented 8 years ago

Repro:

.flowconfig

[lib]
lib
[options]
module.name_mapper='1Doesnt\(Exist\)' -> '\1s'

A.js

/* @flow */
var m1 = require('./1DoesntExist'); // actual error: can't find module 1DoesntExist
var a_1: string = m1.numVal; // expected error: number ~> string

lib/Exists.js

declare module 'Exists' {
  declare var exports: {
    numVal: number;
  };
};

Expected output:

A.js:3:19,27: number
This type is incompatible with
A.js:3:10,15: string

Actual output:

A.js:2:22,37: ./1DoesntExist
Required module not found

Workaround: While libdefs aren't working, it's still possible to map to a node module. There's an npm module called empty that can be useful here. See this gist for a complete example.

I think the issue is that we do name mapping specifically against file paths, so the code just doesn't attempt to handle this case. I think that it should. cc @jeffmo, who originally added this feature.

jeffmo commented 8 years ago

The issue appears to be here, where we assume that an import can be resolved on the FS (but neglect to deal with the fact that an import might be made available in a libdef's declare module).

gabelevi commented 8 years ago

It also appears that you can't map to lib files. If you take the example from the docs and add CSSModule.js.flow to [libs] then you get an error. https://gist.github.com/a879ca861671edb16143d4b20ea8526e

MoOx commented 8 years ago

Following #1621, here is my flow config https://github.com/MoOx/js-boilerplate/blob/4797f86049d87f013b9cab60480d5c954c1f24a8/.flowconfig And the CSSModule definition is defined in node_modules/flow-interfaces/interfaces/CSSModule.d.js (it's this one https://github.com/TechnologyAdvice/flow-interfaces/blob/20fc1a5439803523fd3d3cf338619e58e81fc13b/interfaces/CSSModule.d.js)

MoOx commented 8 years ago

Probably related https://github.com/facebook/flow/pull/1801#issuecomment-219719511

kevinSuttle commented 8 years ago

Still seeing issues with this out of the box. Using the docs,

module.name_mapper.extension='css' -> '<PROJECT_ROOT>/CSSFlowStub.js.flow' makes Flow treat require('foo.css') as if it were require(PROJECT_ROOT + '/CSSFlowStub').

Nope.

 4: require('./Input.css');
     ^^^^^^^^^^^^^^^^^^^^^^ ./Input.css. Required module not found
MoOx commented 8 years ago

@kevinSuttle did you create CSSFlowStub.js.flow file? Please take a look at this https://stackoverflow.com/questions/36912675/flow-required-module-not-found

kevinSuttle commented 8 years ago

That helped. Thanks @MoOx.

hawkrives commented 8 years ago

It would also appear that you can't map to a flow file in flow-typed.

When I do module.name_mapper='^\(.*\)\.css$' -> '<PROJECT_ROOT>/config/flow/css', it works (no errors on importing a css file), but when I have module.name_mapper='^\(.*\)\.css$' -> '<PROJECT_ROOT>/flow-typed/css', it complains about "required module not found".

I suppose it makes sense to not look for them in flow-typed, kinda, but it was confusing.

pbomb commented 8 years ago

I just got tripped up by this as well. Putting the declaration in the flow-typed directory was my first thought. I eventually found this thread and moved my .scss stub file to a different directory and it worked.

Is the plan to support name_mapping to a declaration in flow-typed? Or should I continue to expect having to declare those in a different directory? Thanks.

Rauttis commented 7 years ago

Ran into the same thing. My first instinct was to place it in flow-typed, but that did not work.

joseds commented 7 years ago

Flow seems to be a bit picky about the location of the stub files. I had them in a folder __mocks__ where I already had stub / mock files for different purposes and flow did not pick up the stub files, leaving me with the "required module not found" error. After putting the stubs in a folder named flow-stubs, it worked.

I have filed an issue here: #3687

ArmorDarks commented 6 years ago

Wow, that's quite an old issue.

Did someone found a workaround when you need to remap a path from one file to another, without using an additional NPM package?