Skip to content

Commit

Permalink
Merge pull request #611 from Galooshi/mika-babel-parser-plugins-override
Browse files Browse the repository at this point in the history
Provide the option to override parser plugins in configuration
  • Loading branch information
mikabytes authored Jan 27, 2024
2 parents 4793888 + ef9065d commit 6b6d0fe
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 60 deletions.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,54 @@ module.exports = {
};
```

### `parserPlugins`

ImportJS defaults to a reasonable compromise for what syntax to support but can be overridden (replaced) in configuration. The latest defaults can be found [here](lib/parse.js#L3)

Available plugins are over at [Babel: Plugins List](https://babeljs.io/docs/plugins-list)

#### Example: Remove all preconfigured defaults

```javascript
parserPlugins: []
```

#### Example: Add pipeline operator (`hack` proposal)

When `parserPlugins` is specified you need to re-add the defaults.

```javascript
parserPlugins: [
'jsx',
'doExpressions',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
'nullishCoalescingOperator',
'exportNamespaceFrom',
'exportDefaultFrom',
[
'pipelineOperator',
{
proposal: 'hack',
},
],
]
```

## Dynamic configuration

Different sections of your application may have special importing needs. For
Expand Down
3 changes: 3 additions & 0 deletions lib/Configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import meteorEnvironment from './environments/meteorEnvironment';
import nodeEnvironment from './environments/nodeEnvironment';
import normalizePath from './normalizePath';
import version from './version';
import { DEFAULT_PARSER_PLUGINS } from './parse.js';

const JSON_CONFIG_FILE = '.importjs.json';
const JS_CONFIG_FILES = ['.importjs.js', '.importjs.cjs', '.importjs.mjs'];
Expand Down Expand Up @@ -82,6 +83,7 @@ const DEFAULT_CONFIG = {
namedExports: true,
globals: true,
},
parserPlugins: DEFAULT_PARSER_PLUGINS,
};

const KNOWN_CONFIGURATION_OPTIONS = [
Expand Down Expand Up @@ -110,6 +112,7 @@ const KNOWN_CONFIGURATION_OPTIONS = [
'mergableOptions',
'danglingCommas',
'emptyLineBetweenGroups',
'parserPlugins',
];

const DEPRECATED_CONFIGURATION_OPTIONS = [];
Expand Down
4 changes: 3 additions & 1 deletion lib/Importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import findCurrentImports from './findCurrentImports';
import findJsModulesFor from './findJsModulesFor';
import findUndefinedIdentifiers from './findUndefinedIdentifiers';
import findUsedIdentifiers from './findUsedIdentifiers';
import parse from './parse';
import parse, { configureParserPlugins } from './parse';

function fixImportsMessage(
removedItems: Set<string>,
Expand Down Expand Up @@ -88,6 +88,8 @@ export default class Importer {
this.config = new Configuration(this.pathToCurrentFile, workingDirectory);
this.workingDirectory = workingDirectory;

configureParserPlugins(this.config.get('parserPlugins'));

this.messages = Array.from(this.config.messages);
this.unresolvedImports = {};
try {
Expand Down
1 change: 1 addition & 0 deletions lib/__tests__/Configuration-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('Configuration', () => {
namedExports: true,
globals: true,
});
expect(configuration.get('parserPlugins')).toContain('jsx');
});

describe('with a JSON configuration file', () => {
Expand Down
50 changes: 24 additions & 26 deletions lib/__tests__/parse-test.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,49 @@
import path from 'path';
import parse from '../parse';
import parse, { configureParserPlugins } from '../parse';

const local = (subPath) => path.resolve(__dirname, subPath);

it('knows about object rest spread', () => {
it('infer typescript from file extension', () => {
expect(() =>
parse(
`
const { a, b, ...c } = foo;
`,
local('foo,js'),
class Employee {
private name: string;
}
`,
local('foo.tsx'),
),
).not.toThrowError();
});

it('knows about decorators', () => {
it('should understand Flow without being told to', () => {
expect(() =>
parse(
`
@Awesome
class Foo {};
`,
local('foo,js'),
const a : string = "hello";
`,
local('foo.js'),
),
).not.toThrowError();
});

it('knows about class properties', () => {
expect(() =>
parse(
`
class Foo {
foo = 'bar';
}
`,
local('foo,js'),
),
).not.toThrowError();
});
it('should include plugins provided', () => {
configureParserPlugins([
[
'pipelineOperator',
{
proposal: 'hack',
topicToken: '%',
},
],
]);

it('knows about dynamic imports', () => {
expect(() =>
parse(
`
import('./foo').then(module => module());
`,
local('foo,js'),
"hello" |> console.log(%);
`,
local('foo.js'),
),
).not.toThrowError();
});
70 changes: 40 additions & 30 deletions lib/parse.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,44 @@
// @flow

export const DEFAULT_PARSER_PLUGINS = [
'jsx',
'doExpressions',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
[
'pipelineOperator',
{
proposal: 'minimal',
},
],
'nullishCoalescingOperator',
'exportNamespaceFrom',
'exportDefaultFrom',
];

const TYPESCRIPT_FILE_PATH_REGEX = /\.tsx?$/;
let parserPlugins = DEFAULT_PARSER_PLUGINS;

export function configureParserPlugins(
newParserPlugins: Array<string | [string, Object]>,
) {
parserPlugins = newParserPlugins;
return;
}

function getParserPlugins(
absolutePathToFile: string,
Expand All @@ -8,36 +47,7 @@ function getParserPlugins(
? 'typescript'
: 'flow';

return [
typePlugin,
'jsx',
'doExpressions',
'objectRestSpread',
'decorators-legacy',
'classProperties',
'classPrivateProperties',
'classPrivateMethods',
'exportExtensions',
'asyncGenerators',
'functionBind',
'functionSent',
'dynamicImport',
'numericSeparator',
'optionalChaining',
'importMeta',
'bigInt',
'optionalCatchBinding',
'throwExpressions',
[
'pipelineOperator',
{
proposal: 'minimal',
},
],
'nullishCoalescingOperator',
'exportNamespaceFrom',
'exportDefaultFrom',
];
return [typePlugin, ...parserPlugins];
}

export default function parse(
Expand Down
18 changes: 15 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
"type": "git",
"url": "git+https://github.com/galooshi/import-js.git"
},
"keywords": ["es6", "commonjs", "es2015", "ts", "typescript", "importing"],
"keywords": [
"es6",
"commonjs",
"es2015",
"ts",
"typescript",
"importing"
],
"author": "Henric Trotzig",
"contributors": [
{
Expand Down Expand Up @@ -73,8 +80,13 @@
"jest": {
"automock": false,
"testEnvironment": "node",
"setupFiles": ["./setupJest.js"],
"testPathIgnorePatterns": ["<rootDir>/build/", "<rootDir>/node_modules/"]
"setupFiles": [
"./setupJest.js"
],
"testPathIgnorePatterns": [
"<rootDir>/build/",
"<rootDir>/node_modules/"
]
},
"prettier": {
"singleQuote": true
Expand Down

0 comments on commit 6b6d0fe

Please sign in to comment.