-
Notifications
You must be signed in to change notification settings - Fork 65
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
fix(dts): adapt ReplaceInFileConfig interface to actual implementation #107
Conversation
@adamreisnz could you release the fix? |
I'll need a second opinion on this from another TS contributor. |
@antongolub I tested it. For me it's cause this issue :
|
What TS version was used? |
@antongolub I used the version 3.6.4, updating to 3.7.5 fixed the issue. |
While we are modifying it, I think there are others issues in this type definition. I want to be able to do this : import { replaceInFile , ReplaceInFileConfig, ReplaceResult } from 'replace-in-file';
...
const options: ReplaceInFileConfig = {
files: filePath,
from: regex,
to: replaceRegex
};
const results: ReplaceResult[] = await replaceInFile(options);
//or
const results: ReplaceResult[] = replaceInFile.sync(options);
... But it is not possible because of missing exports. The two functions replaceInFile need to be exported. In this case the sync export you added is unnecessary. |
I've added duplicate import {sync as replaceSync} from 'replace-in-file' This helps to avoid imperative renaming. const replaceSync = replaceInFile.sync |
@adamreisnz, @Gotik, @JonnySpruce, @kvpt Any objections or suggestions? |
@antongolub Your modifications work fine for me, and with your latest changes also with older typescript version 👍 Like I said, I think we should also add missing exports Here: replace-in-file/types/index.d.ts Line 3 in 22364d3
Here: replace-in-file/types/index.d.ts Line 4 in 22364d3
And here: replace-in-file/types/index.d.ts Line 7 in 22364d3
|
@kvpt could you verify the last improvement? |
@antongolub Your changes are good for me. |
I'd like to keep namespace for backward compatibility. |
@adamreisnz, @Gotik, @JonnySpruce, any feedback is appreciated. |
I don't get involved on the TypeScript side of things, so will defer to the others. If they approve the changes, I'm happy to merge. |
@adamreisnz, could anyone else check this out? |
@adamreisnz, could you summon other reviewers? I can also add some dtslint-based tests for typings, but it requires additional dev deps. If it's suitable, let me know. |
@Gotik @JonnySpruce @kvpt would you mind having one last look at these changes before they are merged? 💯 |
@adamreisnz, no feedback = no objections. Let's merge. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for the delay - fair warning I haven't worked with TypeScript since July so I may be a bit rusty, but since nobody else has looked at this I thought I'd do what I can to make sure the work does get merged.
I think the changes look good, they're definitely all improvements over the previous code. There are some minor non-Typscript changes to the way the module exported so you may want to make sure you're happy with that @adamreisnz. (FWIW I think they're good improvements, and appear to be backwards compatible).
from: string | RegExp | string[] | RegExp[] | FromCallback; | ||
to: string | string[] | ToCallback; | ||
from: From | Array<From>; | ||
to: To | Array<To>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this change not also suggest an Array of FromCallback
type could happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's possible, but let's inspect the current implementation:
replaceInFileSync
invokesparseConfig
parseConfig
optionally handlesfiles
andignore
fields, injectsdefaultConfig
and returns it back
Object.assign({}, defaults, config)
- Then config params are passed to
replaceSync
const {files, from, to, dry, verbose} = config;
...
paths.map(path => replaceSync(path, from, to, config));
- and then to
makeReplacements
makeReplacements(
contents, from, to, file, countMatches
);
- where
from
turns into array
if (!Array.isArray(from)) {
from = [from];
}
- which is reduced
const newContents = from.reduce((contents, item, i) => {
// ...
if (typeof item === 'function') {
item = item(file);
}
// ...
}
It looks that Array<FromCallback>
is supported.
@@ -84,5 +84,9 @@ replaceInFile.sync = function(config) { | |||
return paths.map(path => replaceSync(path, from, to, config)); | |||
}; | |||
|
|||
replaceInFile.replaceInFile = replaceInFile; // Self-reference to support named import | |||
replaceInFile.replaceInFileSync = replaceInFileSync; | |||
replaceInFile.sync = replaceInFileSync; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the benefit of having both sync
and replaceInFileSync
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sync
is just a legacy alias for backward compatibility
New named exports let us to avoid using of default module reference:
// Before
import replaceInFile, {sync as replaceInFileSync} from 'replace-in-file'
// After
import {replaceInFile, replaceInFileSync} from 'replace-in-file'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - changes are definitely an improvement.
Great, many thanks @JonnySpruce in helping get this over the line |
@@ -3,7 +3,7 @@ | |||
/** | |||
* Dependencies | |||
*/ | |||
const replace = require('./replace-in-file'); | |||
import replace, {sync, replaceInFile, replaceInFileSync} from './replace-in-file' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit surprised that this works and passes the travis build in Node versions 8 and 10?
I thought ES6 style import were only available in Node from version 12 onwards.
Other than the comment above which would be good to clarify, I think we're ready to merge |
No description provided.