On Wed, 16 Mar 2022 at 05:34, Glen Choo <chooglen@xxxxxxxxxx> wrote: > > Junio C Hamano <gitster@xxxxxxxxx> writes: > > > Glen Choo <chooglen@xxxxxxxxxx> writes: > > > >> Interesting, I'll take note. I'm still personally not keen on supporting > >> CommonJS-only patterns when we are purportedly trying to show diffs for > >> JavaScript, but if we think this fits the style, I'm happy to oblige. > > > > The question is, with these patterns that are aware of CommonJS > > convention, would your bog-standard-and-boring vanilla JS code be > > detected incorrectly? Becoming aware of popular conventions without > > hurting others would be a good thing. > > > > And the "popular conventions" does not have to be limited to > > CommonJS/Node. > > From the perspective of "'exports' is a special name", yes, we could > detect vanilla JS code 'incorrectly' because, in vanilla JS, the names > 'exports' or 'module.exports' are not special. So perhaps, one could > imagine a browser-side script that deals with "imports" and "exports" as > part of their business: > > const exports = { > quantity: 1, > type: 'boxes', > }; > exports.getQuantity = () => { > foo(); > }; > > This diff driver would mistakenly detect `exports.getQuantity = () => > {`. > > Although, the more I think about it, the spirit of this patch seems to > be "we want to show headers whenever we think we are in a function", so > we don't actually need to treat 'exports' or 'module.exports' specially > at all, e.g. this case should also pass our diff driver tests: > > const foo = {}; > foo.RIGHT = () => { > > ChangeMe(); > }; > > and if we do this, we will correctly handle 'exports' and > 'module.exports' anyway by virtue of them being plain old JS objects. The spirit of this patch is to show headers when we are in the function body(which has a large code block). Because this can help users understand the context. And prevent mismatch non-related function. ex: function WRONG() { // ... } const foo = {}; foo.RIGHT = () => { ChangeMe(); }; if we don't match "foo.RIGHT = () => {". It may match the "function WRONG() {". This would be very misleading. So the v3 patch. I do not treat `exports` as a special keyword. And can match the code like "foo.RIGHT = () => {". The regex would be "((module\\.)?[$_[:alpha:]][$_[:alnum:]]*\\.[$_[:alpha:]][$_[:alnum:]]*[\t ]*=[\t ]*(\\(.*\\)|[$_[:alpha:]][$_[:alnum:]]*)[\t ]*=>.*)"