本文整理匯總了TypeScript中@babel/types.isIdentifier函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript isIdentifier函數的具體用法?TypeScript isIdentifier怎麽用?TypeScript isIdentifier使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了isIdentifier函數的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: matchesCallExpression
export function matchesCallExpression(
expression: babel.MemberExpression, path: string[]): boolean {
if (!expression.property || !expression.object) {
return false;
}
assert(path.length >= 2);
if (!babel.isIdentifier(expression.property)) {
return false;
}
// Unravel backwards, make sure properties match each step of the way.
if (expression.property.name !== path[path.length - 1]) {
return false;
}
// We've got ourselves a final member expression.
if (path.length === 2 && babel.isIdentifier(expression.object)) {
return expression.object.name === path[0];
}
// Nested expressions.
if (path.length > 2 && babel.isMemberExpression(expression.object)) {
return matchesCallExpression(
expression.object, path.slice(0, path.length - 1));
}
return false;
}
示例2: isVariable
export function isVariable(node: Expression): node is VariableAST {
return (
isCallExpression(node) &&
isMemberExpression(node.callee, { computed: false }) &&
isIdentifier(node.callee.object, { name: 'Variable' }) &&
isIdentifier(node.callee.property, { name: 'find' }) &&
isIdentifier(node.arguments[0], { name: 'gameModel' }) &&
isStringLiteral(node.arguments[1])
);
}
示例3: toMethodParam
export function toMethodParam(
nodeParam: babel.LVal, jsdocAnn?: jsdoc.Annotation): MethodParam {
const paramTags = new Map<string, doctrine.Tag>();
let name;
let defaultValue;
let rest;
if (jsdocAnn) {
for (const tag of (jsdocAnn.tags || [])) {
if (tag.title === 'param' && tag.name) {
paramTags.set(tag.name, tag);
}
}
}
if (babel.isIdentifier(nodeParam)) {
// Basic parameter: method(param)
name = nodeParam.name;
} else if (
babel.isRestElement(nodeParam) &&
babel.isIdentifier(nodeParam.argument)) {
// Rest parameter: method(...param)
name = nodeParam.argument.name;
rest = true;
} else if (
babel.isAssignmentPattern(nodeParam) &&
babel.isIdentifier(nodeParam.left)) {
// Parameter with a default: method(param = "default")
name = nodeParam.left.name;
defaultValue = generate(nodeParam.right).code;
} else {
// Some AST pattern we don't recognize. Hope the code generator does
// something reasonable.
name = generate(nodeParam).code;
}
let type;
let description;
const tag = paramTags.get(name);
if (tag) {
if (tag.type) {
type = doctrine.type.stringify(tag.type);
}
if (tag.description) {
description = tag.description;
}
}
const param: MethodParam = {name, type, defaultValue, rest, description};
return param;
}
示例4: getClosureType
export function getClosureType(
node: babel.Node,
parsedJsdoc: doctrine.Annotation|undefined,
sourceRange: SourceRange,
document: ParsedDocument): Result<string, Warning> {
if (parsedJsdoc) {
const typeTag = jsdoc.getTag(parsedJsdoc, 'type');
if (typeTag) {
return {successful: true, value: doctrine.type.stringify(typeTag.type!)};
}
}
const type = VALID_EXPRESSION_TYPES.get(node.type);
if (type) {
return {successful: true, value: type};
}
if (babel.isIdentifier(node)) {
return {
successful: true,
value: CLOSURE_CONSTRUCTOR_MAP.get(node.name) || node.name
};
}
const warning = new Warning({
code: 'no-closure-type',
message: `Unable to determine closure type for expression of type ` +
`${node.type}`,
severity: Severity.WARNING,
sourceRange,
parsedDocument: document,
});
return {successful: false, error: warning};
}
示例5: isVariableCall
export function isVariableCall(node: Expression): node is VariableCallAST {
return (
isCallExpression(node) &&
isMemberExpression(node.callee, { computed: false }) &&
isVariable(node.callee.object) &&
isIdentifier(node.callee.property)
);
}
示例6: extractGlobalMethod
export function extractGlobalMethod(node: CallExpression) {
let ret: Identifier[] = [];
let depth = node.callee;
while (isMemberExpression(depth)) {
if (!isIdentifier(depth.property)) {
throw Error('Unhandled');
}
ret.push(depth.property);
depth = depth.object;
}
if (!isIdentifier(depth)) {
throw Error('Unhandled');
}
ret.push(depth);
return ret
.reverse()
.map(i => i.name)
.join('.');
}
示例7: getPropertyName
export function getPropertyName(prop: PropertyOrMethod): string|undefined {
const key = prop.key;
// {foo: bar} // note that `foo` is not quoted, so it's an identifier
if (!prop.computed && babel.isIdentifier(key)) {
return key.name;
}
// Otherwise, try to statically evaluate the expression
const keyValue = astValue.expressionToValue(key);
if (keyValue !== undefined) {
return '' + keyValue;
}
return undefined;
}
示例8: analyzeProperties
export function analyzeProperties(
node: babel.Node, document: JavaScriptDocument): ScannedPolymerProperty[] {
const analyzedProps: ScannedPolymerProperty[] = [];
if (!babel.isObjectExpression(node)) {
return analyzedProps;
}
for (const property of esutil.getSimpleObjectProperties(node)) {
const prop = toScannedPolymerProperty(
property, document.sourceRangeForNode(property)!, document);
if (prop === undefined) {
continue;
}
// toScannedPolymerProperty does the wrong thing for us with type. We want
// type to be undefined unless there's a positive signal for the type.
// toScannedPolymerProperty will give Object because it infers based on the
// property declaration.
prop.type = undefined;
const typeTag = jsdoc.getTag(prop.jsdoc, 'type');
if (typeTag) {
prop.type =
typeTag.type ? doctrine.type.stringify(typeTag.type) : undefined;
}
prop.published = true;
let isComputed = false;
const value = property.value;
if (babel.isIdentifier(value)) {
// Polymer supports this simple syntax, where only the attribute
// deserializer is specified.
prop.attributeType = value.name;
} else if (!babel.isObjectExpression(value)) {
continue;
} else {
/**
* Parse the expression inside a property object block. e.g.
* property: {
* key: {
* type: String,
* notify: true,
* value: -1,
* readOnly: true,
* reflectToAttribute: true
* }
* }
*/
for (const propertyArg of esutil.getSimpleObjectProperties(value)) {
const propertyKey = esutil.getPropertyName(propertyArg);
switch (propertyKey) {
case 'type':
prop.attributeType = astValue.getIdentifierName(propertyArg.value);
if (prop.attributeType === undefined && prop.type === undefined) {
prop.warnings.push(new Warning({
code: 'invalid-property-type',
message: 'Invalid type in property object.',
severity: Severity.WARNING,
sourceRange: document.sourceRangeForNode(propertyArg)!,
parsedDocument: document
}));
}
break;
case 'notify':
prop.notify = !!astValue.expressionToValue(propertyArg.value);
break;
case 'observer':
const val = astValue.expressionToValue(propertyArg.value);
prop.observerNode = propertyArg.value;
const parseResult = parseExpressionInJsStringLiteral(
document, propertyArg.value, 'identifierOnly');
prop.warnings.push(...parseResult.warnings);
prop.observerExpression = parseResult.databinding;
if (val === undefined) {
prop.observer = astValue.CANT_CONVERT;
} else {
prop.observer = JSON.stringify(val);
}
break;
case 'readOnly':
prop.readOnly = !!astValue.expressionToValue(propertyArg.value);
break;
case 'reflectToAttribute':
prop.reflectToAttribute =
!!astValue.expressionToValue(propertyArg.value);
break;
case 'computed':
isComputed = true;
const computedParseResult = parseExpressionInJsStringLiteral(
document, propertyArg.value, 'callExpression');
prop.warnings.push(...computedParseResult.warnings);
prop.computedExpression = computedParseResult.databinding;
break;
case 'value':
prop.default =
JSON.stringify(astValue.expressionToValue(propertyArg.value));
//.........這裏部分代碼省略.........
示例9: declarationPropertyHandlers
export function declarationPropertyHandlers(
declaration: ScannedPolymerElement,
document: JavaScriptDocument,
path: NodePath): PropertyHandlers {
return {
is(node: babel.Node) {
if (babel.isLiteral(node)) {
declaration.tagName = '' + astValue.expressionToValue(node);
}
},
properties(node: babel.Node) {
for (const prop of analyzeProperties(node, document)) {
declaration.addProperty(prop);
}
},
behaviors(node: babel.Node) {
if (!babel.isArrayExpression(node)) {
return;
}
for (const element of node.elements) {
const result = getBehaviorReference(element, document, path);
if (result.successful === false) {
declaration.warnings.push(result.error);
} else {
declaration.behaviorAssignments.push(result.value);
}
}
},
observers(node: babel.Node) {
const observers = extractObservers(node, document);
if (!observers) {
return;
}
declaration.warnings = declaration.warnings.concat(observers.warnings);
declaration.observers = declaration.observers.concat(observers.observers);
},
listeners(node: babel.Node) {
if (!babel.isObjectExpression(node)) {
declaration.warnings.push(new Warning({
code: 'invalid-listeners-declaration',
message: '`listeners` property should be an object expression',
severity: Severity.WARNING,
sourceRange: document.sourceRangeForNode(node)!,
parsedDocument: document
}));
return;
}
for (const p of getSimpleObjectProperties(node)) {
const evtName =
babel.isLiteral(p.key) && astValue.expressionToValue(p.key) ||
babel.isIdentifier(p.key) && p.key.name;
const handler =
!babel.isLiteral(p.value) || astValue.expressionToValue(p.value);
if (typeof evtName !== 'string' || typeof handler !== 'string') {
// TODO (maklesoft): Notifiy the user somehow that a listener entry
// was not extracted
// because the event or handler namecould not be statically analyzed.
// E.g. add a low-severity
// warning once opting out of rules is supported.
continue;
}
declaration.listeners.push({event: evtName, handler: handler});
}
}
};
}
示例10: Identifier
Identifier(path) {
console.log("Visiting: " + path.node.name);
}
};
// Example from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#babel-traverse
const code = `function square(n) {
return n * n;
}`;
const ast = parse(code);
traverse(ast, {
enter(path) {
const node = path.node;
if (t.isIdentifier(node) && node.name === "n") {
node.name = "x";
}
}
});
// Examples from https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#writing-your-first-babel-plugin
const v1: Visitor = {
BinaryExpression(path) {
if (t.isIdentifier(path.node.left)) {
// ...
}
path.replaceWith(
t.binaryExpression("**", path.node.left, t.numericLiteral(2))
);