Skip to content

Commit

Permalink
JS Extras: Highlight import and export bindings (#2533)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment committed Aug 27, 2020
1 parent bcef22a commit c51abab
Show file tree
Hide file tree
Showing 4 changed files with 304 additions and 2 deletions.
29 changes: 28 additions & 1 deletion components/prism-js-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,33 @@
]
});

/**
* Replaces the `<ID>` placeholder in the given pattern with a pattern for general JS identifiers.
*
* @param {string} source
* @param {string} [flags]
* @returns {RegExp}
*/
function withId(source, flags) {
return RegExp(
source.replace(/<ID>/g, function () { return /[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/.source; }),
flags);
}
Prism.languages.insertBefore('javascript', 'keyword', {
'imports': {
// https://tc39.es/ecma262/#sec-imports
pattern: withId(/(\bimport\b\s*)(?:<ID>(?:\s*,\s*(?:\*\s*as\s+<ID>|\{[^{}]*\}))?|\*\s*as\s+<ID>|\{[^{}]*\})(?=\s*\bfrom\b)/.source),
lookbehind: true,
inside: Prism.languages.javascript
},
'exports': {
// https://tc39.es/ecma262/#sec-exports
pattern: withId(/(\bexport\b\s*)(?:\*(?:\s*as\s+<ID>)?(?=\s*\bfrom\b)|\{[^{}]*\})/.source),
lookbehind: true,
inside: Prism.languages.javascript
}
});

Prism.languages.javascript['keyword'].unshift(
{
pattern: /\b(?:as|default|export|from|import)\b/,
Expand Down Expand Up @@ -64,7 +91,7 @@

Prism.languages.insertBefore('javascript', 'punctuation', {
'property-access': {
pattern: /(\.\s*)#?[_$a-zA-Z\xA0-\uFFFF][$\w\xA0-\uFFFF]*/,
pattern: withId(/(\.\s*)#?<ID>/.source),
lookbehind: true
},
'maybe-class-name': {
Expand Down
2 changes: 1 addition & 1 deletion components/prism-js-extras.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

151 changes: 151 additions & 0 deletions tests/languages/javascript!+js-extras/exports_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
export * from "mod";
export * as Foo from "mod";
export {} from "mod";
export {x} from "mod";
export {d} from "mod";
export {d,} from "mod";
export {d as Foo, b as Bar} from "mod";
export {d as Foo, b as Bar,} from "mod";
export {}
export {x}
export {d}
export {d,}
export {d as Foo, b as Bar}
export {d as Foo, b as Bar,}

----------------------------------------------------

[
["keyword", "export"],
["exports", [
["operator", "*"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["operator", "*"],
["keyword", "as"],
["maybe-class-name", "Foo"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"x",
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d",
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d",
["punctuation", ","],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", ","],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "export"],
["exports", [
["punctuation", "{"],
["punctuation", "}"]
]],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"x",
["punctuation", "}"]
]],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d",
["punctuation", "}"]
]],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d",
["punctuation", ","],
["punctuation", "}"]
]],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", "}"]
]],
["keyword", "export"],
["exports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", ","],
["punctuation", "}"]
]]
]
124 changes: 124 additions & 0 deletions tests/languages/javascript!+js-extras/imports_feature.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import React from 'react';
import x from "mod";
import * as Foo from "mod";
import {} from "mod";
import {d} from "mod";
import {d,} from "mod";
import {d as Foo, b as Bar} from "mod";
import {d as Foo, b as Bar,} from "mod";
import Foo, { Bar } from "mod";
import Foo, * as Bar from "mod";

import "mod";

----------------------------------------------------

[
["keyword", "import"],
["imports", [
["maybe-class-name", "React"]
]],
["keyword", "from"],
["string", "'react'"],
["punctuation", ";"],
["keyword", "import"],
["imports", [
"x"
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["operator", "*"],
["keyword", "as"],
["maybe-class-name", "Foo"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["punctuation", "{"],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["punctuation", "{"],
"d",
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["punctuation", "{"],
"d",
["punctuation", ","],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["punctuation", "{"],
"d ",
["keyword", "as"],
["maybe-class-name", "Foo"],
["punctuation", ","],
" b ",
["keyword", "as"],
["maybe-class-name", "Bar"],
["punctuation", ","],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["maybe-class-name", "Foo"],
["punctuation", ","],
["punctuation", "{"],
["maybe-class-name", "Bar"],
["punctuation", "}"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],
["keyword", "import"],
["imports", [
["maybe-class-name", "Foo"],
["punctuation", ","],
["operator", "*"],
["keyword", "as"],
["maybe-class-name", "Bar"]
]],
["keyword", "from"],
["string", "\"mod\""],
["punctuation", ";"],

["keyword", "import"],
["string", "\"mod\""],
["punctuation", ";"]
]

0 comments on commit c51abab

Please sign in to comment.