From 16b02889ca50b8d1aefe3191105ee46faad65020 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 25 Jun 2019 16:26:40 +1000 Subject: [PATCH] fix(eslint): Add object expression in getJSDocComment --- package.json | 1 + src/eslint/getJSDocComment.js | 1 + test/eslint/getJSDocComment.js | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 test/eslint/getJSDocComment.js diff --git a/package.json b/package.json index d1d774b9c..8860047a3 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,7 @@ "mocha": "^6.1.4", "nyc": "^14.1.1", "semantic-release": "^15.13.17", + "sinon": "^7.3.2", "typescript": "^3.5.2" }, "engines": { diff --git a/src/eslint/getJSDocComment.js b/src/eslint/getJSDocComment.js index 1a7aaf802..1a26d4af8 100644 --- a/src/eslint/getJSDocComment.js +++ b/src/eslint/getJSDocComment.js @@ -70,6 +70,7 @@ const getJSDocComment = function (sourceCode, node) { return findJSDocComment(parent.parent); case 'ArrowFunctionExpression': + case 'ObjectExpression': case 'FunctionExpression': if ( parent.type !== 'CallExpression' && diff --git a/test/eslint/getJSDocComment.js b/test/eslint/getJSDocComment.js new file mode 100644 index 000000000..2eb76d8de --- /dev/null +++ b/test/eslint/getJSDocComment.js @@ -0,0 +1,34 @@ +import {Linter} from 'eslint/lib/linter'; +import {assert} from 'chai'; +import sinon from 'sinon'; +import getJSDocComment from '../../src/eslint/getJSDocComment'; + +describe('getJSDocComment', () => { + const linter = new Linter(); + it('should get JSDoc comment for node when the node is an ObjectExpression', () => { + const code = [ + '/** Desc*/', + 'const A = {', + '}' + ].join('\n'); + + const assertJSDoc = function (node) { + const sourceCode = linter.getSourceCode(); + const jsdoc = getJSDocComment(sourceCode, node); + + assert.strictEqual(jsdoc.type, 'Block'); + assert.strictEqual(jsdoc.value, '* Desc'); + }; + + const spy = sinon.spy(assertJSDoc); + + linter.defineRule('checker', () => { + return {ObjectExpression: spy}; + }); + linter.verify(code, { + parserOptions: {ecmaVersion: 6}, + rules: {checker: 'error'} + }); + assert.isTrue(spy.calledOnce, 'Event handler should be called.'); + }); +});