Import Sort Babylon Parser
import-sort-parser-babylon is a specialized parser plugin designed for the `import-sort` ecosystem, enabling the sorting of ECMAScript (ES2015+) imports in JavaScript and TypeScript files. Currently at version 6.0.0, this package integrates with the primary `import-sort` library, which is generally used in conjunction with `import-sort-config` and a chosen sorting style (e.g., `import-sort-style-eslint`). It leverages the capabilities of the Babel parser (formerly known as Babylon) to analyze and manipulate import declarations, offering robust support for modern JavaScript syntax, JSX, and TypeScript features. The `import-sort` project, of which this parser is a component, appears to maintain an active development cadence, providing tools for CLI usage, editor integrations, and programmatic sorting. Its key differentiator lies in its use of the battle-tested Babel parsing engine, ensuring broad compatibility with various language features and proposals.
Common errors
-
SyntaxError: Unexpected token (X:Y)
cause The Babel parser used by `import-sort-parser-babylon` did not recognize a specific language feature (e.g., new syntax, TypeScript, JSX) because the corresponding plugin was not enabled in the configuration.fixAdd the required Babel parser plugin (e.g., `jsx`, `typescript`, `decorators-legacy`) to the `parserOptions.plugins` array in your `importSort` configuration within `package.json` or `.importsortrc`. -
parser.parseImports is not a function
cause The `import-sort` core or integration could not find or correctly load the parser function from `import-sort-parser-babylon`, possibly due to incorrect module resolution or a corrupted installation.fixEnsure `import-sort-parser-babylon` is correctly installed and listed in your project dependencies. Verify your `importSort` configuration specifies `'babylon'` as the parser name, and restart any IDE extensions or CLI processes to reload the configuration. -
Configuration error: Unknown parser 'babylon'
cause The `import-sort` tool itself (or its CLI/IDE integration) failed to locate the `import-sort-parser-babylon` package, often due to an incorrect package name in the configuration or an incomplete installation.fixDouble-check that `import-sort-parser-babylon` is installed in your project and that your `importSort` configuration correctly specifies `parser: 'babylon'` (or similar, depending on the exact `import-sort` configuration structure).
Warnings
- breaking The underlying parser 'babylon' was renamed to '@babel/parser'. While 'import-sort-parser-babylon' might abstract this, direct usage or advanced configuration might require referencing '@babel/parser' features or plugins.
- gotcha Incorrect or missing Babel parser plugins in your `import-sort` configuration can lead to parsing errors for modern JavaScript or TypeScript syntax (e.g., decorators, class properties).
- gotcha Conflicting import sorting rules from other tools like ESLint (`sort-imports`) or Prettier plugins can lead to formatting churn or unexpected behavior.
- gotcha When using `import-sort` within a broader toolchain (e.g., with Prettier v3.x or pnpm), ensure compatibility and correct plugin resolution, as some users have reported issues with specific package managers or tool versions.
Install
-
npm install import-sort-parser-babylon -
yarn add import-sort-parser-babylon -
pnpm add import-sort-parser-babylon
Imports
- default
const babylonParser = require('import-sort-parser-babylon');import babylonParser from 'import-sort-parser-babylon';
- ImportSortParser
import type { ImportSortParser } from 'import-sort-parser-babylon';
Quickstart
{
"importSort": {
"language": "javascript",
".js, .jsx, .ts, .tsx": {
"parser": "babylon",
"style": "eslint",
"options": {
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module",
"plugins": [
"jsx",
"typescript",
"classProperties",
"decorators-legacy",
"dynamicImport"
]
}
}
}
}
}