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

docs(*): add docstrings - #37 #38

Merged
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
13 changes: 11 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"jsx": true
}
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"extends": ["eslint:recommended", "plugin:react/recommended", "plugin:jsdoc/recommended"],
"rules": {
"arrow-parens": ["error", "as-needed"],
"comma-dangle": ["error", "always-multiline"],
Expand All @@ -29,7 +29,16 @@
"no-multi-spaces": "error",
"no-trailing-spaces": "error",
"quotes": ["error", "single"],
"semi": ["error", "always"]
"semi": ["error", "always"],
"require-jsdoc": ["warn", {
"require": {
"FunctionDeclaration": true,
"MethodDefinition": true,
"ClassDeclaration": true,
"ArrowFunctionExpression": true,
"FunctionExpression": true
}
}]
},
"settings": {
"react": {
Expand Down
96 changes: 96 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"build:dev": "webpack --mode development --https false",
"build-dev": "webpack --mode development --https false && echo . && echo . && echo . && echo Please use 'build:dev' instead of 'build-dev'.",
"dev-server": "webpack-dev-server --mode development",
"lint": "eslint **/*.{js,jsx}",
"lint:fix": "eslint **/*.{js,jsx} --fix",
"lint": "eslint '**/*.{js,jsx}'",
"lint:fix": "eslint '**/*.{js,jsx}' --fix",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these quotes needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
If no quotation marks are applied, then this issue occurs.
So the quotation marks are required for the lint command to run properly.

"start": "office-addin-debugging start manifest.xml",
"start:desktop": "office-addin-debugging start manifest.xml desktop",
"start:web": "office-addin-debugging start manifest.xml web",
Expand Down Expand Up @@ -49,6 +49,7 @@
"copy-webpack-plugin": "^6.0.1",
"css-loader": "^3.0.0",
"eslint": "^7.1.0",
"eslint-plugin-jsdoc": "^32.3.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even after adding this package, I don't get an error when I run npm run lint even though I have removed all docstrings from my local.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
This package does not enforce js-doc to be present.
I found that we could go with the package eslint-plugin-require-jsdoc.
However, this rule was deprecated as mentioned here.
So should we go with this one? 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package does not enforce js-doc to be present.

Why have you added it then? Can you tell its purpose? Does it only check the syntax of the JSDoc?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
The purpose of the above-added package was to enforce valid js-docs. It ensures that JSDoc follows certain rules. More details here. To enforce that JSDoc is present eslint-plugin-require-jsdoc should be installed (deprecated) as I mentioned here.

Copy link
Collaborator Author

@K-Kumar-01 K-Kumar-01 Apr 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@algomaster99
I was able to figure that the eslint rules for the arrow function and other things were false by default, which resulted in no warning being thrown for missing docs.
Hence we do no require eslint-plugin-require-jsdoc. I will update my PR with the new rules ASAP.

"eslint-plugin-react": "^7.16.0",
"extract-text-webpack-plugin": "4.0.0-beta.0",
"file-loader": "^4.2.0",
Expand Down
18 changes: 18 additions & 0 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ import { Menu } from 'semantic-ui-react';
import TemplateLibrary from './TemplateLibrary';
import './App.css';

/**
* Returns the App component which is rendered.
*
* @param {boolean} isOfficeInitialized Checks whether office is intialized or not
* @returns {React.FC} React component
*/
const App = ({ isOfficeInitialized }) => {
const [activeNav, setActiveNav] = useState('library');
const [openOnStartup, setOpenOnStartup] = useState(false);
Expand All @@ -16,10 +22,22 @@ const App = ({ isOfficeInitialized }) => {
}
}, [isOfficeInitialized]);

/**
* Sets the active nav item in the navbar.
*
* @param {MouseEvent} event Mouse click event
* @param {object} obj An object
* @param {string} obj.name Name of the event
*/
const handleClick = (event, { name }) => {
setActiveNav(name);
};

/**
* Sets whether add-in should load by default on MS Word startup.
*
* @param {MouseEvent} event Mouseclick to see if checkbox is clicked
*/
const handleStartupState = event => {
Office.context.document.settings.set('Office.AutoShowTaskpaneWithDocument', event.target.checked);
setOpenOnStartup(event.target.checked);
Expand Down
44 changes: 44 additions & 0 deletions src/components/TemplateLibrary/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ import triggerClauseParse from '../../utils/TriggerClauseParse';
const CUSTOM_XML_NAMESPACE = 'https://accordproject.org/';
const XML_HEADER = '<?xml version="1.0" encoding="utf-8" ?>';

/**
* Template library Renderer.
*
* @returns {React.FC} Template library
*/
const LibraryComponent = () => {
const [templates, setTemplates] = useState(null);
const [overallCounter, setOverallCounter] = useState({});

useEffect(() => {
/**
* Loads the template library from https://templates.accordproject.org/ and stores them in the state.
*/
async function load() {
const templateLibrary = new TemplateLibrary();
const templateIndex = await templateLibrary
Expand All @@ -30,6 +38,11 @@ const LibraryComponent = () => {
load();
}, []);

/**
* Renders an uploaded template.
*
* @param {MouseEvent} event event containing the file object
*/
const onUploadTemplate = async event => {
const fileUploaded = event.target.files[0];
try {
Expand All @@ -43,6 +56,9 @@ const LibraryComponent = () => {
};

useEffect(() => {
/**
* Initializes the document by fetching the templates whose identifier is stored in CustomXMLPart.
*/
async function initializeDocument() {
Office.context.document.customXmlParts.getByNamespaceAsync(CUSTOM_XML_NAMESPACE, result => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
Expand Down Expand Up @@ -79,6 +95,13 @@ const LibraryComponent = () => {
}
}, [templates]);


/**
* Sets up a template for rendering.
*
* @param {object} ciceroMark Ciceromark JSON
* @param {object} template Template object
*/
const setup = async (ciceroMark, template) => {
await Word.run(async context => {
let counter = { ...overallCounter };
Expand Down Expand Up @@ -130,6 +153,12 @@ const LibraryComponent = () => {
});
};

/**
* Converts a template text to CiceroMark JSON.
*
* @param {object} template The template object
* @returns {object} CiceroMark JSON of a template
*/
const templateToCiceroMark = template => {
const sampleText = template.getMetadata().getSample();
const clause = new Clause(template);
Expand All @@ -138,6 +167,11 @@ const LibraryComponent = () => {
return ciceroMark;
};

/**
* Fetches templateIndex from https://templates.accordproject.org/, load the template, and save template details to CustomXML.
*
* @param {object} templateIndex Details of a particular template like URL, author, displayName, etc.
*/
const loadTemplateText = async templateIndex => {
// URL to compiled archive
const template = await Template.fromUrl(templateIndex.ciceroUrl);
Expand All @@ -147,6 +181,11 @@ const LibraryComponent = () => {
saveTemplateToXml(templateIdentifier);
};

/**
* Saves the template details to CustomXML.
*
* @param {string} templateIdentifier Identifier for a template
*/
const saveTemplateToXml = templateIdentifier => {
Office.context.document.customXmlParts.getByNamespaceAsync(CUSTOM_XML_NAMESPACE, result => {
if (result.status === Office.AsyncResultStatus.Succeeded) {
Expand Down Expand Up @@ -186,6 +225,11 @@ const LibraryComponent = () => {
});
};

/**
* Redirects to the template URL.
*
* @param {object} template Template object
*/
const goToTemplateDetail = template => {
const templateOrigin = new URL(template.url).origin;
const { name, version } = template;
Expand Down
6 changes: 5 additions & 1 deletion src/error/BadFile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import * as ReactDOM from 'react-dom';
import { Message } from 'semantic-ui-react';

import './index.css';

/**
* Renders a message when a bad file is uploaded.
*
* @returns {React.FC} Bad file component
*/
const BadFile = () => (
<Message negative className="message-container">
<Message.Header>Bad file uploaded</Message.Header>
Expand Down
8 changes: 6 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import * as React from 'react';
import * as ReactDOM from 'react-dom';

let isOfficeInitialized = false;

/**
* Renders a react component.
*
* @param {React.ReactDOM} Component React component to be rendered
*/
const render = Component => {
ReactDOM.render(
<AppContainer>
Expand All @@ -14,7 +18,7 @@ const render = Component => {
);
};

/* Render application after Office initializes */
/* Renders application after Office initializes */
Office.initialize = () => {
isOfficeInitialized = true;
render(App);
Expand Down
11 changes: 11 additions & 0 deletions src/utils/AttachVariableChangeListener.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Attaches listener to content controls so that similar instances of variables are updated simultaneously.
*
* @param {string} title Title of the variable
*/
const attachVariableChangeListener = title => {
Office.context.document.bindings.addFromNamedItemAsync(title, Office.CoercionType.Text, { id: title }, res => {
if (res.status === Office.AsyncResultStatus.Succeeded) {
Expand All @@ -18,6 +23,12 @@ const attachVariableChangeListener = title => {
});
};


/**
* Updates value of all the instance of variables with the same tags when one of them is manually modified.
*
* @param {Office.BindingDataChangedEventArgs} event Provides information about the binding that raised the DataChanged event
*/
const variableChangeListener = event => {
const { binding } = event;
// ID of the binding the user changed
Expand Down