Skip to content

Instantly share code, notes, and snippets.

@pilwon
Created March 29, 2015 06:57
Show Gist options
  • Save pilwon/ff55634a29bb4456e0dd to your computer and use it in GitHub Desktop.
Save pilwon/ff55634a29bb4456e0dd to your computer and use it in GitHub Desktop.
Tests if regex matches all valid ES6 import syntaxes. https://github.com/facebook/react-native/pull/386
//
// Tests if regular expression matches all valid ES6 import syntaxes.
//
// ES6 Language Specification Reference:
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-imports
//
const ES6_IMPORT_REGEX = /\bimport\s+(?:.+\s+from\s+)?[\'"]([^"\']+)["\']/g;
const VALID_ES6_IMPORT_SYNTAXES = [
"import 'module' ;",
"import Default from 'module' ;",
"import * as All from 'module' ;",
"import { } from 'module' ;",
"import { Foo } from 'module' ;",
"import { Foo , } from 'module' ;",
"import { Foo as Bar } from 'module' ;",
"import { Foo as Bar , } from 'module' ;",
"import { Foo , Bar } from 'module' ;",
"import { Foo , Bar , } from 'module' ;",
"import { Foo as Bar , Baz } from 'module' ;",
"import { Foo as Bar , Baz , } from 'module' ;",
"import { Foo , Bar as Baz } from 'module' ;",
"import { Foo , Bar as Baz , } from 'module' ;",
"import { Foo as Bar , Baz as Qux } from 'module' ;",
"import { Foo as Bar , Baz as Qux , } from 'module' ;",
"import { Foo , Bar, Baz } from 'module' ;",
"import { Foo , Bar, Baz , } from 'module' ;",
"import { Foo as Bar , Baz , Qux } from 'module' ;",
"import { Foo as Bar , Baz , Qux , } from 'module' ;",
"import { Foo , Bar as Baz , Qux } from 'module' ;",
"import { Foo , Bar as Baz , Qux , } from 'module' ;",
"import { Foo , Bar , Baz as Qux } from 'module' ;",
"import { Foo , Bar , Baz as Qux , } from 'module' ;",
"import { Foo as Bar , Baz as Qux , Norf } from 'module' ;",
"import { Foo as Bar , Baz as Qux , Norf , } from 'module' ;",
"import { Foo as Bar , Baz , Qux as Norf } from 'module' ;",
"import { Foo as Bar , Baz , Qux as Norf , } from 'module' ;",
"import { Foo , Bar as Baz , Qux as Norf } from 'module' ;",
"import { Foo , Bar as Baz , Qux as Norf , } from 'module' ;",
"import { Foo as Bar , Baz as Qux , Norf as Enuf } from 'module' ;",
"import { Foo as Bar , Baz as Qux , Norf as Enuf , } from 'module' ;",
"import Default , * as All from 'module' ;",
"import Default , { } from 'module' ;",
"import Default , { Foo } from 'module' ;",
"import Default , { Foo , } from 'module' ;",
"import Default , { Foo as Bar } from 'module' ;",
"import Default , { Foo as Bar , } from 'module' ;",
"import Default , { Foo , Bar } from 'module' ;",
"import Default , { Foo , Bar , } from 'module' ;",
"import Default , { Foo as Bar , Baz } from 'module' ;",
"import Default , { Foo as Bar , Baz , } from 'module' ;",
"import Default , { Foo , Bar as Baz } from 'module' ;",
"import Default , { Foo , Bar as Baz , } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux , } from 'module' ;",
"import Default , { Foo , Bar, Baz } from 'module' ;",
"import Default , { Foo , Bar, Baz , } from 'module' ;",
"import Default , { Foo as Bar , Baz , Qux } from 'module' ;",
"import Default , { Foo as Bar , Baz , Qux , } from 'module' ;",
"import Default , { Foo , Bar as Baz , Qux } from 'module' ;",
"import Default , { Foo , Bar as Baz , Qux , } from 'module' ;",
"import Default , { Foo , Bar , Baz as Qux } from 'module' ;",
"import Default , { Foo , Bar , Baz as Qux , } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux , Norf } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux , Norf , } from 'module' ;",
"import Default , { Foo as Bar , Baz , Qux as Norf } from 'module' ;",
"import Default , { Foo as Bar , Baz , Qux as Norf , } from 'module' ;",
"import Default , { Foo , Bar as Baz , Qux as Norf } from 'module' ;",
"import Default , { Foo , Bar as Baz , Qux as Norf , } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux , Norf as NoMore } from 'module' ;",
"import Default , { Foo as Bar , Baz as Qux , Norf as NoMore , } from 'module' ;",
'import "module" ;',
'import Default from "module" ;',
'import * as All from "module" ;',
'import { } from "module" ;',
'import { Foo } from "module" ;',
'import { Foo , } from "module" ;',
'import { Foo as Bar } from "module" ;',
'import { Foo as Bar , } from "module" ;',
'import { Foo , Bar } from "module" ;',
'import { Foo , Bar , } from "module" ;',
'import { Foo as Bar , Baz } from "module" ;',
'import { Foo as Bar , Baz , } from "module" ;',
'import { Foo , Bar as Baz } from "module" ;',
'import { Foo , Bar as Baz , } from "module" ;',
'import { Foo as Bar , Baz as Qux } from "module" ;',
'import { Foo as Bar , Baz as Qux , } from "module" ;',
'import { Foo , Bar, Baz } from "module" ;',
'import { Foo , Bar, Baz , } from "module" ;',
'import { Foo as Bar , Baz , Qux } from "module" ;',
'import { Foo as Bar , Baz , Qux , } from "module" ;',
'import { Foo , Bar as Baz , Qux } from "module" ;',
'import { Foo , Bar as Baz , Qux , } from "module" ;',
'import { Foo , Bar , Baz as Qux } from "module" ;',
'import { Foo , Bar , Baz as Qux , } from "module" ;',
'import { Foo as Bar , Baz as Qux , Norf } from "module" ;',
'import { Foo as Bar , Baz as Qux , Norf , } from "module" ;',
'import { Foo as Bar , Baz , Qux as Norf } from "module" ;',
'import { Foo as Bar , Baz , Qux as Norf , } from "module" ;',
'import { Foo , Bar as Baz , Qux as Norf } from "module" ;',
'import { Foo , Bar as Baz , Qux as Norf , } from "module" ;',
'import { Foo as Bar , Baz as Qux , Norf as NoMore } from "module" ;',
'import { Foo as Bar , Baz as Qux , Norf as NoMore , } from "module" ;',
'import Default , * as All from "module" ;',
'import Default , { } from "module" ;',
'import Default , { Foo } from "module" ;',
'import Default , { Foo , } from "module" ;',
'import Default , { Foo as Bar } from "module" ;',
'import Default , { Foo as Bar , } from "module" ;',
'import Default , { Foo , Bar } from "module" ;',
'import Default , { Foo , Bar , } from "module" ;',
'import Default , { Foo as Bar , Baz } from "module" ;',
'import Default , { Foo as Bar , Baz , } from "module" ;',
'import Default , { Foo , Bar as Baz } from "module" ;',
'import Default , { Foo , Bar as Baz , } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux , } from "module" ;',
'import Default , { Foo , Bar, Baz } from "module" ;',
'import Default , { Foo , Bar, Baz , } from "module" ;',
'import Default , { Foo as Bar , Baz , Qux } from "module" ;',
'import Default , { Foo as Bar , Baz , Qux , } from "module" ;',
'import Default , { Foo , Bar as Baz , Qux } from "module" ;',
'import Default , { Foo , Bar as Baz , Qux , } from "module" ;',
'import Default , { Foo , Bar , Baz as Qux } from "module" ;',
'import Default , { Foo , Bar , Baz as Qux , } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux , Norf } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux , Norf , } from "module" ;',
'import Default , { Foo as Bar , Baz , Qux as Norf } from "module" ;',
'import Default , { Foo as Bar , Baz , Qux as Norf , } from "module" ;',
'import Default , { Foo , Bar as Baz , Qux as Norf } from "module" ;',
'import Default , { Foo , Bar as Baz , Qux as Norf , } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux , Norf as Enuf } from "module" ;',
'import Default , { Foo as Bar , Baz as Qux , Norf as Enuf , } from "module" ;',
];
VALID_ES6_IMPORT_SYNTAXES.forEach((code) => {
let deps = [];
code.replace(ES6_IMPORT_REGEX, function (match, dep) {
deps.push(dep);
});
console.log(`[${deps.length ? 'PASS' : 'FAIL'}] ${code}`);
});
[PASS] import 'module' ;
[PASS] import Default from 'module' ;
[PASS] import * as All from 'module' ;
[PASS] import { } from 'module' ;
[PASS] import { Foo } from 'module' ;
[PASS] import { Foo , } from 'module' ;
[PASS] import { Foo as Bar } from 'module' ;
[PASS] import { Foo as Bar , } from 'module' ;
[PASS] import { Foo , Bar } from 'module' ;
[PASS] import { Foo , Bar , } from 'module' ;
[PASS] import { Foo as Bar , Baz } from 'module' ;
[PASS] import { Foo as Bar , Baz , } from 'module' ;
[PASS] import { Foo , Bar as Baz } from 'module' ;
[PASS] import { Foo , Bar as Baz , } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux , } from 'module' ;
[PASS] import { Foo , Bar, Baz } from 'module' ;
[PASS] import { Foo , Bar, Baz , } from 'module' ;
[PASS] import { Foo as Bar , Baz , Qux } from 'module' ;
[PASS] import { Foo as Bar , Baz , Qux , } from 'module' ;
[PASS] import { Foo , Bar as Baz , Qux } from 'module' ;
[PASS] import { Foo , Bar as Baz , Qux , } from 'module' ;
[PASS] import { Foo , Bar , Baz as Qux } from 'module' ;
[PASS] import { Foo , Bar , Baz as Qux , } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux , Norf } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux , Norf , } from 'module' ;
[PASS] import { Foo as Bar , Baz , Qux as Norf } from 'module' ;
[PASS] import { Foo as Bar , Baz , Qux as Norf , } from 'module' ;
[PASS] import { Foo , Bar as Baz , Qux as Norf } from 'module' ;
[PASS] import { Foo , Bar as Baz , Qux as Norf , } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux , Norf as Enuf } from 'module' ;
[PASS] import { Foo as Bar , Baz as Qux , Norf as Enuf , } from 'module' ;
[PASS] import Default , * as All from 'module' ;
[PASS] import Default , { } from 'module' ;
[PASS] import Default , { Foo } from 'module' ;
[PASS] import Default , { Foo , } from 'module' ;
[PASS] import Default , { Foo as Bar } from 'module' ;
[PASS] import Default , { Foo as Bar , } from 'module' ;
[PASS] import Default , { Foo , Bar } from 'module' ;
[PASS] import Default , { Foo , Bar , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz , } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux , } from 'module' ;
[PASS] import Default , { Foo , Bar, Baz } from 'module' ;
[PASS] import Default , { Foo , Bar, Baz , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz , Qux } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz , Qux , } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz , Qux } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz , Qux , } from 'module' ;
[PASS] import Default , { Foo , Bar , Baz as Qux } from 'module' ;
[PASS] import Default , { Foo , Bar , Baz as Qux , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz , Qux as Norf } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz , Qux as Norf , } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz , Qux as Norf } from 'module' ;
[PASS] import Default , { Foo , Bar as Baz , Qux as Norf , } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf as NoMore } from 'module' ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf as NoMore , } from 'module' ;
[PASS] import "module" ;
[PASS] import Default from "module" ;
[PASS] import * as All from "module" ;
[PASS] import { } from "module" ;
[PASS] import { Foo } from "module" ;
[PASS] import { Foo , } from "module" ;
[PASS] import { Foo as Bar } from "module" ;
[PASS] import { Foo as Bar , } from "module" ;
[PASS] import { Foo , Bar } from "module" ;
[PASS] import { Foo , Bar , } from "module" ;
[PASS] import { Foo as Bar , Baz } from "module" ;
[PASS] import { Foo as Bar , Baz , } from "module" ;
[PASS] import { Foo , Bar as Baz } from "module" ;
[PASS] import { Foo , Bar as Baz , } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux , } from "module" ;
[PASS] import { Foo , Bar, Baz } from "module" ;
[PASS] import { Foo , Bar, Baz , } from "module" ;
[PASS] import { Foo as Bar , Baz , Qux } from "module" ;
[PASS] import { Foo as Bar , Baz , Qux , } from "module" ;
[PASS] import { Foo , Bar as Baz , Qux } from "module" ;
[PASS] import { Foo , Bar as Baz , Qux , } from "module" ;
[PASS] import { Foo , Bar , Baz as Qux } from "module" ;
[PASS] import { Foo , Bar , Baz as Qux , } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux , Norf } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux , Norf , } from "module" ;
[PASS] import { Foo as Bar , Baz , Qux as Norf } from "module" ;
[PASS] import { Foo as Bar , Baz , Qux as Norf , } from "module" ;
[PASS] import { Foo , Bar as Baz , Qux as Norf } from "module" ;
[PASS] import { Foo , Bar as Baz , Qux as Norf , } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux , Norf as NoMore } from "module" ;
[PASS] import { Foo as Bar , Baz as Qux , Norf as NoMore , } from "module" ;
[PASS] import Default , * as All from "module" ;
[PASS] import Default , { } from "module" ;
[PASS] import Default , { Foo } from "module" ;
[PASS] import Default , { Foo , } from "module" ;
[PASS] import Default , { Foo as Bar } from "module" ;
[PASS] import Default , { Foo as Bar , } from "module" ;
[PASS] import Default , { Foo , Bar } from "module" ;
[PASS] import Default , { Foo , Bar , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz } from "module" ;
[PASS] import Default , { Foo as Bar , Baz , } from "module" ;
[PASS] import Default , { Foo , Bar as Baz } from "module" ;
[PASS] import Default , { Foo , Bar as Baz , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux , } from "module" ;
[PASS] import Default , { Foo , Bar, Baz } from "module" ;
[PASS] import Default , { Foo , Bar, Baz , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz , Qux } from "module" ;
[PASS] import Default , { Foo as Bar , Baz , Qux , } from "module" ;
[PASS] import Default , { Foo , Bar as Baz , Qux } from "module" ;
[PASS] import Default , { Foo , Bar as Baz , Qux , } from "module" ;
[PASS] import Default , { Foo , Bar , Baz as Qux } from "module" ;
[PASS] import Default , { Foo , Bar , Baz as Qux , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz , Qux as Norf } from "module" ;
[PASS] import Default , { Foo as Bar , Baz , Qux as Norf , } from "module" ;
[PASS] import Default , { Foo , Bar as Baz , Qux as Norf } from "module" ;
[PASS] import Default , { Foo , Bar as Baz , Qux as Norf , } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf as Enuf } from "module" ;
[PASS] import Default , { Foo as Bar , Baz as Qux , Norf as Enuf , } from "module" ;
@jonschlinkert
Copy link

the quote regex is brittle, something like this should be used instead:

const ES6_IMPORT_REGEX = /\bimport\s+(?:.+\s+from\s+)?(['"])([^\1]+)(\1)/g;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment