import { JS_TEMPLATE, JS_TEMPLATE_INTERPOLATION } from '../shared/languages/patterns.js'; import { toArray } from '../util/iterables.js'; import clike from './clike.js'; /** @type {import('../types.d.ts').LanguageProto<'javascript'>} */ export default { id: 'javascript', base: clike, optional: 'js-templates', alias: 'js', grammar ({ getOptionalLanguage }) { const jsTemplates = /** @type {GrammarTokens} */ (getOptionalLanguage('js-templates'))?.[ 'template-string' ]; return { 'class-name': [ { pattern: /(\b(?:class|extends|implements|instanceof|interface|new)\s+)[\w.\\]+/, lookbehind: true, inside: { 'punctuation': /[.\\]/, }, }, { pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$A-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\.(?:constructor|prototype))/, lookbehind: true, }, ], 'keyword': [ { pattern: /(^|[^.]|\.\.\.\s*)\busing(?=\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*(?:=(?!=)|\bof\b))/, lookbehind: true, }, { pattern: /(^|[^.]|\.\.\.\s*)\b(?:as|assert(?=\s*\{)|export|from(?=\s*(?:['"]|$))|import)\b/, lookbehind: true, alias: 'module', }, { pattern: /((?:^|\})\s*)catch\b/, lookbehind: true, alias: 'control-flow', }, { pattern: /(^|[^.]|\.\.\.\s*)\b(?:await|break|case|continue|default|do|else|finally(?=\s*(?:\{|$))|for|if|return|switch|throw|try|while|yield)\b/, lookbehind: true, alias: 'control-flow', }, { pattern: /(^|[^.]|\.\.\.\s*)\b(?:async(?=\s*(?:function\b|\(|[$\w\xA0-\uFFFF]|$))|class|const|debugger|delete|enum|extends|function|(?:get|set)(?=\s*(?:[#\[$\w\xA0-\uFFFF]|$))|implements|in|instanceof|interface|let|new|null|of|package|private|protected|public|static|super|this|typeof|undefined|var|void|with)\b/, lookbehind: true, }, ], // Allow for all non-ASCII characters (See http://stackoverflow.com/a/2008444) 'function': /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*(?:\.\s*(?:apply|bind|call)\s*)?\()/, 'number': { pattern: RegExp( /(^|[^\w$])/.source + '(?:' + // constant (/NaN|Infinity/.source + '|' + // binary integer /0[bB][01]+(?:_[01]+)*n?/.source + '|' + // octal integer /0[oO][0-7]+(?:_[0-7]+)*n?/.source + '|' + // hexadecimal integer /0[xX][\dA-Fa-f]+(?:_[\dA-Fa-f]+)*n?/.source + '|' + // decimal bigint /\d+(?:_\d+)*n/.source + '|' + // decimal number (integer or float) but no bigint /(?:\d+(?:_\d+)*(?:\.(?:\d+(?:_\d+)*)?)?|\.\d+(?:_\d+)*)(?:[Ee][+-]?\d+(?:_\d+)*)?/ .source) + ')' + /(?![\w$])/.source ), lookbehind: true, }, 'operator': /--|\+\+|\*\*=?|=>|&&=?|\|\|=?|[!=]==|<<=?|>>>?=?|[-+*/%&|^!=<>]=?|\.{3}|\?\?=?|\?\.?|[~:]/, $insertBefore: { 'comment': { 'doc-comment': { pattern: /\/\*\*(?!\/)[\s\S]*?(?:\*\/|$)/, greedy: true, alias: 'comment', inside: 'jsdoc', }, }, 'keyword': { 'regex': { pattern: RegExp( // lookbehind // eslint-disable-next-line regexp/no-dupe-characters-character-class /((?:^|[^$\w\xA0-\uFFFF."'\])\s]|\b(?:return|yield))\s*)/.source + // Regex pattern: // There are 2 regex patterns here. The RegExp set notation proposal added support for nested character // classes if the `v` flag is present. Unfortunately, nested CCs are both context-free and incompatible // with the only syntax, so we have to define 2 different regex patterns. /\//.source + '(?:' + /(?:\[(?:[^\]\\\r\n]|\\.)*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}/ .source + '|' + // `v` flag syntax. This supports 3 levels of nested character classes. /(?:\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.|\[(?:[^[\]\\\r\n]|\\.)*\])*\])*\]|\\.|[^/\\\[\r\n])+\/[dgimyus]{0,7}v[dgimyus]{0,7}/ .source + ')' + // lookahead /(?=(?:\s|\/\*(?:[^*]|\*(?!\/))*\*\/)*(?:$|[\r\n,.;:})\]]|\/\/))/ .source ), lookbehind: true, greedy: true, inside: { 'regex-source': { pattern: /^(\/)[\s\S]+(?=\/[a-z]*$)/, lookbehind: true, alias: 'language-regex', inside: 'regex', }, 'regex-delimiter': /^\/|\/$/, 'regex-flags': /^[a-z]+$/, }, }, // This must be declared before keyword because we use "function" inside the look-forward 'function-variable': { pattern: /#?(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*[=:]\s*(?:async\s*)?(?:\bfunction\b|(?:\((?:[^()]|\([^()]*\))*\)|(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)\s*=>))/, alias: 'function', }, 'parameter': [ { pattern: /(function(?:\s+(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*)?\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\))/, lookbehind: true, inside: 'javascript', }, { pattern: /(^|[^$\w\xA0-\uFFFF])(?!\s)[_$a-z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*=>)/i, lookbehind: true, inside: 'javascript', }, { pattern: /(\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*=>)/, lookbehind: true, inside: 'javascript', }, { pattern: /((?:\b|\s|^)(?!(?:as|async|await|break|case|catch|class|const|continue|debugger|default|delete|do|else|enum|export|extends|finally|for|from|function|get|if|implements|import|in|instanceof|interface|let|new|null|of|package|private|protected|public|return|set|static|super|switch|this|throw|try|typeof|undefined|var|void|while|with|yield)(?![$\w\xA0-\uFFFF]))(?:(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*\s*)\(\s*|\]\s*\(\s*)(?!\s)(?:[^()\s]|\s+(?![\s)])|\([^()]*\))+(?=\s*\)\s*\{)/, lookbehind: true, inside: 'javascript', }, ], 'constant': /\b[A-Z](?:[A-Z_]|\dx?)*\b/, }, 'string': { 'hashbang': { pattern: /^#!.*/, greedy: true, alias: 'comment', }, 'template-string': [ ...toArray(jsTemplates), { pattern: JS_TEMPLATE, greedy: true, inside: /** @type {Grammar} */ ({ 'template-punctuation': { pattern: /^`|`$/, alias: 'string', }, 'interpolation': { pattern: RegExp( /((?:^|[^\\])(?:\\{2})*)/.source + JS_TEMPLATE_INTERPOLATION.source ), lookbehind: true, inside: { 'interpolation-punctuation': { pattern: /^\$\{|\}$/, alias: 'punctuation', }, $rest: /** @type {Grammar['$rest']} */ ('javascript'), }, }, 'string': /[\s\S]+/, }), }, ], 'string-property': { pattern: /((?:^|[,{])[ \t]*)(["'])(?:\\(?:\r\n|[\s\S])|(?!\2)[^\\\r\n])*\2(?=\s*:)/m, lookbehind: true, greedy: true, alias: 'property', }, }, 'operator': { 'literal-property': { pattern: /((?:^|[,{])[ \t]*)(?!\s)[_$a-zA-Z\xA0-\uFFFF](?:(?!\s)[$\w\xA0-\uFFFF])*(?=\s*:)/m, lookbehind: true, alias: 'property', }, }, }, }; }, }; /** * @typedef {import('../types.d.ts').Grammar} Grammar * @typedef {import('../types.d.ts').GrammarTokens} GrammarTokens */