Skip to content

Commit

Permalink
🐛 Escape namespaced dependencies in Babel exclude regex (#21)
Browse files Browse the repository at this point in the history
🐛 Escape namespaced dependencies in Babel exclude regex
  • Loading branch information
obahareth committed Oct 29, 2019
2 parents 97ccd5f + 7127cee commit 3222a26
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "are-you-es5",
"version": "1.3.2",
"version": "1.3.3",
"description": "A package to help you find out which of your `node_modules` aren't ES5 so you can add them to your transpilation steps.",
"main": "dist/index.js",
"repository": "https://github.com/obahareth/are-you-es5.git",
Expand Down
6 changes: 6 additions & 0 deletions src/babel-loader-regex-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export function getBabelLoaderIgnoreRegex(dependencies: string[]) {
dependencies = escapeNamespacedDependencies(dependencies)

// [\\\\/] is a bit confusing but what it's doing is matching either a
// backslash or forwards slash. Forwards slashes don't need to be
// escaped inside a character group, and we need to escape the
Expand All @@ -8,3 +10,7 @@ export function getBabelLoaderIgnoreRegex(dependencies: string[]) {
// [\\/]
return `/[\\\\/]node_modules[\\\\/](?!(${dependencies.join('|')})[\\\\/])/`
}

function escapeNamespacedDependencies(dependencies: string[]): string[] {
return dependencies.map(dep => dep.replace('/', '\\/'))
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ModulesChecker } from './modules-checker'
import IModuleCheckerConfig from './types/module-checker-config'

program
.version('1.3.2')
.version('1.3.3')
.command('check <path>')
.description(
'Checks if all node_modules (including monorepos) at <path> are ES5'
Expand Down
19 changes: 19 additions & 0 deletions tests/babel-loader-regex-builder.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { getBabelLoaderIgnoreRegex } from '../src/babel-loader-regex-builder'

describe('getBabelLoaderIgnoreRegex', () => {
it('returns a regex for ignoring dependencies', () => {
const dependencies = ['dotenv', 'md5-file']
const expectedRegex =
'/[\\\\/]node_modules[\\\\/](?!(dotenv|md5-file)[\\\\/])/'

expect(getBabelLoaderIgnoreRegex(dependencies)).toEqual(expectedRegex)
})

it('handles namespaced dependencies', () => {
const dependencies = ['@react-pdf/renderer', 'dotenv', 'md5-file']
const expectedRegex =
'/[\\\\/]node_modules[\\\\/](?!(@react-pdf\\/renderer|dotenv|md5-file)[\\\\/])/'

expect(getBabelLoaderIgnoreRegex(dependencies)).toEqual(expectedRegex)
})
})

0 comments on commit 3222a26

Please sign in to comment.