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

UpdateRecorder should support multiple adjacent inserts #21110

Closed
1 task done
dgp1130 opened this issue Jun 11, 2021 · 8 comments · Fixed by #21209
Closed
1 task done

UpdateRecorder should support multiple adjacent inserts #21110

dgp1130 opened this issue Jun 11, 2021 · 8 comments · Fixed by #21209
Labels
area: @angular-devkit/schematics freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Milestone

Comments

@dgp1130
Copy link
Collaborator

dgp1130 commented Jun 11, 2021

🐞 Bug report

Command (mark with an x)

  • schematics

Is this a regression?

No

Description

From #20190.

UpdateRecorder doesn't support multiple adjacent inserts.

🔬 Minimal Reproduction

it('works with multiple adjacent inserts', () => {
  const buffer = Buffer.from('Hello beautiful World');
  const entry = new SimpleFileEntry(normalize('/some/path'), buffer);

  const recorder = new UpdateRecorderBase(entry);
  recorder.remove(6, 9);
  recorder.insertRight(6, 'amazing');
  recorder.insertRight(15, ' and fantastic');
  const result = recorder.apply(buffer);
  expect(result.toString()).toBe('Hello amazing and fantastic World');
});

Anything else relevant?

Best workaround is probably to use something more comprehensive like magic-string, but that probably shouldn't be required for something this simple.

@kyubisation
Copy link
Contributor

kyubisation commented Jun 12, 2021

Due to my limited understanding of the context of the UpdateRecorder I don't know if my solution suggestion is valid.

One way to do this, would be to track all changes in a list without mutating the original. When calling apply(), all changes are applied sequentially, sorted descending by index.

A naive implementation:
https://stackblitz.com/edit/node-4trosz?devtoolsheight=33&file=index.ts (See below)
Run npm test to run the tests (which use a mock jasmine implementation)

@kyubisation
Copy link
Contributor

Updated the StackBlitz example to use jasmine:
https://stackblitz.com/edit/node-7gdaqv?devtoolsheight=33&file=index.ts
Run npm test to run the tests

@kyubisation
Copy link
Contributor

So, I've been looking at the UpdateBuffer spec and I have to admit I don't understand the requirements. Is this complexity really required?

@dgp1130
Copy link
Collaborator Author

dgp1130 commented Jun 24, 2021

@kyubisation, I don't think this complexity is strictly required. We do recommend using magic-string as a more complete implementation. We are hoping to refactor in the future to drop this custom implementation in favor of the real magic-string. Should we just close this issue and wait for that?

@kyubisation
Copy link
Contributor

That's fine with me.
Would you accept an external contribution or is this something that should be done "internally"?

@dgp1130
Copy link
Collaborator Author

dgp1130 commented Jun 24, 2021

I think we would probably accept a PR as it is something which should work and is a legitimate bug. The existence and recommendation of magic-string just means that this is lower priority and we probably won't go out of our way to fix this particular issue. That said, definitely open to improvements here!

I think I'll leave the issue open just because it is a real issue that should be fixed eventually (whether with a direct fix or a switch to magic-string).

@kyubisation
Copy link
Contributor

kyubisation commented Jun 25, 2021

I have implemented a PR, switching usage of UpdateBuffer to magic-string: #21209
As mentioned in the PR, this is just an initial implementation and I'm open to suggestions for improvement.

filipesilva pushed a commit that referenced this issue Aug 20, 2021
…ring

This PR adds UpdateBuffer2 which should eventually
replace UpdateBuffer. UpdateBuffer2 internally uses
the magic-string library.
UpdateBuffer and related symbols have been marked
as deprecated.

Closes #21110
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: @angular-devkit/schematics freq1: low Only reported by a handful of users who observe it rarely severity3: broken type: bug/fix
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants