Skip to content

Commit

Permalink
Merge branch 'main' into yaml-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Dxuian authored Oct 1, 2024
2 parents db106a0 + ff3985d commit 00823e5
Show file tree
Hide file tree
Showing 13 changed files with 473 additions and 35 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ Core Grammars:
- enh(erlang) OTP25/27 maybe statement [nixxquality][]
- enh(dart) Support digit-separators in number literals [Sam Rawlins][]
- enh(csharp) add Contextual keywords `file`, `args`, `dynamic`, `record`, `required` and `scoped` [Alvin Joy][]
- enh(lua) add 'pluto' as an alias [Sainan]
- enh(bash) add reserved keywords `time` and `coproc` [Álvaro Mondéjar][]
- fix(c) - Fixed hex numbers with decimals [Dxuian]
- fix(typescript) - Fixedoptional property not highlighted correctly [Dxuian]
- fix(ruby) - fix `|=` operator false positives (as block arguments) [Aboobacker MK]
- fix(sql) - Fixed sql primary key and foreign key spacing issue [Dxuian]

New Grammars:

Expand Down Expand Up @@ -47,6 +50,7 @@ CONTRIBUTORS
[Dxuian]:https://github.com/Dxuian
[Aboobacker MK]: https://github.com/tachyons
[Imken]: https://github.com/immccn123
[Sainan]: https://github.com/Sainan
[Osmocom]: https://github.com/osmocom
[Álvaro Mondéjar]: https://github.com/mondeja

Expand Down
3 changes: 2 additions & 1 deletion SUPPORTED_LANGUAGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ The table below shows the full list of languages (and corresponding classes/alia
| LiveCode Server | livecodeserver | |
| LiveScript | livescript, ls | |
| LookML | lookml | [highlightjs-lookml](https://github.com/spectacles-ci/highlightjs-lookml) |
| Lua | lua | |
| Lua | lua, pluto | |
| Luau | luau | [highlightjs-luau](https://github.com/highlightjs/highlightjs-luau) |
| Macaulay2 | macaulay2 | [highlightjs-macaulay2](https://github.com/d-torrance/highlightjs-macaulay2) |
| Makefile | makefile, mk, mak, make | |
Expand Down Expand Up @@ -175,6 +175,7 @@ The table below shows the full list of languages (and corresponding classes/alia
| Plaintext | plaintext, txt, text | |
| Pony | pony | |
| PostgreSQL & PL/pgSQL | pgsql, postgres, postgresql | |
| PowerOn | poweron, po | [highlightjs-poweron](https://github.com/libum-llc/highlightjs-poweron) |
| PowerShell | powershell, ps, ps1 | |
| Processing | processing | |
| Prolog | prolog | |
Expand Down
6 changes: 3 additions & 3 deletions src/languages/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ export default function(hljs) {
const PARAMS = {
className: 'params',
// convert this to negative lookbehind in v12
begin: /(\s*)\(/, // to match the parms with
begin: /(\s*)\(/, // to match the parms with
end: /\)/,
excludeBegin: true,
excludeEnd: true,
Expand Down Expand Up @@ -476,8 +476,8 @@ export default function(hljs) {
NUMBER,
CLASS_REFERENCE,
{
className: 'attr',
begin: IDENT_RE + regex.lookahead(':'),
scope: 'attr',
match: IDENT_RE + regex.lookahead(':'),
relevance: 0
},
FUNCTION_VARIABLE,
Expand Down
1 change: 1 addition & 0 deletions src/languages/lua.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default function(hljs) {
];
return {
name: 'Lua',
aliases: ['pluto'],
keywords: {
$pattern: hljs.UNDERSCORE_IDENT_RE,
literal: "true false nil",
Expand Down
51 changes: 31 additions & 20 deletions src/languages/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ export default function(hljs) {
const regex = hljs.regex;
const COMMENT_MODE = hljs.COMMENT('--', '$');
const STRING = {
className: 'string',
scope: 'string',
variants: [
{
begin: /'/,
end: /'/,
contains: [ { begin: /''/ } ]
contains: [ { match: /''/ } ]
}
]
};
const QUOTED_IDENTIFIER = {
begin: /"/,
end: /"/,
contains: [ { begin: /""/ } ]
contains: [ { match: /""/ } ]
};

const LITERALS = [
Expand Down Expand Up @@ -606,22 +606,42 @@ export default function(hljs) {
});

const VARIABLE = {
className: "variable",
begin: /@[a-z0-9][a-z0-9_]*/,
scope: "variable",
match: /@[a-z0-9][a-z0-9_]*/,
};

const OPERATOR = {
className: "operator",
begin: /[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/,
scope: "operator",
match: /[-+*/=%^~]|&&?|\|\|?|!=?|<(?:=>?|<|>)?|>[>=]?/,
relevance: 0,
};

const FUNCTION_CALL = {
begin: regex.concat(/\b/, regex.either(...FUNCTIONS), /\s*\(/),
match: regex.concat(/\b/, regex.either(...FUNCTIONS), /\s*\(/),
relevance: 0,
keywords: { built_in: FUNCTIONS }
};

// turns a multi-word keyword combo into a regex that doesn't
// care about extra whitespace etc.
// input: "START QUERY"
// output: /\bSTART\s+QUERY\b/
function kws_to_regex(list) {
return regex.concat(
/\b/,
regex.either(...list.map((kw) => {
return kw.replace(/\s+/, "\\s+")
})),
/\b/
)
}

const MULTI_WORD_KEYWORDS = {
scope: "keyword",
match: kws_to_regex(COMBOS),
relevance: 0,
};

// keywords with less than 3 letters are reduced in relevancy
function reduceRelevancy(list, {
exceptions, when
Expand Down Expand Up @@ -654,19 +674,10 @@ export default function(hljs) {
},
contains: [
{
begin: regex.either(...COMBOS),
relevance: 0,
keywords: {
$pattern: /[\w\.]+/,
keyword: KEYWORDS.concat(COMBOS),
literal: LITERALS,
type: TYPES
},
},
{
className: "type",
begin: regex.either(...MULTI_WORD_TYPES)
scope: "type",
match: kws_to_regex(MULTI_WORD_TYPES)
},
MULTI_WORD_KEYWORDS,
FUNCTION_CALL,
VARIABLE,
STRING,
Expand Down
16 changes: 13 additions & 3 deletions src/languages/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import javascript from "./javascript.js";

/** @type LanguageFn */
export default function(hljs) {
const regex = hljs.regex;
const tsLanguage = javascript(hljs);

const IDENT_RE = ECMAScript.IDENT_RE;
Expand Down Expand Up @@ -68,20 +69,19 @@ export default function(hljs) {
"override",
"satisfies"
];

/*
namespace is a TS keyword but it's fine to use it as a variable name too.
const message = 'foo';
const namespace = 'bar';
*/

const KEYWORDS = {
$pattern: ECMAScript.IDENT_RE,
keyword: ECMAScript.KEYWORDS.concat(TS_SPECIFIC_KEYWORDS),
literal: ECMAScript.LITERALS,
built_in: ECMAScript.BUILT_INS.concat(TYPES),
"variable.language": ECMAScript.BUILT_IN_VARIABLES
};

const DECORATOR = {
className: 'meta',
begin: '@' + IDENT_RE,
Expand All @@ -102,15 +102,25 @@ export default function(hljs) {
tsLanguage.exports.PARAMS_CONTAINS.push(DECORATOR);

// highlight the function params
const ATTRIBUTE_HIGHLIGHT = tsLanguage.contains.find(c => c.className === "attr");
const ATTRIBUTE_HIGHLIGHT = tsLanguage.contains.find(c => c.scope === "attr");

// take default attr rule and extend it to support optionals
const OPTIONAL_KEY_OR_ARGUMENT = Object.assign({},
ATTRIBUTE_HIGHLIGHT,
{ match: regex.concat(IDENT_RE, regex.lookahead(/\s*\?:/)) }
);
tsLanguage.exports.PARAMS_CONTAINS.push([
tsLanguage.exports.CLASS_REFERENCE, // class reference for highlighting the params types
ATTRIBUTE_HIGHLIGHT, // highlight the params key
OPTIONAL_KEY_OR_ARGUMENT, // Added for optional property assignment highlighting
]);

// Add the optional property assignment highlighting for objects or classes
tsLanguage.contains = tsLanguage.contains.concat([
DECORATOR,
NAMESPACE,
INTERFACE,
OPTIONAL_KEY_OR_ARGUMENT, // Added for optional property assignment highlighting
]);

// TS gets a simpler shebang rule than JS
Expand Down
24 changes: 24 additions & 0 deletions test/builds/node_build_as_esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import hljs from "../../build/es/index.js";

const API = [
"getLanguage",
"registerLanguage",
"highlight",
"highlightAuto",
"highlightAll",
"highlightElement",
];

const assert = (f, msg) => {
if (!f()) {
console.error(msg);
process.exit(1);
}
};
const keys = Object.keys(hljs);

API.forEach((n) => {
assert((_) => keys.includes(n), `API should include ${n}`);
});

console.log("Pass: browser build works with Node.js just fine.");
16 changes: 16 additions & 0 deletions test/builds/rollup_import_cdn_build_esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rollup.config.js
import commonjs from "@rollup/plugin-commonjs";

export default {
input: "test/builds/cdn_build_as_esm.mjs",
output: {
file: "build/bundle.js",
format: "iife",
},
plugins: [
commonjs({
include: "build/**", // Default: undefined
exclude: ["node_modules/**"], // Default: undefined
}),
],
};
16 changes: 16 additions & 0 deletions test/builds/rollup_import_node_build_esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// rollup.config.js
import commonjs from "@rollup/plugin-commonjs";

export default {
input: "test/builds/node_build_as_esm.mjs",
output: {
file: "build/bundle.js",
format: "iife",
},
plugins: [
commonjs({
include: "build/**", // Default: undefined
exclude: ["node_modules/**"], // Default: undefined
}),
],
};
118 changes: 118 additions & 0 deletions test/markup/sql/combos.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
<span class="hljs-keyword">CREATE TABLE</span> users (
id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
username <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
email <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>)
);

<span class="hljs-comment">-- Table with Composite Primary Key</span>
<span class="hljs-keyword">CREATE TABLE</span> orders (
order_id <span class="hljs-type">INT</span>,
user_id <span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY KEY</span> (order_id, user_id)
);

<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
<span class="hljs-keyword">CREATE TABLE</span> products (
product_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span> AUTO_INCREMENT,
name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
price <span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
);

<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
<span class="hljs-keyword">CREATE TABLE</span> order_items (
item_id <span class="hljs-type">INT</span>,
order_id <span class="hljs-type">INT</span>,
product_id <span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY KEY</span> (item_id),
<span class="hljs-keyword">FOREIGN KEY</span> (order_id) <span class="hljs-keyword">REFERENCES</span> orders(order_id)
);

<span class="hljs-comment">-- Table with Date and Primary Key</span>
<span class="hljs-keyword">CREATE TABLE</span> events (
event_id <span class="hljs-type">INT</span> <span class="hljs-keyword">PRIMARY KEY</span>,
event_name <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
event_date <span class="hljs-type">DATE</span>
);

<span class="hljs-comment">-- Basic Table with a Single Primary Key</span>
<span class="hljs-keyword">CREATE
TABLE</span>
users
(
id
<span class="hljs-type">INT</span>
<span class="hljs-keyword">PRIMARY
KEY</span>,
username
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>),
email
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>)
);

<span class="hljs-comment">-- Table with Composite Primary Key</span>
<span class="hljs-keyword">CREATE
TABLE</span>
orders
(
order_id
<span class="hljs-type">INT</span>,
user_id
<span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY
KEY</span>
(order_id,
user_id)
);

<span class="hljs-comment">-- Table with Primary Key and Auto Increment</span>
<span class="hljs-keyword">CREATE
TABLE</span>
products
(
product_id
<span class="hljs-type">INT</span>
<span class="hljs-keyword">PRIMARY
KEY</span>
AUTO_INCREMENT,
name
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
price
<span class="hljs-type">DECIMAL</span>(<span class="hljs-number">10</span>, <span class="hljs-number">2</span>)
);

<span class="hljs-comment">-- Table with Primary Key and Foreign Key</span>
<span class="hljs-keyword">CREATE
TABLE</span>
order_items
(
item_id
<span class="hljs-type">INT</span>,
order_id
<span class="hljs-type">INT</span>,
product_id
<span class="hljs-type">INT</span>,
<span class="hljs-keyword">PRIMARY
KEY</span>
(item_id),
<span class="hljs-keyword">FOREIGN
KEY</span>
(order_id)
<span class="hljs-keyword">REFERENCES</span>
orders(order_id)
);

<span class="hljs-comment">-- Table with Date and Primary Key</span>
<span class="hljs-keyword">CREATE
TABLE</span>
events
(
event_id
<span class="hljs-type">INT</span>
<span class="hljs-keyword">PRIMARY
KEY</span>,
event_name
<span class="hljs-type">VARCHAR</span>(<span class="hljs-number">100</span>),
event_date
<span class="hljs-type">DATE</span> <span class="hljs-type">with timezone</span>
);
Loading

0 comments on commit 00823e5

Please sign in to comment.