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

feat: Convert ludown to bf-lu #1608

Merged
merged 16 commits into from
Dec 16, 2019
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';
import { LgFile } from '@bfc/indexers';
import { editor } from '@bfcomposer/monaco-editor/esm/vs/editor/editor.api';
import { lgIndexer, Diagnostic } from '@bfc/indexers';
import { lgIndexer, Diagnostic, combineMessage, isValid } from '@bfc/indexers';

import { StoreContext } from '../../store';
import * as lgUtil from '../../utils/lgUtil';

const { check, isValid, combineMessage } = lgIndexer;
const { check } = lgIndexer;

interface CodeEditorProps {
file: LgFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Nav, INavLinkGroup, INavLink } from 'office-ui-fabric-react/lib/Nav';
import { LGTemplate } from 'botbuilder-lg';
import { RouteComponentProps } from '@reach/router';
import get from 'lodash/get';
import { lgIndexer } from '@bfc/indexers';
import { isValid } from '@bfc/indexers';

import { LoadingSpinner } from '../../components/LoadingSpinner';
import { StoreContext } from '../../store';
Expand All @@ -35,9 +35,7 @@ const CodeEditor = React.lazy(() => import('./code-editor'));
const LGPage: React.FC<RouteComponentProps> = props => {
const { state } = useContext(StoreContext);
const { lgFiles, dialogs } = state;
const [editMode, setEditMode] = useState(
lgFiles.filter(file => lgIndexer.isValid(file.diagnostics) === false).length > 0
);
const [editMode, setEditMode] = useState(lgFiles.filter(file => !isValid(file.diagnostics)).length > 0);
const [fileValid, setFileValid] = useState(true);
const [inlineTemplate, setInlineTemplate] = useState<null | lgUtil.Template>(null);
const [line, setLine] = useState<number>(0);
Expand Down Expand Up @@ -110,7 +108,7 @@ const LGPage: React.FC<RouteComponentProps> = props => {

useEffect(() => {
const errorFiles = lgFiles.filter(file => {
return lgIndexer.isValid(file.diagnostics) === false;
return !isValid(file.diagnostics);
});
const hasError = errorFiles.length !== 0;
setFileValid(hasError === false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import formatMessage from 'format-message';
import { NeutralColors, FontSizes } from '@uifabric/fluent-theme';
import { DialogInfo, LgFile } from '@bfc/indexers';
import { LGTemplate, LGParser } from 'botbuilder-lg';
import { lgIndexer } from '@bfc/indexers';
import { isValid } from '@bfc/indexers';
import get from 'lodash/get';

import { StoreContext } from '../../store';
Expand All @@ -44,7 +44,7 @@ const TableView: React.FC<TableViewProps> = props => {
useEffect(() => {
if (isEmpty(lgFile)) return;
let allTemplates: LGTemplate[] = [];
if (lgIndexer.isValid(lgFile.diagnostics) === true) {
if (isValid(lgFile.diagnostics)) {
const resource = LGParser.parse(lgFile.content, '');
allTemplates = get(resource, 'templates', []);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { LuEditor } from '@bfc/code-editor';
import get from 'lodash/get';
import debounce from 'lodash/debounce';
import isEmpty from 'lodash/isEmpty';

import * as luUtil from '../../utils/luUtil';
import { combineMessage, isValid } from '@bfc/indexers';

export default function CodeEditor(props) {
const { file, errorMsg: updateErrorMsg } = props;
Expand All @@ -34,8 +33,8 @@ export default function CodeEditor(props) {

// diagnostics is load file error,
// updateErrorMsg is save file return error.
const isInvalid = !luUtil.isValid(diagnostics) || updateErrorMsg !== '';
const errorMsg = isInvalid ? `${luUtil.combineMessage(diagnostics)}\n ${updateErrorMsg}` : '';
const isInvalid = !isValid(file.diagnostics) || updateErrorMsg !== '';
const errorMsg = isInvalid ? `${combineMessage(diagnostics)}\n ${updateErrorMsg}` : '';

return (
<LuEditor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import { ScrollablePane, ScrollbarVisibility } from 'office-ui-fabric-react/lib/
import { Sticky, StickyPositionType } from 'office-ui-fabric-react/lib/Sticky';
import formatMessage from 'format-message';
import { NeutralColors, FontSizes } from '@uifabric/fluent-theme';
import { isValid, combineMessage } from '@bfc/indexers';

import { OpenConfirmModal, DialogStyle } from '../../components/Modal';
import { StoreContext } from '../../store';
import * as luUtil from '../../utils/luUtil';
import { navigateTo } from '../../utils';

import { formCell, luPhraseCell } from './styles';
Expand Down Expand Up @@ -71,9 +71,7 @@ export default function TableView(props) {
}, [luFiles, activeDialog]);

function checkErrors(files) {
return files.filter(file => {
return luUtil.isValid(file.diagnostics) === false;
});
return files.filter(file => !isValid(file.diagnostics));
}

function getIntentState(file) {
Expand All @@ -90,7 +88,7 @@ export default function TableView(props) {

async function showErrors(files) {
for (const file of files) {
const errorMsg = luUtil.combineMessage(file.diagnostics);
const errorMsg = combineMessage(file.diagnostics);
const errorTitle = formatMessage('There was a problem parsing {fileId}.lu file.', { fileId: file.id });
const confirmed = await OpenConfirmModal(errorTitle, errorMsg, {
style: DialogStyle.Console,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Licensed under the MIT License.

import { useContext, useMemo } from 'react';
import { lgIndexer } from '@bfc/indexers';
import { createSingleMessage } from '@bfc/indexers';

import { StoreContext } from '../../store';
import { replaceDialogDiagnosticLabel } from '../../utils';
Expand Down Expand Up @@ -35,7 +35,7 @@ export default function useNotifications(filter?: string) {
notifactions.push({
type: 'lu',
location,
message: diagnostic.text,
message: createSingleMessage(diagnostic),
severity: 'Error',
diagnostic,
id: lufile.id,
Expand All @@ -49,7 +49,7 @@ export default function useNotifications(filter?: string) {
type: 'lg',
severity: DiagnosticSeverity[diagnostic.severity] || '',
location,
message: lgIndexer.createSingleMessage(diagnostic),
message: createSingleMessage(diagnostic),
diagnostic,
id: lgFile.id,
});
Expand Down
6 changes: 3 additions & 3 deletions Composer/packages/client/src/utils/lgUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
*/

import { LGParser, LGTemplate } from 'botbuilder-lg';
import { lgIndexer } from '@bfc/indexers';
import { lgIndexer, combineMessage, isValid } from '@bfc/indexers';

const { check, isValid, combineMessage, parse } = lgIndexer;
const { check, parse } = lgIndexer;
export interface Template {
name: string;
parameters?: string[];
Expand All @@ -20,7 +20,7 @@ export interface Template {
export function checkLgContent(content: string, id: string) {
// check lg content, make up error message
const diagnostics = check(content, id);
if (isValid(diagnostics) === false) {
if (!isValid(diagnostics)) {
const errorMsg = combineMessage(diagnostics);
throw new Error(errorMsg);
}
Expand Down
13 changes: 1 addition & 12 deletions Composer/packages/client/src/utils/luUtil.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { LuFile, DialogInfo, LuDiagnostic } from '@bfc/indexers';
import { LuFile, DialogInfo } from '@bfc/indexers';

export function getReferredFiles(luFiles: LuFile[], dialogs: DialogInfo[]) {
return luFiles.filter(file => {
Expand All @@ -11,14 +11,3 @@ export function getReferredFiles(luFiles: LuFile[], dialogs: DialogInfo[]) {
return false;
});
}

export function isValid(diagnostics: LuDiagnostic[]) {
return diagnostics.length === 0;
}

export function combineMessage(diagnostics: LuDiagnostic[]) {
return diagnostics.reduce((msg, d) => {
msg += `${d.text}\n`;
return msg;
}, '');
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import React, { useState, useEffect } from 'react';
import { LuEditor } from '@bfc/code-editor';
import { LuFile } from '@bfc/indexers';
import { LuFile, combineMessage } from '@bfc/indexers';

interface InlineLuEditorProps {
file: LuFile;
Expand All @@ -18,11 +18,7 @@ const InlineLuEditor: React.FC<InlineLuEditorProps> = props => {

useEffect(() => {
setLocalContent(file.content || '');
const errorMsgText = file.diagnostics
.map(item => {
return item.text;
})
.join('\n');
const errorMsgText = combineMessage(file.diagnostics);

setLocalErrorMsg(errorMsgText || errorMsg || '');
}, [file]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FieldProps } from '@bfcomposer/react-jsonschema-form';
import { Dropdown, ResponsiveMode, IDropdownOption } from 'office-ui-fabric-react/lib/Dropdown';
import { Spinner, SpinnerSize } from 'office-ui-fabric-react/lib/Spinner';
import { MicrosoftIRecognizer } from '@bfc/shared';
import { LuFile } from '@bfc/indexers';
import { LuFile, combineMessage } from '@bfc/indexers';

import { BaseField } from '../BaseField';
import { LoadingSpinner } from '../../../LoadingSpinner';
Expand Down Expand Up @@ -39,9 +39,7 @@ export const RecognizerField: React.FC<FieldProps<MicrosoftIRecognizer>> = props
//make the inline editor show error message
useEffect(() => {
if (selectedFile && selectedFile.diagnostics.length > 0) {
const msg = selectedFile.diagnostics.reduce((msg: string, diagnostic) => {
return (msg += `${diagnostic.text}\n`);
}, '');
const msg = combineMessage(selectedFile.diagnostics);
setErrorMsg(msg);
} else {
setErrorMsg('');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

import { createSingleMessage, combineMessage, findErrors, isValid } from '../../src/utils';

const diagnostics = [
{
message: 'syntax error',
range: { start: { line: 15, character: 0 }, end: { line: 15, character: 1 } },
severity: 0,
source: 'a.lu',
},
{
message: 'syntax error',
range: { start: { line: 0, character: 0 }, end: { line: 0, character: 1 } },
severity: 0,
source: 'b.lu',
},
{
message: 'syntax error',
range: { start: { line: 1, character: 0 }, end: { line: 1, character: 1 } },
severity: 0,
source: 'c.lu',
},
{
message: 'file empty',
range: { start: { line: 2, character: 0 }, end: { line: 2, character: 1 } },
severity: 1,
source: 'd.lu',
},
];

const diagnostics1 = [
{
message: 'file empty',
range: { start: { line: 2, character: 0 }, end: { line: 2, character: 1 } },
severity: 1,
source: 'd.lu',
},
];

describe('diagnostic utils', () => {
it('should check if the diagnostics have errors', () => {
expect(isValid(diagnostics)).toBe(false);
expect(isValid(diagnostics1)).toBe(true);
});
it('should find all errors', () => {
expect(findErrors(diagnostics).length).toBe(3);
expect(findErrors(diagnostics1).length).toBe(0);
});
it('should create a message for single diagnostic', () => {
expect(createSingleMessage(diagnostics[0])).toContain('line 15:0 - line 15:1');
expect(createSingleMessage(diagnostics[1])).toContain('line 0:0 - line 0:1');
});
it('should combine all error message', () => {
const result = combineMessage(diagnostics);
expect(result).toContain('line 15:0 - line 15:1');
expect(result).toContain('line 0:0 - line 0:1');
expect(result).toContain('line 1:0 - line 1:1');
expect(result).toContain('line 2:0 - line 2:1');
});
});
4 changes: 2 additions & 2 deletions Composer/packages/lib/indexers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
"ts-jest": "^24.1.0"
},
"dependencies": {
"@bfcomposer/bf-lu": "1.1.2",
"botbuilder-lg": "4.7.0-preview.93464",
"botframework-expressions": "4.7.0-preview.93464",
"lodash": "^4.17.15",
"ludown": "^1.3.4"
"lodash": "^4.17.15"
}
}
2 changes: 2 additions & 0 deletions Composer/packages/lib/indexers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

export * from './dialogIndexer';
export * from './lgIndexer';
export * from './luIndexer';
export * from './type';
export * from './diagnostic';
export * from './lgUtils';
export * from './dialogUtils';
export * from './utils';
24 changes: 0 additions & 24 deletions Composer/packages/lib/indexers/src/lgIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ function parse(content: string, id?: string): LgTemplate[] {
return templates;
}

function isValid(diagnostics: Diagnostic[]): boolean {
return diagnostics.every(d => d.severity !== DiagnosticSeverity.Error);
}

function index(files: FileInfo[]): LgFile[] {
if (files.length === 0) return [];
const lgFiles: LgFile[] = [];
Expand All @@ -74,28 +70,8 @@ function index(files: FileInfo[]): LgFile[] {
return lgFiles;
}

function createSingleMessage(d: Diagnostic): string {
let msg = `${d.message}\n`;
if (d.range) {
const { start, end } = d.range;
const position = `line ${start.line}:${start.character} - line ${end.line}:${end.character}`;
msg += `${position} \n ${msg}`;
}
return msg;
}

function combineMessage(diagnostics: Diagnostic[]): string {
return diagnostics.reduce((msg, d) => {
msg += createSingleMessage(d);
return msg;
}, '');
}

export const lgIndexer = {
index,
parse,
check,
isValid,
createSingleMessage,
combineMessage,
};
Loading