Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidate JS, TS, LiveScript, CoffeeScript on common foundation #2518

Merged
merged 2 commits into from Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc.js
Expand Up @@ -2,7 +2,8 @@ module.exports = {
"env": {
"browser": true,
"es6": true,
"node": true
"node": true,
"mocha": true
},
"extends": [
"eslint:recommended",
Expand Down
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -2,6 +2,7 @@

Parser Engine:

- (parser) Adds SHEBANG utility mode [Josh Goebel][]
- (enh) Added `on:begin` callback for modes (#2261) [Josh Goebel][]
- (enh) Added `on:end` callback for modes (#2261) [Josh Goebel][]
- (enh) Added ability to programatically ignore begin and end matches (#2261) [Josh Goebel][]
Expand All @@ -13,6 +14,7 @@ Deprecations:

Language Improvements:

- enh(typescript/javascript/coffeescript/livescript) derive ECMAscript keywords from a common foudation (#2518) [Josh Goebel][]
- enh(typescript) add setInterval, setTimeout, clearInterval, clearTimeout (#2514) [Josh Goebel][]
- enh(javascript) add setInterval, setTimeout, clearInterval, clearTimeout (#2514) [Vania Kucher][]
- fix(javascript) prevent `set` keyword conflicting with setTimeout, etc. (#2514) [Vania Kucher][]
Expand Down
21 changes: 16 additions & 5 deletions src/languages/bash.js
Expand Up @@ -55,11 +55,21 @@ export default function(hljs) {
VAR
]
};
const SHEBANG = {
className: 'meta',
begin: /^#![^\n]+sh\s*$/,
const SH_LIKE_SHELLS = [
"fish",
"bash",
"zsh",
"sh",
"csh",
"ksh",
"tcsh",
"dash",
"scsh",
];
const KNOWN_SHEBANG = hljs.SHEBANG({
binary: `(${SH_LIKE_SHELLS.join("|")})`,
relevance: 10
};
});
const FUNCTION = {
className: 'function',
begin: /\w[\w\d_]*\s*\(\s*\)\s*\{/,
Expand Down Expand Up @@ -98,7 +108,8 @@ export default function(hljs) {
'-ne -eq -lt -gt -f -d -e -s -l -a' // relevance booster
},
contains: [
SHEBANG,
KNOWN_SHEBANG, // to catch known shells and boost relevancy
hljs.SHEBANG(), // to catch unknown shells but still highlight the shebang
FUNCTION,
ARITHMETIC,
hljs.HASH_COMMENT_MODE,
Expand Down
50 changes: 37 additions & 13 deletions src/languages/coffeescript.js
Expand Up @@ -7,21 +7,45 @@ Category: common, scripting
Website: https://coffeescript.org
*/

import * as ECMAScript from "./lib/ecmascript";

export default function(hljs) {
var COFFEE_BUILT_INS = [
'npm',
'print'
];
var COFFEE_LITERALS = [
'yes',
'no',
'on',
'off'
];
var COFFEE_KEYWORDS = [
'then',
'unless',
'until',
'loop',
'by',
'when',
'and',
'or',
'is',
'isnt',
'not'
];
var NOT_VALID_KEYWORDS = [
"var",
"const",
"let",
"function",
"static"
];
var excluding = (list) =>
(kw) => !list.includes(kw);
var KEYWORDS = {
keyword:
// JS keywords
'in if for while finally new do return else break catch instanceof throw try this ' +
'switch continue typeof delete debugger super yield import export from as default await ' +
// Coffee keywords
'then unless until loop of by when and or is isnt not',
literal:
// JS literals
'true false null undefined ' +
// Coffee literals
'yes no on off',
built_in:
'npm require console print module global window document'
keyword: ECMAScript.KEYWORDS.concat(COFFEE_KEYWORDS).filter(excluding(NOT_VALID_KEYWORDS)).join(" "),
literal: ECMAScript.LITERALS.concat(COFFEE_LITERALS).join(" "),
built_in: ECMAScript.BUILT_INS.concat(COFFEE_BUILT_INS).join(" ")
};
var JS_IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
var SUBST = {
Expand Down
7 changes: 1 addition & 6 deletions src/languages/hy.js
Expand Up @@ -45,11 +45,6 @@ export default function(hljs) {
var SYMBOL_RE = '[' + SYMBOLSTART + '][' + SYMBOLSTART + '0-9/;:]*';
var SIMPLE_NUMBER_RE = '[-+]?\\d+(\\.\\d+)?';

var SHEBANG = {
className: 'meta',
begin: '^#!', end: '$'
};

var SYMBOL = {
begin: SYMBOL_RE,
relevance: 0
Expand Down Expand Up @@ -105,6 +100,6 @@ export default function(hljs) {
name: 'Hy',
aliases: ['hylang'],
illegal: /\S/,
contains: [SHEBANG, LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL]
contains: [hljs.SHEBANG(), LIST, STRING, HINT, HINT_COL, COMMENT, KEY, COLLECTION, NUMBER, LITERAL]
}
}
31 changes: 9 additions & 22 deletions src/languages/javascript.js
Expand Up @@ -5,6 +5,8 @@ Category: common, scripting
Website: https://developer.mozilla.org/en-US/docs/Web/JavaScript
*/

import * as ECMAScript from "./lib/ecmascript";

export default function(hljs) {
var FRAGMENT = {
begin: '<>',
Expand All @@ -16,24 +18,9 @@ export default function(hljs) {
};
var IDENT_RE = '[A-Za-z$_][0-9A-Za-z$_]*';
var KEYWORDS = {
keyword:
'in of if for while finally var new function do return void else break catch ' +
'instanceof with throw case default try this switch continue typeof delete ' +
'let yield const export super debugger as async await static ' +
// ECMAScript 6 modules import
'import from as'
,
literal:
'true false null undefined NaN Infinity',
built_in:
'eval isFinite isNaN parseFloat parseInt decodeURI decodeURIComponent ' +
'encodeURI encodeURIComponent escape unescape Object Function Boolean Error ' +
'EvalError InternalError RangeError ReferenceError StopIteration SyntaxError ' +
'TypeError URIError Number Math Date String RegExp Array Float32Array ' +
'Float64Array Int16Array Int32Array Int8Array Uint16Array Uint32Array ' +
'Uint8Array Uint8ClampedArray ArrayBuffer DataView JSON Intl arguments require ' +
'module console window document Symbol Set Map WeakSet WeakMap Proxy Reflect ' +
'Promise setInterval setTimeout clearInterval clearTimeout'
keyword: ECMAScript.KEYWORDS.join(" "),
literal: ECMAScript.LITERALS.join(" "),
built_in: ECMAScript.BUILT_INS.join(" ")
};
var NUMBER = {
className: 'number',
Expand Down Expand Up @@ -110,15 +97,15 @@ export default function(hljs) {
aliases: ['js', 'jsx', 'mjs', 'cjs'],
keywords: KEYWORDS,
contains: [
hljs.SHEBANG({
binary: "node",
relevance: 5
}),
{
className: 'meta',
relevance: 10,
begin: /^\s*['"]use (strict|asm)['"]/
},
{
className: 'meta',
begin: /^#!/, end: /$/
},
hljs.APOS_STRING_MODE,
hljs.QUOTE_STRING_MODE,
HTML_TEMPLATE,
Expand Down
138 changes: 138 additions & 0 deletions src/languages/lib/ecmascript.js
@@ -0,0 +1,138 @@
const KEYWORDS = [
"as", // for exports
"in",
"of",
"if",
"for",
"while",
"finally",
"var",
"new",
"function",
"do",
"return",
"void",
"else",
"break",
"catch",
"instanceof",
"with",
"throw",
"case",
"default",
"try",
"switch",
"continue",
"typeof",
"delete",
"let",
"yield",
"const",
"class",
// JS handles these with a special rule
// "get",
// "set",
"debugger",
"async",
"await",
"static",
"import",
"from",
"export",
"extends"
];
const LITERALS = [
"true",
"false",
"null",
"undefined",
"NaN",
"Infinity"
];

const TYPES = [
"Intl",
"DataView",
"Number",
"Math",
"Date",
"String",
"RegExp",
"Object",
"Function",
"Boolean",
"Error",
"Symbol",
"Set",
"Map",
"WeakSet",
"WeakMap",
"Proxy",
"Reflect",
"JSON",
"Promise",
"Float64Array",
"Int16Array",
"Int32Array",
"Int8Array",
"Uint16Array",
"Uint32Array",
"Float32Array",
"Array",
"Uint8Array",
"Uint8ClampedArray",
"ArrayBuffer"
];

const ERROR_TYPES = [
"EvalError",
"InternalError",
"RangeError",
"ReferenceError",
"SyntaxError",
"TypeError",
"URIError"
];

const BUILT_IN_GLOBALS = [
"setInterval",
"setTimeout",
"clearInterval",
"clearTimeout",

"require",
"exports",

"eval",
"isFinite",
"isNaN",
"parseFloat",
"parseInt",
"decodeURI",
"decodeURIComponent",
"encodeURI",
"encodeURIComponent",
"escape",
"unescape"
];

const BUILT_IN_VARIABLES = [
"arguments",
"this",
"super",
"console",
"window",
"document",
"localStorage",
"module",
"global" // Node.js
];

const BUILT_INS = [].concat(
BUILT_IN_GLOBALS,
BUILT_IN_VARIABLES,
TYPES,
ERROR_TYPES
);

export { LITERALS, BUILT_INS, KEYWORDS };
6 changes: 1 addition & 5 deletions src/languages/lisp.js
Expand Up @@ -9,10 +9,6 @@ export default function(hljs) {
var LISP_IDENT_RE = '[a-zA-Z_\\-\\+\\*\\/\\<\\=\\>\\&\\#][a-zA-Z0-9_\\-\\+\\*\\/\\<\\=\\>\\&\\#!]*';
var MEC_RE = '\\|[^]*?\\|';
var LISP_SIMPLE_NUMBER_RE = '(\\-|\\+)?\\d+(\\.\\d+|\\/\\d+)?((d|e|f|l|s|D|E|F|L|S)(\\+|\\-)?\\d+)?';
var SHEBANG = {
className: 'meta',
begin: '^#!', end: '$'
};
var LITERAL = {
className: 'literal',
begin: '\\b(t{1}|nil)\\b'
Expand Down Expand Up @@ -97,7 +93,7 @@ export default function(hljs) {
illegal: /\S/,
contains: [
NUMBER,
SHEBANG,
hljs.SHEBANG(),
LITERAL,
STRING,
COMMENT,
Expand Down