js-slang
Version:
Javascript-based implementations of Source, written in Typescript
53 lines • 2.22 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isNamespaceSpecifier = exports.isVariableDeclaration = exports.isIdentifier = exports.isDeclaration = exports.isStatement = exports.isModuleDeclaration = exports.isDirective = exports.isImportDeclaration = void 0;
const isImportDeclaration = (node) => node.type === 'ImportDeclaration';
exports.isImportDeclaration = isImportDeclaration;
// It is necessary to write this type guard like this as the 'type' of both
// 'Directive' & 'ExpressionStatement' is 'ExpressionStatement'.
//
// export interface Directive extends BaseNode {
// type: "ExpressionStatement";
// expression: Literal;
// directive: string;
// }
//
// export interface ExpressionStatement extends BaseStatement {
// type: "ExpressionStatement";
// expression: Expression;
// }
//
// As such, we check whether the 'directive' property exists on the object
// instead in order to differentiate between the two.
const isDirective = (node) => {
return 'directive' in node;
};
exports.isDirective = isDirective;
const isModuleDeclaration = (node) => {
return [
'ImportDeclaration',
'ExportNamedDeclaration',
'ExportDefaultDeclaration',
'ExportAllDeclaration'
].includes(node.type);
};
exports.isModuleDeclaration = isModuleDeclaration;
const isStatement = (node) => {
return !(0, exports.isDirective)(node) && !(0, exports.isModuleDeclaration)(node);
};
exports.isStatement = isStatement;
function isDeclaration(node) {
// export type Declaration =
// FunctionDeclaration | VariableDeclaration | ClassDeclaration;
return (node.type === 'VariableDeclaration' ||
node.type === 'FunctionDeclaration' ||
node.type === 'ClassDeclaration');
}
exports.isDeclaration = isDeclaration;
const isIdentifier = (node) => node.type === 'Identifier';
exports.isIdentifier = isIdentifier;
const isVariableDeclaration = (node) => node.type === 'VariableDeclaration';
exports.isVariableDeclaration = isVariableDeclaration;
const isNamespaceSpecifier = (node) => node.type === 'ImportNamespaceSpecifier';
exports.isNamespaceSpecifier = isNamespaceSpecifier;
//# sourceMappingURL=typeGuards.js.map
;