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

fix(dts): adapt ReplaceInFileConfig interface to actual implementation #107

Merged
merged 3 commits into from
Apr 25, 2020

Conversation

antongolub
Copy link
Contributor

No description provided.

@coveralls
Copy link

coveralls commented Feb 6, 2020

Coverage Status

Coverage remained the same at 100.0% when pulling 531b1ec on antongolub:master into 2a96bb2 on adamreisnz:master.

@antongolub
Copy link
Contributor Author

@adamreisnz could you release the fix?

@adamreisnz
Copy link
Owner

I'll need a second opinion on this from another TS contributor.
@Gotik, @JonnySpruce care to comment/review?

@kvpt
Copy link

kvpt commented Feb 11, 2020

@antongolub I tested it.

For me it's cause this issue :

error TS2303: Circular definition of import alias 'sync'
L10 export { sync };

@antongolub
Copy link
Contributor Author

@kvpt

What TS version was used?

@kvpt
Copy link

kvpt commented Feb 11, 2020

@antongolub I used the version 3.6.4, updating to 3.7.5 fixed the issue.

@kvpt
Copy link

kvpt commented Feb 11, 2020

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.
The namespace replaceInFile need to be exported.

In this case the sync export you added is unnecessary.

@antongolub
Copy link
Contributor Author

antongolub commented Feb 11, 2020

@kvpt

I've added duplicate sync declaration to be able to use named import like this:

import {sync as replaceSync} from 'replace-in-file'

This helps to avoid imperative renaming.

const replaceSync = replaceInFile.sync

@antongolub
Copy link
Contributor Author

@adamreisnz, @Gotik, @JonnySpruce, @kvpt

Any objections or suggestions?

@kvpt
Copy link

kvpt commented Feb 18, 2020

@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:

function replaceInFile(config: ReplaceInFileConfig): Promise<ReplaceResult[]>;

Here:

function replaceInFile(config: ReplaceInFileConfig, cb: (error: Error, results: ReplaceResult[]) => void): void;

And here:

namespace replaceInFile {

@antongolub
Copy link
Contributor Author

@kvpt could you verify the last improvement?

@kvpt
Copy link

kvpt commented Feb 19, 2020

@antongolub Your changes are good for me.
The only question I have is the purpose of the replaceInFile namespace, now that all the functions inside are hoisted. For me it is now useless and without being exported we can't access methods inside, or I miss something ?

@antongolub
Copy link
Contributor Author

I'd like to keep namespace for backward compatibility.

@antongolub
Copy link
Contributor Author

@adamreisnz, @Gotik, @JonnySpruce, any feedback is appreciated.

@adamreisnz
Copy link
Owner

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.

@antongolub
Copy link
Contributor Author

@adamreisnz, could anyone else check this out?

@antongolub
Copy link
Contributor Author

@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.

@adamreisnz
Copy link
Owner

@Gotik @JonnySpruce @kvpt would you mind having one last look at these changes before they are merged? 💯

@antongolub
Copy link
Contributor Author

@adamreisnz, no feedback = no objections. Let's merge.

Copy link
Contributor

@JonnySpruce JonnySpruce left a 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>;
Copy link
Contributor

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?

Copy link
Contributor Author

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:

  1. replaceInFileSync invokes parseConfig
  2. parseConfig optionally handles files and ignore fields, injects defaultConfig and returns it back
Object.assign({}, defaults, config)
  1. Then config params are passed to replaceSync
const {files, from, to, dry, verbose} = config;
...
paths.map(path => replaceSync(path, from, to, config));
  1. and then to makeReplacements
makeReplacements(
    contents, from, to, file, countMatches
  );
  1. where from turns into array
  if (!Array.isArray(from)) {
    from = [from];
  }
  1. 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;
Copy link
Contributor

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?

Copy link
Contributor Author

@antongolub antongolub Apr 24, 2020

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'

Copy link
Contributor

@JonnySpruce JonnySpruce left a 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.

@adamreisnz
Copy link
Owner

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'
Copy link
Owner

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.

@adamreisnz
Copy link
Owner

Other than the comment above which would be good to clarify, I think we're ready to merge

@adamreisnz adamreisnz merged commit 931bee7 into adamreisnz:master Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants