diff --git a/CHANGES.md b/CHANGES.md index f56b910725..89b141b1d1 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -5,6 +5,7 @@ Core Grammars: - fix(cpp) not all kinds of number literals are highlighted correctly [LĂȘ Duy Quang][] - fix(css) fix overly greedy pseudo class matching [Bradley Mackey][] - enh(arcade) updated to ArcGIS Arcade version 1.24 [Kristian Ekenes][] +- fix(typescript): params types [Mohamed Ali][] - fix(rust) fix escaped double quotes in string [Mohamed Ali][] - fix(yaml) fix for yaml with keys having brackets highlighted incorrectly [Aneesh Kulkarni][] - fix(bash) fix # within token being detected as the start of a comment [Felix Uhl][] diff --git a/src/languages/javascript.js b/src/languages/javascript.js index 4dd1090150..a0adc24dbc 100644 --- a/src/languages/javascript.js +++ b/src/languages/javascript.js @@ -256,7 +256,7 @@ export default function(hljs) { const PARAMS_CONTAINS = SUBST_AND_COMMENTS.concat([ // eat recursive parens in sub expressions { - begin: /\(/, + begin: /(\s*)\(/, end: /\)/, keywords: KEYWORDS, contains: ["self"].concat(SUBST_AND_COMMENTS) @@ -264,7 +264,8 @@ export default function(hljs) { ]); const PARAMS = { className: 'params', - begin: /\(/, + // convert this to negative lookbehind in v12 + begin: /(\s*)\(/, // to match the parms with end: /\)/, excludeBegin: true, excludeEnd: true, @@ -509,7 +510,7 @@ export default function(hljs) { skip: true }, { - begin: /\(/, + begin: /(\s*)\(/, end: /\)/, excludeBegin: true, excludeEnd: true, diff --git a/src/languages/typescript.js b/src/languages/typescript.js index 98b9e89517..5e95bcbdd2 100644 --- a/src/languages/typescript.js +++ b/src/languages/typescript.js @@ -87,6 +87,13 @@ export default function(hljs) { Object.assign(tsLanguage.keywords, KEYWORDS); tsLanguage.exports.PARAMS_CONTAINS.push(DECORATOR); + + // highlight the function params + const ATTRIBUTE_HIGHLIGHT = tsLanguage.contains.find(c => c.className === "attr"); + tsLanguage.exports.PARAMS_CONTAINS.push([ + tsLanguage.exports.CLASS_REFERENCE, // class reference for highlighting the params types + ATTRIBUTE_HIGHLIGHT, // highlight the params key + ]); tsLanguage.contains = tsLanguage.contains.concat([ DECORATOR, NAMESPACE, diff --git a/test/markup/javascript/class.expect.txt b/test/markup/javascript/class.expect.txt index 7e1dd67c80..2053069221 100644 --- a/test/markup/javascript/class.expect.txt +++ b/test/markup/javascript/class.expect.txt @@ -38,3 +38,29 @@ InT CSSParserT IResponseTsS + +// this class to show that the params may be seperated with space after the function name and highlighted as params also. +class Car extends Vehicle { + constructor (speed, cost) { + super(speed); + + var c = Symbol('cost'); + this[c] = cost; + + this.intro = `This is a car runs at + ${speed}.`; + } + + join () { + } + + other (a = ( ( 3 + 2 ) ) ) { + console.log(a) + } + + something (a = ( ( 3 + 2 ) ), b = 1 ) { + console.log(a) + } + + onemore (a=(3+2, b=(5*9))) {} +} diff --git a/test/markup/javascript/class.txt b/test/markup/javascript/class.txt index 4badef7377..57b9f0bea7 100644 --- a/test/markup/javascript/class.txt +++ b/test/markup/javascript/class.txt @@ -38,3 +38,29 @@ OutT InT CSSParserT IResponseTsS + +// this class to show that the params may be seperated with space after the function name and highlighted as params also. +class Car extends Vehicle { + constructor (speed, cost) { + super(speed); + + var c = Symbol('cost'); + this[c] = cost; + + this.intro = `This is a car runs at + ${speed}.`; + } + + join () { + } + + other (a = ( ( 3 + 2 ) ) ) { + console.log(a) + } + + something (a = ( ( 3 + 2 ) ), b = 1 ) { + console.log(a) + } + + onemore (a=(3+2, b=(5*9))) {} +} diff --git a/test/markup/javascript/seperated-parameters.expect.txt b/test/markup/javascript/seperated-parameters.expect.txt new file mode 100644 index 0000000000..f63006c91d --- /dev/null +++ b/test/markup/javascript/seperated-parameters.expect.txt @@ -0,0 +1,5 @@ +function visibleTodoFilter (state = 'watch', action) {} + +function visibleTodoFilter (state, action) { + // ... +} diff --git a/test/markup/javascript/seperated-parameters.txt b/test/markup/javascript/seperated-parameters.txt new file mode 100644 index 0000000000..6dd0cd9eaf --- /dev/null +++ b/test/markup/javascript/seperated-parameters.txt @@ -0,0 +1,5 @@ +function visibleTodoFilter (state = 'watch', action) {} + +function visibleTodoFilter (state, action) { + // ... +} diff --git a/test/markup/typescript/class.expect.txt b/test/markup/typescript/class.expect.txt index 0245c33418..13dc789278 100644 --- a/test/markup/typescript/class.expect.txt +++ b/test/markup/typescript/class.expect.txt @@ -9,3 +9,20 @@ ${speed}.`; } } + +interface TrainProps { + speed: number; + cost: number; +} + +class Train extends Vehicle { + constructor (trainData: TrainProps) { + super(trainData.speed); + + var c = Symbol('cost'); + this[c] = trainData.cost; + + this.intro = `This is a car runs at + ${trainData.speed}.`; + } +} diff --git a/test/markup/typescript/class.txt b/test/markup/typescript/class.txt index 47fa67cc35..33f4921891 100644 --- a/test/markup/typescript/class.txt +++ b/test/markup/typescript/class.txt @@ -9,3 +9,20 @@ class Car extends Vehicle { ${speed}.`; } } + +interface TrainProps { + speed: number; + cost: number; +} + +class Train extends Vehicle { + constructor (trainData: TrainProps) { + super(trainData.speed); + + var c = Symbol('cost'); + this[c] = trainData.cost; + + this.intro = `This is a car runs at + ${trainData.speed}.`; + } +} diff --git a/test/markup/typescript/declares.expect.txt b/test/markup/typescript/declares.expect.txt index 0521e1360c..689ee9a5f8 100644 --- a/test/markup/typescript/declares.expect.txt +++ b/test/markup/typescript/declares.expect.txt @@ -1,6 +1,6 @@ export declare class CommandHandler extends EventEmitter { - constructor(config: CommandHandlerConfig); + constructor(config: CommandHandlerConfig); /** * Install the handler onto Discord.js * @param client - Client to handle diff --git a/test/markup/typescript/functions.expect.txt b/test/markup/typescript/functions.expect.txt index 7aee291d5e..cd13295b96 100644 --- a/test/markup/typescript/functions.expect.txt +++ b/test/markup/typescript/functions.expect.txt @@ -4,7 +4,12 @@ return foo; }; -function println(value: string); +var identity2 = function (foo) { // seperate the params and function keyword + return foo; +}; + +function println(value: string); +function println (value: string);// seperate the params and function keyword function getArray(): number[] { return [1, 2]; @@ -24,3 +29,23 @@ const bad = ((a=2, b=5) => [...a, b]); sides.every((length,width=(3+2+(4/5))) => length > 0 ); +// test props types with interface, types, and etc. +interface Rect { + x: number; + y: number; + w: number; + h: number; +} + +function boundingRect(rect: Rect) {} +function boundingRect (rect: Rect) {} +function boundingRect(rect: Rect): Rect {} +function boundingRect (rect: Rect): Rect {} +function boundingRects(rects: Rect[]) {} +function boundingRects (rects: Rect[]) {} +function boundingRects(rects: Rect[]): Rect {} +function boundingRects (rects: Rect[]): Rect {} +function boundingRects(rects: Array<Rect>): Rect {} +function boundingRects (rects: Array<Rect>): Rect {} +function boundingRects (rects: Array<Rect>) {} + diff --git a/test/markup/typescript/functions.txt b/test/markup/typescript/functions.txt index 22d72f54ab..272f0163ee 100644 --- a/test/markup/typescript/functions.txt +++ b/test/markup/typescript/functions.txt @@ -4,7 +4,12 @@ var identity = function(foo) { return foo; }; +var identity2 = function (foo) { // seperate the params and function keyword + return foo; +}; + function println(value: string); +function println (value: string);// seperate the params and function keyword function getArray(): number[] { return [1, 2]; @@ -24,3 +29,22 @@ const array = [1, 2, 3].reduce((acc, next) => [...acc, next], []); const bad = ((a=2, b=5) => [...a, b]); sides.every((length,width=(3+2+(4/5))) => length > 0 ); +// test props types with interface, types, and etc. +interface Rect { + x: number; + y: number; + w: number; + h: number; +} + +function boundingRect(rect: Rect) {} +function boundingRect (rect: Rect) {} +function boundingRect(rect: Rect): Rect {} +function boundingRect (rect: Rect): Rect {} +function boundingRects(rects: Rect[]) {} +function boundingRects (rects: Rect[]) {} +function boundingRects(rects: Rect[]): Rect {} +function boundingRects (rects: Rect[]): Rect {} +function boundingRects(rects: Array): Rect {} +function boundingRects (rects: Array): Rect {} +function boundingRects (rects: Array) {} diff --git a/test/markup/typescript/jsx.expect.txt b/test/markup/typescript/jsx.expect.txt index 8171f9f24a..e774c97f4c 100644 --- a/test/markup/typescript/jsx.expect.txt +++ b/test/markup/typescript/jsx.expect.txt @@ -8,7 +8,7 @@ } } -export function getModuleInstanceState(node: Node): ModuleInstanceState { +export function getModuleInstanceState(node: Node): ModuleInstanceState { else if (node.kind === SyntaxKind.ModuleDeclaration) { return getModuleInstanceState((<Array<Array<number>>node).body); return getModuleInstanceState((<ModuleDeclaration>node).body);