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

Prevent usage of reserved words in grammars #749

Merged
merged 3 commits into from
Nov 7, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 117 additions & 5 deletions packages/langium/src/grammar/langium-grammar-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Utils } from 'vscode-uri';
import { NamedAstNode } from '../references/name-provider';
import { References } from '../references/references';
import { LangiumServices } from '../services';
import { AstNode, Reference } from '../syntax-tree';
import { AstNode, Properties, Reference } from '../syntax-tree';
import { getContainerOfType, getDocument, streamAllContents } from '../utils/ast-util';
import { MultiMap } from '../utils/collections';
import { toDocumentSegment } from '../utils/cst-util';
Expand All @@ -29,21 +29,27 @@ export class LangiumGrammarValidationRegistry extends ValidationRegistry {
super(services);
const validator = services.validation.LangiumGrammarValidator;
const checks: ValidationChecks<ast.LangiumGrammarAstType> = {
Action: validator.checkActionTypeUnions,
Action: [
validator.checkActionTypeUnions,
validator.checkAssignmentReservedName
],
AbstractRule: validator.checkRuleName,
Assignment: [
validator.checkAssignmentWithFeatureName,
validator.checkAssignmentToFragmentRule
validator.checkAssignmentToFragmentRule,
validator.checkAssignmentReservedName
],
ParserRule: [
validator.checkParserRuleDataType,
validator.checkRuleParametersUsed
validator.checkRuleParametersUsed,
validator.checkParserRuleReservedName,
],
TerminalRule: [
validator.checkTerminalRuleReturnType,
validator.checkHiddenTerminalRule,
validator.checkEmptyTerminalRule
],
InferredType: validator.checkTypeReservedName,
Keyword: validator.checkKeyword,
UnorderedGroup: validator.checkUnorderedGroup,
Grammar: [
Expand All @@ -63,6 +69,9 @@ export class LangiumGrammarValidationRegistry extends ValidationRegistry {
],
GrammarImport: validator.checkPackageImport,
CharacterRange: validator.checkInvalidCharacterRange,
Interface: validator.checkTypeReservedName,
Type: validator.checkTypeReservedName,
TypeAttribute: validator.checkTypeReservedName,
RuleCall: [
validator.checkUsedHiddenTerminalRule,
validator.checkUsedFragmentTerminalRule,
Expand Down Expand Up @@ -548,11 +557,39 @@ export class LangiumGrammarValidator {
if (rule.name && !isEmptyRule(rule)) {
const firstChar = rule.name.substring(0, 1);
if (firstChar.toUpperCase() !== firstChar) {
accept('warning', 'Rule name should start with an upper case letter.', { node: rule, property: 'name', code: IssueCodes.RuleNameUppercase });
accept('warning', 'Rule name should start with an upper case letter.', {
node: rule,
property: 'name',
code: IssueCodes.RuleNameUppercase
});
}
}
}

checkTypeReservedName(type: ast.Interface | ast.TypeAttribute | ast.Type | ast.InferredType, accept: ValidationAcceptor): void {
this.checkReservedName(type, 'name', accept);
}

checkAssignmentReservedName(assignment: ast.Assignment | ast.Action, accept: ValidationAcceptor): void {
this.checkReservedName(assignment, 'feature', accept);
}

checkParserRuleReservedName(rule: ast.ParserRule, accept: ValidationAcceptor): void {
if (!rule.inferredType) {
this.checkReservedName(rule, 'name', accept);
}
}

private checkReservedName<N extends AstNode>(node: N, property: Properties<N>, accept: ValidationAcceptor): void {
const name = node[property as keyof N];
if (typeof name === 'string' && reservedNames.has(name)) {
accept('error', `'${name}' is a reserved name of the JavaScript runtime.`, {
node,
property
});
}
}

checkKeyword(keyword: ast.Keyword, accept: ValidationAcceptor): void {
if (keyword.value.length === 0) {
accept('error', 'Keywords cannot be empty.', { node: keyword });
Expand Down Expand Up @@ -684,3 +721,78 @@ function isPrimitiveType(type: string): boolean {
function isEmptyRule(rule: ast.AbstractRule): boolean {
return !rule.definition || !rule.definition.$cstNode || rule.definition.$cstNode.length === 0;
}

const reservedNames = new Set([
// Built-in objects, properties and methods
// Collections
'Array',
'Int8Array',
'Uint8Array',
'Uint8ClampedArray',
'Int16Array',
'Uint16Array',
'Int32Array',
'Uint32Array',
'Float32Array',
'Float64Array',
'BigInt64Array',
'BigUint64Array',
// Keyed collections
'Map',
'Set',
'WeakMap',
'WeakSet',
// Errors
'Error',
'AggregateError',
'EvalError',
'InternalError',
'RangeError',
'ReferenceError',
'SyntaxError',
'TypeError',
'URIError',
// Primitives
'BigInt',
'RegExp',
'Number',
'Object',
'Function',
'Symbol',
'String',
// Math
'Math',
'NaN',
'Infinity',
'isFinite',
'isNaN',
// Structured data
'Buffer',
'ArrayBuffer',
'SharedArrayBuffer',
'Atomics',
'DataView',
'JSON',
'globalThis',
'decodeURIComponent',
'decodeURI',
'encodeURIComponent',
'encodeURI',
'parseInt',
'parseFloat',
// Control abstraction
'Promise',
'Generator',
'GeneratorFunction',
'AsyncFunction',
'AsyncGenerator',
'AsyncGeneratorFunction',
// Reflection
'Reflect',
'Proxy',
// Others
'Date',
'Intl',
'eval',
'undefined'
]);
Loading