@maniascript/mslint
Version:
ManiaScript linter
43 lines (42 loc) • 1.73 kB
JavaScript
import {} from '../linter/rule.js';
import { VariableDeclaration } from '@maniascript/parser';
export var Type;
(function (Type) {
Type["Explicite"] = "Explicite";
Type["Implicite"] = "Implicite";
})(Type || (Type = {}));
export const typeDeclaration = {
meta: {
id: 'type-declaration',
description: 'Require or forbid type in variable declarations',
recommended: true,
settings: {
type: 'Explicite'
}
},
create(context) {
const target = context.settings['type'] !== undefined ? context.settings['type'] : Type.Explicite;
return {
'VariableDeclaration:exit': (node) => {
if (node instanceof VariableDeclaration && !node.isLet) {
if (target === Type.Explicite) {
if (node.type === undefined) {
context.report(node, `Variable '${node.name.name}' must have an explicite type on declaration`);
}
}
else {
if (node.type !== undefined) {
if (node.initializerExpression === undefined &&
node.initializerType === undefined) {
context.report(node, `Variable '${node.name.name}' should be initialized to have an implicit type on declaration`);
}
else {
context.report(node, `Variable '${node.name.name}' should have an implicit type on declaration`);
}
}
}
}
}
};
}
};