Skip to content

Commit

Permalink
Fixed 'undefined' error message in mapper editor.
Browse files Browse the repository at this point in the history
Error was caused due to discrepancies between zone.js and monaco editor Promise.all implementation. Added polyfill patch for short term solution.

Track issues:
- microsoft/monaco-editor#790
- angular/zone.js#1077
  • Loading branch information
fcastill committed Aug 30, 2018
1 parent f9ede73 commit df2097e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 33 deletions.
48 changes: 30 additions & 18 deletions src/client/flogo/flow/shared/mapper-language/language-features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,15 @@ export class DiagnosticsAdapter {
if (!initialModel) {
return;
}
LanguageService.doValidation(initialModel.getValue()).then(diagnostics => {
const model = monaco.editor.getModel(resource);
if (model.getModeId() === languageId) {
const markers = diagnostics.map(d => toDiagnostics(resource, d));
monaco.editor.setModelMarkers(model, languageId, markers);
}
});
LanguageService
.doValidation(initialModel.getValue())
.then(diagnostics => {
const model = monaco.editor.getModel(resource);
if (model.getModeId() === languageId) {
const markers = diagnostics.map(d => toDiagnostics(resource, d));
monaco.editor.setModelMarkers(model, languageId, markers);
}
});

// this._worker(resource).then(worker => {
// return worker.doValidation(resource.toString()).then(diagnostics => {
Expand Down Expand Up @@ -117,23 +119,33 @@ function toDiagnostics(resource: Uri, diagnostic: RecognitionException|LexingErr
}
}

function parseExceptionToMarker(exception: RecognitionException) {
const marker = {
const tokenToPosition = (token: RecognitionException['token']) => ({
startLineNumber: token.startLine,
startColumn: token.startColumn,
endLineNumber: token.endLine,
endColumn: token.endColumn,
});
const missingToken = (token: RecognitionException['token']) => ({
startLineNumber: token.startLine,
startColumn: token.endColumn + 1,
endLineNumber: token.endLine,
endColumn: token.endColumn + 2,
});
function parseExceptionToMarker(exception: RecognitionException): monaco.editor.IMarkerData {
let positionInfo = tokenToPosition(exception.token);
//
if (isNaN(exception.token.startOffset) && (<any>exception).previousToken) {
positionInfo = missingToken((<any>exception).previousToken);
}
return {
code: exception.name,
severity: monaco.MarkerSeverity.Error,
message: exception.message,
startLineNumber: exception.token.startLine,
startColumn: exception.token.startColumn,
endLineNumber: exception.token.endLine,
endColumn: exception.token.endColumn,
...positionInfo,
};
if (isNaN(exception.token.startOffset)) {

}
return marker;
}

function lexErrorToMarker(resource: monaco.Uri, lexingError: LexingError) {
function lexErrorToMarker(resource: monaco.Uri, lexingError: LexingError): monaco.editor.IMarkerData {
const model = monaco.editor.getModel(resource);
const startPosition = model.getPositionAt(lexingError.offset);
const endPosition = model.getPositionAt(lexingError.offset + lexingError.length);
Expand Down
17 changes: 9 additions & 8 deletions src/client/flogo/flow/shared/mapper-language/language-service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { parse, RecognitionException, LexingError } from 'flogo-parser';

const getAsArray = (fromErrorsArr) => {
return !!fromErrorsArr ? fromErrorsArr : [];
};

export class LanguageService {
static doValidation(expression: string): Promise<Array<RecognitionException|LexingError>> {
if (!expression || !expression.trim()) {
return Promise.resolve(<any>[]);
}
const parseResult = parse(expression);
let errors = [];
if (parseResult.lexErrors && parseResult.lexErrors.length > 0) {
errors = errors.concat(parseResult.lexErrors);
}
if (parseResult.parseErrors && parseResult.parseErrors.length > 0) {
errors = errors.concat(parseResult.parseErrors);
}
return Promise.resolve(errors);
return Promise.resolve( [
...getAsArray(parseResult.lexErrors),
...getAsArray(parseResult.parseErrors),
]);
}
}
38 changes: 36 additions & 2 deletions src/client/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,43 @@ import 'web-animations-js'; // Run `npm install --save web-animations-js`.
*/
import 'zone.js/dist/zone'; // Included with Angular CLI.



/***************************************************************************************************
* APPLICATION IMPORTS
*/
(window as any).global = window;

// Patch to fix monaco-editor's promise (winJS) incompatibility with zone.js' promises
// See:
// - https://github.com/Microsoft/monaco-editor/issues/790
// - https://github.com/angular/zone.js/issues/1077
// tslint:disable - external code used as it is from: https://github.com/Microsoft/monaco-editor/issues/790#issuecomment-378452532
(window as any).Promise.all = function (values: any): Promise<any> {
let resolve: (v: any) => void;
let reject: (v: any) => void;
const promise = new this((res, rej) => {
resolve = res;
reject = rej;
});
let count = 0;
let index = 0;
const resolvedValues: any[] = [];
for (let value of values) {
if (!(value && value.then)) {
value = this.resolve(value);
}
value.then(
((index) => (value: any) => {
resolvedValues[index] = value;
count--;
if (!count) {
resolve(resolvedValues);
}
})(index),
reject);
count++;
index++;
}
if (!count) { resolve(resolvedValues); }
return promise;
}
// tslint:enable
10 changes: 5 additions & 5 deletions src/parser/lib/parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,21 @@ const StringLiteral = createToken({

const DblQuoteStringLiteral = createToken({
name: 'DblQuoteStringLiteral',
label: 'DblQuoteStringLiteral',
label: 'StringLiteral',
categories: StringLiteral,
pattern: /"(?:[^\\"]|\\(?:[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*"/,
});

const SingleQuoteStringLiteral = createToken({
name: 'SingleQuoteStringLiteral',
label: 'SingleQuoteStringLiteral',
label: 'StringLiteral',
categories: StringLiteral,
pattern: /'(?:[^\\']|\\(?:[bfnrtv'\\/]|u[0-9a-fA-F]{4}))*'/,
});

const NestedDblQuoteStringLiteral = createToken({
name: 'NestedDblQuoteStringLiteral',
label: 'NestedDblQuoteStringLiteral',
label: 'StringLiteral',
categories: StringLiteral,
pattern: /\\"(?:[^\\\\"]|\\\\(?:[bfnrtv"\\/]|u[0-9a-fA-F]{4}))*\\"/,
});
Expand All @@ -164,15 +164,15 @@ function matchStringTemplateOpen(text: string, startOffset?: number, tokens?: IT

const StringTemplateOpen = createToken({
name: 'StringTemplateOpen',
label: '"{{',
label: 'Open String template ("{{)',
pattern: { exec: matchStringTemplateOpen },
line_breaks: false,
push_mode: 'string_template'
});

const StringTemplateClose = createToken({
name: 'StringTemplateClose',
label: '}}"',
label: 'Closing StringTemplate (}}")',
pattern: /}}"/,
pop_mode: true,
});
Expand Down

0 comments on commit df2097e

Please sign in to comment.