This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Support .mjs module javascript extension in clang-format
ClosedPublic

Authored by fergald on Aug 22 2019, 5:34 AM.

Details

Summary

PR43085.

Recognize .mjs files as JavaScript. .mjs is the extension used by Node.js for ECMAScript modules. A specific extension (and associated content type javascript/esm) is introduced to differentiate it from previous CommonJS modules and solve some interoperability problems.

Diff Detail

Repository
rL LLVM

Event Timeline

fergald created this revision.Aug 22 2019, 5:34 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2019, 5:34 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
thakis accepted this revision.Aug 22 2019, 7:17 PM
thakis added a subscriber: thakis.

Does clang-format format import and export lines in mjs files correctly?

This revision is now accepted and ready to land.Aug 22 2019, 7:17 PM

I cannot commit this, please do so, if you're happy with it.

This causes .mjs files to be treated as javascript.

I think you meant .mjs was not recognized before but will be recognized as JavaScript with this change.

Can you elaborate a bit why .mjs is so common that it justifies a clang-format change?

module-javascript. Module javascript is not compatible with non-module java script as it uses the "export" and "import" keywords which are only legal in module-javascript.

java script -> JavaScript
module-javascript -> Module JavaScript?

fergald edited the summary of this revision. (Show Details)Aug 22 2019, 7:35 PM

"why .mjs is so common that it justifies a clang-format change?"

I don't have data but Module JS is the future, it's supported by all major browsers for some time and also Node.js, it solves the problem of everything being lumped into one namespace.

Why does it need a different suffix? It adds the "export" and "import" keywords and so it incompatible with common JS. Forcing everyone to keep using js for both is not helpful (clang-format is not forcing of course but applying clang-format with --assume-filename is pretty awkward).

Node specifies .mjs as the suffix

https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff
https://nodejs.org/api/esm.html

So the use of the .mjs suffix is going to grow in volume as people modernize their project. It would be great for clang-format to be enabling that migration.

MaskRay accepted this revision.EditedAug 22 2019, 8:12 PM

Reading some materials online... I think this should be named ECMAScript module (ECM), not JavaScript modules.. I can fix the description and commit for you.

How about this description?

Recognize .mjs files as JavaScript. .mjs is the extension used by Node.js for ECMAScript modules. A specific extension (and associated content type javascript/esm) is
introduced to differentiate it from previous CommonJS modules and solve some
interoperability problems.
fergald edited the summary of this revision. (Show Details)Aug 22 2019, 9:15 PM

Thanks, I'm happy with that description

This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 22 2019, 9:45 PM