diff --git a/README.md b/README.md index 2c3a78d..4f204bc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/Configuration.js b/lib/Configuration.js index 791e291..892f6aa 100644 --- a/lib/Configuration.js +++ b/lib/Configuration.js @@ -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']; @@ -82,6 +83,7 @@ const DEFAULT_CONFIG = { namedExports: true, globals: true, }, + parserPlugins: DEFAULT_PARSER_PLUGINS, }; const KNOWN_CONFIGURATION_OPTIONS = [ @@ -110,6 +112,7 @@ const KNOWN_CONFIGURATION_OPTIONS = [ 'mergableOptions', 'danglingCommas', 'emptyLineBetweenGroups', + 'parserPlugins', ]; const DEPRECATED_CONFIGURATION_OPTIONS = []; diff --git a/lib/Importer.js b/lib/Importer.js index 52dcb8e..56ad5de 100644 --- a/lib/Importer.js +++ b/lib/Importer.js @@ -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, @@ -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 { diff --git a/lib/__tests__/Configuration-test.js b/lib/__tests__/Configuration-test.js index c0cd6de..5cd0425 100644 --- a/lib/__tests__/Configuration-test.js +++ b/lib/__tests__/Configuration-test.js @@ -98,6 +98,7 @@ describe('Configuration', () => { namedExports: true, globals: true, }); + expect(configuration.get('parserPlugins')).toContain('jsx'); }); describe('with a JSON configuration file', () => { diff --git a/lib/__tests__/parse-test.js b/lib/__tests__/parse-test.js index f0169ba..dfab5dc 100644 --- a/lib/__tests__/parse-test.js +++ b/lib/__tests__/parse-test.js @@ -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(); }); diff --git a/lib/parse.js b/lib/parse.js index 46e5fd9..d970491 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -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, +) { + parserPlugins = newParserPlugins; + return; +} function getParserPlugins( absolutePathToFile: string, @@ -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( diff --git a/package.json b/package.json index d3d0e32..2cdec6e 100644 --- a/package.json +++ b/package.json @@ -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": [ { @@ -73,8 +80,13 @@ "jest": { "automock": false, "testEnvironment": "node", - "setupFiles": ["./setupJest.js"], - "testPathIgnorePatterns": ["/build/", "/node_modules/"] + "setupFiles": [ + "./setupJest.js" + ], + "testPathIgnorePatterns": [ + "/build/", + "/node_modules/" + ] }, "prettier": { "singleQuote": true