-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Allow mapping of expected extensions in import/extension rule #2033
Conversation
1 similar comment
I'm still pretty confused here. If you want to require an extension, configure the rule to do so. If you want to only require an extension in What does this PR allow you to do that you can't already do with an eslint config? |
I'm new to eslint so I wouldn't be suprised that I misunderstand something, but here is how I understand "overrides":
I havn't found any configuration that would enforce a specific (".js" in my case) extension for all imports |
Given the following files:
if the content of file1.ts is: import {something} from "./file2"
import {other} from "./file2.js" the typescript module resolver resolves both This PR allows the developper to configure the "import/extensions" in a way that would rather make the linter expect ".js" extension in all import statements even if the resolved module is a file with another extension. import {other} from "./file2.js" |
You’re trying to force an extension appear in code that doesn’t actually exist on the filesystem? |
im trying to force an extension on an import of a module that does not yes exist. It is really awkward but typescript ensures you that it will. Otherwise typescript raises an exception if it cant make that garantee |
typescript should definately implement such a functionality but this plugin is 80 lines away from doing it |
its a non-intrusive solution that would please any nodejs+typescript enthousiasts perhaps the proposed solution could be extended to other transpilers or contexts in its flexibility. Or should I change the implementation to focus on ts transpilation? |
im linting the ts file and not the transpiled js file |
The proper solution here would be to implement this as an option in the typescript resolver, not in this plugin itself. |
I think your opinion is very defendable, but I would argue that the resolver is correct in that it resolves the module to the right file. Concerning separation of concerns, I also argue that enforcing a specific extension fits very well with this module's responsibility. |
It’s that the extension doesn’t already exist that’s the problem. This plugin is only attempting to combine node’s CJS resolution algorithm with a style preference. |
Hahah now I guess I'm confused :S
I may have missinterpreted the readme when reading that this repo was "trying to support linting of ES2015+ (ES6+) import/export syntax, and prevent any issues with misspelling of file paths and import names." I also think that linters are not ownly good for validating stylistic errors, but also (and probably more importantly) programmatic errors. |
I think the un-introssiveness of the implementation makes this at least worth a second and third opinion |
It was only very recently that node changed the plan and decided not to auto-add extensions for ESM. As such, either we enforce what babel/ts does (no extensions, primarily) or what node does (.mjs or, sometimes, .js, but only when the files exist). It seems like you’re trying to work around typescript’s poor integration with node’s native ESM support, rather than accepting that the two shouldn’t be used together yet. |
Typescript should indeed improve esm support, but they wont, because they point to here saying that linting should be the solution. Given the popularity of the MEAN stack I think having typescript accross the entire stack is goingg to be a trend for the future. Before this can happen, I think features like tgese need to be implemented. In the context where the imported module exists (in a version not yet transpiled), |
I wouldn't call it working around, I woulf call it "improve"
|
And prior to transpile time, would explicitly point to nothing - which is when the linter runs. It would likely work better to transpile your TS with babel, and transform the specifiers to add .js in the build process, and continue to omit them in your source code - then you don’t need to do somersaults in your linter config to support this case. |
prior to transpile time, it does not point to nothing. When you use a typescript resolver, it points to the ts file |
You suggest I don't transpile my ts files with Typescrtipt but rather do it with Babel? |
Always, yes. Babel is much better at transpiling TS than tsc is.
if that is true, then i'm not sure why any change is needed - the |
Exactly the js extension does work perfectly Currently the plugin takes for granted that the file's name in which the module is defined has the same extension as the module itself.
I think that this is a valid opinion which I share in some regards (it seems more and more so as time moves on). However until typed-objects become more stable I will be sticking to typescript (I don't think I will be the sole developper to do so out there) |
transpiling with babel doesn't solve the problem. The workaround is to rename typescript files so that the extension is ".js" rather than ".ts" which is even more awkward there really is no way to get this to pass is there? I'm not tied to the implementation at all, but allowing the linter to expect module extensions other than the extension of the file it is defined in seems perfectly aligned with the scope & goal of this repo. |
this would fix microsoft/TypeScript#16577 |
Just found this . You can basically configure how nodejs resolves esm modules. running nodejs with the following flag solves the initial problem behind this PR "node --experimental-specifier-resolution=node" |
I am a little hesitant to add my voice in this thread with the hostility in it, but I am looking for a similar solution. I am sorry this has become a tough issue for those involved. If this pull request is stuck between a rock and a hard place, is there a possibility of allowing resolver plugins to implement this mapping while we are in this uncanny valley of TypeScript + Node.js ESM? I imagine the rule primarily being used for TypeScript and gradually being faded out depending on whether the module specifier resolution aligns more with node, or TypeScript implements a solution for full import paths besides manually adding them in the TypeScript code with From my testing, |
@evelynhathaway that's an interesting idea - I'm not sure yet how we'd rework things so that was done cleanly, but allowing a custom resolver to do this stuff is a lot safer/simpler than building it into the core plugin. |
Because all of the following requirements are true:
Then if someone wants to use Typescript to gereate nodejs apps that use "top-level await", it makes sens to start usiung nodejs's
newly supported ESM. (this is also probably going to apply to more and more module functionalities). To do so, while developping, people have to make sure that all relative imports comply with nodejs's requirement to specify the trailing ".js" file extension.
Typescript has no functionality to enforce ".js" file extensions. Because tslint was deprecated in favor of eslint, I feel
this eslint plugin would make a lot of people happy in filling out the gap.
Note this is currently a draft which more work
fixes #2030