Skip to content

Instantly share code, notes, and snippets.

@jonesnxt
Created October 12, 2022 21:15
Show Gist options
  • Save jonesnxt/535ce3ecf1a1bf2d7ba9792b4acee153 to your computer and use it in GitHub Desktop.
Save jonesnxt/535ce3ecf1a1bf2d7ba9792b4acee153 to your computer and use it in GitHub Desktop.
S('Program').each((file) => {
const imports = file
.children('ImportDeclaration')
.map((imp) => ({
text: imp.text(),
hasNamedImports: imp.children().length !== 1,
packageName: imp.children('Literal').text(),
}))
.sort((a, b) => (a.packageName > b.packageName ? -1 : 1));
file.children('ImportDeclaration').text('');
const firstElement = file.children().eq(0);
firstElement.before('\n');
// local imports
imports
.filter((imp) => imp.hasNamedImports && imp.packageName[1] === '.')
.forEach((imp) => firstElement.before(imp.text + '\n'));
firstElement.before('\n');
// node_modules imports
imports
.filter((imp) => imp.hasNamedImports && imp.packageName[1] !== '.')
.forEach((imp) => firstElement.before(imp.text + '\n'));
firstElement.before('\n');
// no named imports, like import 'style.css';
imports
.filter((imp) => !imp.hasNamedImports)
.forEach((imp) => firstElement.before(imp.text + '\n'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment