hermes-parser
Advanced tools
Comparing version 0.18.1 to 0.18.2
@@ -81,3 +81,3 @@ /** | ||
function createPropsTypeAnnotation(loc, range) { | ||
function createPropsTypeAnnotation(propTypes, spread, loc, range) { | ||
// Create empty loc for type annotation nodes | ||
@@ -91,4 +91,30 @@ const createParamsTypeLoc = () => ({ | ||
parent: EMPTY_PARENT | ||
}); | ||
}); // Optimize `{...Props}` -> `Props` | ||
if (spread != null && propTypes.length === 0) { | ||
return { | ||
type: 'TypeAnnotation', | ||
typeAnnotation: spread.argument, | ||
...createParamsTypeLoc() | ||
}; | ||
} | ||
const typeProperties = [...propTypes]; | ||
if (spread != null) { | ||
// Spread needs to be the first type, as inline properties take precedence. | ||
typeProperties.unshift(spread); | ||
} | ||
const propTypeObj = { | ||
type: 'ObjectTypeAnnotation', | ||
callProperties: [], | ||
properties: typeProperties, | ||
indexers: [], | ||
internalSlots: [], | ||
exact: false, | ||
inexact: false, | ||
...createParamsTypeLoc() | ||
}; | ||
return { | ||
@@ -107,12 +133,3 @@ type: 'TypeAnnotation', | ||
type: 'TypeParameterInstantiation', | ||
params: [{ | ||
type: 'ObjectTypeAnnotation', | ||
callProperties: [], | ||
properties: [], | ||
indexers: [], | ||
internalSlots: [], | ||
exact: false, | ||
inexact: true, | ||
...createParamsTypeLoc() | ||
}], | ||
params: [propTypeObj], | ||
...createParamsTypeLoc() | ||
@@ -136,9 +153,5 @@ }, | ||
if (params.length === 1 && params[0].type === 'RestElement' && params[0].argument.type === 'Identifier') { | ||
var _restElementArgument$, _restElementArgument$2; | ||
const restElementArgument = params[0].argument; | ||
return { | ||
props: nodeWith(restElementArgument, { | ||
typeAnnotation: createPropsTypeAnnotation((_restElementArgument$ = restElementArgument.typeAnnotation) == null ? void 0 : _restElementArgument$.loc, (_restElementArgument$2 = restElementArgument.typeAnnotation) == null ? void 0 : _restElementArgument$2.range) | ||
}), | ||
props: restElementArgument, | ||
ref: null | ||
@@ -158,2 +171,20 @@ }; | ||
}); | ||
const [propTypes, spread] = paramsWithoutRef.reduce(([propTypes, spread], param) => { | ||
switch (param.type) { | ||
case 'RestElement': | ||
{ | ||
if (spread != null) { | ||
throw (0, _createSyntaxError.createSyntaxError)(param, `Invalid state, multiple rest elements found as Component Parameters`); | ||
} | ||
return [propTypes, mapComponentParameterRestElementType(param)]; | ||
} | ||
case 'ComponentParameter': | ||
{ | ||
propTypes.push(mapComponentParameterType(param)); | ||
return [propTypes, spread]; | ||
} | ||
} | ||
}, [[], null]); | ||
const propsProperties = paramsWithoutRef.flatMap(mapComponentParameter); | ||
@@ -164,3 +195,3 @@ let props = null; | ||
if (refParam == null) { | ||
throw new Error('TransformReactScriptForBabel: Invalid state, ref should always be set at this point if props are empty'); | ||
throw new Error('StripComponentSyntax: Invalid state, ref should always be set at this point if props are empty'); | ||
} | ||
@@ -178,3 +209,3 @@ | ||
optional: false, | ||
typeAnnotation: createPropsTypeAnnotation(emptyParamsLoc, emptyParamsRange), | ||
typeAnnotation: createPropsTypeAnnotation([], null, emptyParamsLoc, emptyParamsRange), | ||
loc: emptyParamsLoc, | ||
@@ -189,3 +220,3 @@ range: emptyParamsRange, | ||
properties: propsProperties, | ||
typeAnnotation: createPropsTypeAnnotation({ | ||
typeAnnotation: createPropsTypeAnnotation(propTypes, spread, { | ||
start: lastPropsProperty.loc.end, | ||
@@ -215,2 +246,49 @@ end: lastPropsProperty.loc.end | ||
function mapComponentParameterType(param) { | ||
var _typeAnnotation$typeA; | ||
const typeAnnotation = param.local.type === 'AssignmentPattern' ? param.local.left.typeAnnotation : param.local.typeAnnotation; | ||
const optional = param.local.type === 'AssignmentPattern' ? true : param.local.type === 'Identifier' ? param.local.optional : false; | ||
return { | ||
type: 'ObjectTypeProperty', | ||
key: (0, _astNodeMutationHelpers.shallowCloneNode)(param.name), | ||
value: (_typeAnnotation$typeA = typeAnnotation == null ? void 0 : typeAnnotation.typeAnnotation) != null ? _typeAnnotation$typeA : { | ||
type: 'AnyTypeAnnotation', | ||
loc: param.local.loc, | ||
range: param.local.range, | ||
parent: EMPTY_PARENT | ||
}, | ||
kind: 'init', | ||
optional, | ||
method: false, | ||
static: false, | ||
proto: false, | ||
variance: null, | ||
loc: param.local.loc, | ||
range: param.local.range, | ||
parent: EMPTY_PARENT | ||
}; | ||
} | ||
function mapComponentParameterRestElementType(param) { | ||
var _param$argument$typeA, _param$argument$typeA2; | ||
if (param.argument.type !== 'Identifier' && param.argument.type !== 'ObjectPattern') { | ||
throw (0, _createSyntaxError.createSyntaxError)(param, `Invalid ${param.argument.type} encountered in restParameter`); | ||
} | ||
return { | ||
type: 'ObjectTypeSpreadProperty', | ||
argument: (_param$argument$typeA = (_param$argument$typeA2 = param.argument.typeAnnotation) == null ? void 0 : _param$argument$typeA2.typeAnnotation) != null ? _param$argument$typeA : { | ||
type: 'AnyTypeAnnotation', | ||
loc: param.loc, | ||
range: param.range, | ||
parent: EMPTY_PARENT | ||
}, | ||
loc: param.loc, | ||
range: param.range, | ||
parent: EMPTY_PARENT | ||
}; | ||
} | ||
function mapComponentParameter(param) { | ||
@@ -217,0 +295,0 @@ switch (param.type) { |
@@ -22,2 +22,3 @@ /** | ||
// $FlowFixMe[deprecated-type] | ||
function isNode(thing) { | ||
@@ -24,0 +25,0 @@ return typeof thing === 'object' && thing != null && typeof thing.type === 'string'; |
{ | ||
"name": "hermes-parser", | ||
"version": "0.18.1", | ||
"version": "0.18.2", | ||
"description": "A JavaScript parser built from the Hermes engine", | ||
@@ -12,3 +12,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"hermes-estree": "0.18.1" | ||
"hermes-estree": "0.18.2" | ||
}, | ||
@@ -15,0 +15,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
1045695
6311
+ Addedhermes-estree@0.18.2(transitive)
- Removedhermes-estree@0.18.1(transitive)
Updatedhermes-estree@0.18.2