@@ -151,54 +151,110 @@ describe("applyEditsToNormalizedContent uniqueness", () => {
151151 } ) ;
152152} ) ;
153153
154- describe ( "fuzzy edit — byte preservation on matched line" , ( ) => {
155- it ( "preserves unicode bytes outside the edited span when fuzzy matching" , ( ) => {
156- // Line has NBSP (U+00A0 between RETRY and MAX), fullwidth parens,
157- // fullwidth digit, halfwidth katakana, and em-dash — all outside
158- // the span being replaced.
154+ describe ( "fuzzy edit source-span mapping" , ( ) => {
155+ it ( "preserves escaped Unicode bytes outside a fuzzy-matched span" , ( ) => {
159156 const content =
160- 'export const RETRY MAX = 3; // 再試行(最大3回)アイウ — 設定\nexport const OTHER = 1;\n' ;
161-
162- // oldText uses plain space instead of NBSP → triggers fuzzy match
163- const edits = [
164- { oldText : "export const RETRY MAX = 3;" , newText : "export const RETRY_MAX = 5;" } ,
165- ] ;
166-
167- const result = applyEditsPreservingLineEndings ( content , edits , "config.ts" ) ;
168-
169- // The replacement itself must be applied
170- expect ( result . finalContent ) . toContain ( "RETRY_MAX = 5;" ) ;
171-
172- // The comment must remain byte-identical (no NFKC normalization)
173- const originalComment = content . slice ( content . indexOf ( "//" ) ) ;
174- const afterComment = result . finalContent . slice ( result . finalContent . indexOf ( "//" ) ) ;
175- expect ( afterComment ) . toBe ( originalComment ) ;
176-
177- // The exact-match path on the same file must produce the same result
178- const exactContent =
179- 'export const RETRY MAX = 3; // 再試行(最大3回)アイウ — 設定\nexport const OTHER = 1;\n' ;
180- const exactEdits = [
181- {
182- oldText : "export const RETRY MAX = 3;" ,
183- newText : "export const RETRY_MAX = 5;" ,
184- } ,
185- ] ;
186- const exactResult = applyEditsPreservingLineEndings (
187- exactContent ,
188- exactEdits ,
157+ "export const RETRY\u00A0MAX = 3; // \u518D\u8A66\u884C\uFF08\u6700\u5927\uFF13\u56DE\uFF09\uFF71\uFF72\uFF73 \u2014 \u8A2D\u5B9A\n" +
158+ "export const OTHER = 1;\n" ;
159+
160+ const result = applyEditsPreservingLineEndings (
161+ content ,
162+ [ { oldText : "export const RETRY MAX = 3;" , newText : "export const RETRY_MAX = 5;" } ] ,
189163 "config.ts" ,
190164 ) ;
191- expect ( result . finalContent ) . toBe ( exactResult . finalContent ) ;
165+
166+ expect ( result . finalContent ) . toBe (
167+ "export const RETRY_MAX = 5; // \u518D\u8A66\u884C\uFF08\u6700\u5927\uFF13\u56DE\uFF09\uFF71\uFF72\uFF73 \u2014 \u8A2D\u5B9A\n" +
168+ "export const OTHER = 1;\n" ,
169+ ) ;
170+ } ) ;
171+
172+ it ( "maps smart-quote folds while preserving a smart-quote comment" , ( ) => {
173+ const content =
174+ "const label = \u201Chello\u201D; // keep \u201Ccomment\u201D \u2014 unchanged\n" ;
175+ const result = applyEditsPreservingLineEndings (
176+ content ,
177+ [ { oldText : 'const label = "hello";' , newText : "const label = 'hi';" } ] ,
178+ "test.ts" ,
179+ ) ;
180+
181+ expect ( result . finalContent ) . toBe (
182+ "const label = 'hi'; // keep \u201Ccomment\u201D \u2014 unchanged\n" ,
183+ ) ;
184+ } ) ;
185+
186+ it ( "allows a match that covers a complete NFKC expansion" , ( ) => {
187+ const result = applyEditsPreservingLineEndings (
188+ "const value = \uFB01;\n" ,
189+ [ { oldText : "fi" , newText : "pair" } ] ,
190+ "test.ts" ,
191+ ) ;
192+
193+ expect ( result . finalContent ) . toBe ( "const value = pair;\n" ) ;
194+ } ) ;
195+
196+ it ( "rejects a match ending inside an NFKC expansion" , ( ) => {
197+ expect ( ( ) =>
198+ applyEditsPreservingLineEndings (
199+ "const value = \uFB01;\n" ,
200+ [ { oldText : "f" , newText : "x" } ] ,
201+ "test.ts" ,
202+ ) ,
203+ ) . toThrow ( / a m b i g u o u s U n i c o d e - n o r m a l i z a t i o n o r t r i m m e d - w h i t e s p a c e b o u n d a r y / ) ;
204+ } ) ;
205+
206+ it ( "maps a complete combining sequence and preserves supplementary characters" , ( ) => {
207+ const content = "const caf\u0065\u0301 = 1; // \u{1F642}\n" ;
208+ const result = applyEditsPreservingLineEndings (
209+ content ,
210+ [ { oldText : "const caf\u00E9 = 1;" , newText : "const cafe = 2;" } ] ,
211+ "test.ts" ,
212+ ) ;
213+
214+ expect ( result . finalContent ) . toBe ( "const cafe = 2; // \u{1F642}\n" ) ;
215+ } ) ;
216+
217+ it ( "preserves trimmed bytes at EOF" , ( ) => {
218+ const result = applyEditsPreservingLineEndings (
219+ "const value\u00A0= 1; " ,
220+ [ { oldText : "const value = 1;" , newText : "const value = 2;" } ] ,
221+ "test.ts" ,
222+ ) ;
223+
224+ expect ( result . finalContent ) . toBe ( "const value = 2; " ) ;
192225 } ) ;
193226
194- it ( "preserves smart quotes in comments during fuzzy edit" , ( ) => {
195- const content = 'let x = "hello"; // says "hello world" — nice\nlet y = 2;\n' ;
196- // Use straight quotes in oldText while the file has smart quotes
197- const edits = [ { oldText : 'let x = "hello";' , newText : "let x = 'hi';" } ] ;
198- const result = applyEditsPreservingLineEndings ( content , edits , "test.ts" ) ;
227+ it ( "keeps trim-folding behavior inside a complete fuzzy span" , ( ) => {
228+ const result = applyEditsPreservingLineEndings (
229+ "before\nfirst \nsecond\nafter \n" ,
230+ [ { oldText : "first\nsecond" , newText : "combined" } ] ,
231+ "test.ts" ,
232+ ) ;
233+
234+ expect ( result . finalContent ) . toBe ( "before\ncombined\nafter \n" ) ;
235+ } ) ;
236+
237+ it ( "preserves CRLF while mapping a fuzzy Unicode span" , ( ) => {
238+ const result = applyEditsPreservingLineEndings (
239+ "const value\u00A0= 1;\r\nnext\r\n" ,
240+ [ { oldText : "const value = 1;" , newText : "const value = 2;" } ] ,
241+ "test.ts" ,
242+ ) ;
243+
244+ expect ( result . finalContent ) . toBe ( "const value = 2;\r\nnext\r\n" ) ;
245+ } ) ;
246+
247+ it ( "keeps exact and fuzzy replacements in original coordinates" , ( ) => {
248+ const result = applyEditsPreservingLineEndings (
249+ "const a\u00A0= 1;\nconst b = 2;\n" ,
250+ [
251+ { oldText : "const a = 1;" , newText : "const a = 3;" } ,
252+ { oldText : "const b = 2;" , newText : "const b = 4;" } ,
253+ ] ,
254+ "test.ts" ,
255+ ) ;
199256
200- expect ( result . finalContent ) . toContain ( "let x = 'hi';" ) ;
201- expect ( result . finalContent ) . toContain ( '// says "hello world" — nice' ) ;
257+ expect ( result . finalContent ) . toBe ( "const a = 3;\nconst b = 4;\n" ) ;
202258 } ) ;
203259} ) ;
204260
0 commit comments