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

GSoC'21: adding to FES phase 1 #5305

Merged
merged 20 commits into from Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from 17 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
70 changes: 70 additions & 0 deletions src/core/friendly_errors/browser_errors.js
Expand Up @@ -15,6 +15,16 @@ const strings = {
msg: "Can't find variable: {{}}",
type: 'NOTDEFINED',
browser: 'Safari'
},
{
msg: "Cannot access '{{.}}' before initialization",
type: 'CANNOTACCESS',
browser: 'Chrome'
},
{
msg: "can't access lexical declaration '{{.}}' before initialization",
type: 'CANNOTACCESS',
browser: 'Firefox'
}
],
SyntaxError: [
Expand Down Expand Up @@ -42,13 +52,73 @@ const strings = {
msg: "expected {{.}}, got '{{.}}'",
type: 'UNEXPECTEDTOKEN',
browser: 'Chrome'
},
{
msg: "Identifier '{{.}}' has already been declared",
type: 'REDECLAREDVARIABLE',
browser: 'Chrome'
},
{
msg: 'redeclaration of {} {{.}}',
type: 'REDECLAREDVARIABLE',
browser: 'Firefox'
},
{
msg: 'Missing initializer in const declaration',
type: 'MISSINGINITIALIZER',
browser: 'Chrome'
},
{
msg: 'missing = in const declaration',
type: 'MISSINGINITIALIZER',
browser: 'Firefox'
},
{
msg: 'Illegal return statement',
type: 'BADRETURNORYIELD',
browser: 'Chrome'
},
{
msg: 'return not in function',
type: 'BADRETURNORYIELD',
browser: 'Firefox'
lm-n marked this conversation as resolved.
Show resolved Hide resolved
}
],
TypeError: [
{
msg: '{{.}} is not a function',
type: 'NOTFUNC',
browser: 'all'
},
{
msg: "Cannot read property '{{.}}' of null",
type: 'READNULL',
browser: 'Chrome'
},
{
msg: '{{.}} is null',
type: 'READNULL',
browser: 'Firefox'
},
{
msg: "Cannot read property '{{.}}' of undefined",
type: 'READUDEFINED',
browser: 'Chrome'
},
{
msg: '{{.}} is undefined',
type: 'READUDEFINED',
browser: 'Firefox'
},
{
msg: 'Assignment to constant variable',
type: 'CONSTASSIGN',
browser: 'Chrome'
},
{
msg: "invalid assignment to const '{{.}}'",
type: 'CONSTASSIGN',
browser: 'Firefox'
}
]
};
Expand Down
135 changes: 135 additions & 0 deletions src/core/friendly_errors/fes_core.js
Expand Up @@ -712,6 +712,8 @@ if (typeof IS_MINIFIED !== 'undefined') {
// for syntax errors
switch (matchedError.type) {
case 'INVALIDTOKEN': {
//Error if there is an invalid or unexpected token that doesn't belong at this position in the code
//let x = “not a string”; -> string not in proper quotes
let url =
'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Illegal_character#What_went_wrong';
p5._friendlyError(
Expand All @@ -722,6 +724,8 @@ if (typeof IS_MINIFIED !== 'undefined') {
break;
}
case 'UNEXPECTEDTOKEN': {
//Error if a specific language construct(, { ; etc) was expected, but something else was provided
//for (let i = 0; i < 5,; ++i) -> a comma after i<5 instead of a semicolon
let url =
'https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Unexpected_token#What_went_wrong';
p5._friendlyError(
Expand All @@ -731,12 +735,57 @@ if (typeof IS_MINIFIED !== 'undefined') {
);
break;
}
case 'REDECLAREDVARIABLE': {
//Error if a variable is redeclared by the user. Example=>
//let a = 10;
//let a = 100;
let errSym = matchedError.match[1];
let url =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Redeclared_parameter#what_went_wrong';
p5._friendlyError(
translator('fes.globalErrors.syntax.redeclaredVariable', {
symbol: errSym,
url
})
);
break;
}
case 'MISSINGINITIALIZER': {
//Error if a const variable is not initialized during declaration
//Example => const a;
let url =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_initializer_in_const#what_went_wrong';
p5._friendlyError(
translator('fes.globalErrors.syntax.missingInitializer', {
url
})
);
break;
}
case 'BADRETURNORYIELD': {
//Error when a return statement is misplaced(usually outside of a function)
// const a = function(){
// .....
// }
// return; -> misplaced return statement
let url =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Bad_return_or_yield#what_went_wrong';
p5._friendlyError(
translator('fes.globalErrors.syntax.badReturnOrYield', {
url
})
);
break;
}
}
break;
}
case 'ReferenceError': {
switch (matchedError.type) {
case 'NOTDEFINED': {
//Error if there is a non-existent variable referenced somewhere
//let a = 10;
//console.log(x);
let errSym = matchedError.match[1];

if (errSym && handleMisspelling(errSym, error)) {
Expand All @@ -759,6 +808,26 @@ if (typeof IS_MINIFIED !== 'undefined') {
})
);

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
case 'CANNOTACCESS': {
//Error if a lexical variable was accessed before it was initialized
//console.log(a); -> variable accessed before it was initialized
//let a=100;
let errSym = matchedError.match[1];
let url =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_lexical_declaration_before_init#what_went_wrong';
p5._friendlyError(
translator('fes.globalErrors.reference.cannotAccess', {
url,
symbol: errSym,
location: locationObj
? translator('fes.location', locationObj)
: ''
})
);

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
Expand All @@ -769,6 +838,8 @@ if (typeof IS_MINIFIED !== 'undefined') {
case 'TypeError': {
switch (matchedError.type) {
case 'NOTFUNC': {
//Error when some code expects you to provide a function, but that didn't happen
//let a = document.getElementByID('foo'); -> getElementById instead of getElementByID
let errSym = matchedError.match[1];
let splitSym = errSym.split('.');
let url =
Expand Down Expand Up @@ -797,6 +868,70 @@ if (typeof IS_MINIFIED !== 'undefined') {
);
}

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
case 'READNULL': {
//Error if a property of null is accessed
//let a = null;
//console.log(a.property); -> a is null
let errSym = matchedError.match[1];
let url1 =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property#what_went_wrong';
let url2 =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/null';
p5._friendlyError(
translator('fes.globalErrors.type.readFromNull', {
url1,
url2,
symbol: errSym,
location: locationObj
? translator('fes.location', locationObj)
: ''
})
);

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
case 'READUDEFINED': {
//Error if a property of undefined is accessed
//let a; -> default value of a is undefined
//console.log(a.property); -> a is undefined
let errSym = matchedError.match[1];
let url1 =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Cant_access_property#what_went_wrong';
let url2 =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/undefined#description';
p5._friendlyError(
translator('fes.globalErrors.type.readFromUndefined', {
url1,
url2,
symbol: errSym,
location: locationObj
? translator('fes.location', locationObj)
: ''
})
);

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
case 'CONSTASSIGN': {
//Error when a const variable is reassigned a value
//const a = 100;
//a=10;
let url =
'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Invalid_const_assignment#what_went_wrong';
p5._friendlyError(
translator('fes.globalErrors.type.constAssign', {
url,
location: locationObj
? translator('fes.location', locationObj)
: ''
})
);

if (friendlyStack) printFriendlyStack(friendlyStack);
break;
}
Expand Down