svelte-eslint-parser
Advanced tools
Comparing version 0.24.2 to 0.25.0
@@ -56,5 +56,24 @@ "use strict"; | ||
if (isTS) { | ||
(0, scope_1.removeScope)(result.scopeManager, result.getScope(tsAs.typeAnnotation.type === "TSParenthesizedType" | ||
const blockNode = tsAs.typeAnnotation.type === "TSParenthesizedType" | ||
? tsAs.typeAnnotation.typeAnnotation | ||
: tsAs.typeAnnotation)); | ||
: tsAs.typeAnnotation; | ||
const targetScopes = [result.getScope(blockNode)]; | ||
let targetBlockNode = blockNode; | ||
while (targetBlockNode.type === "TSConditionalType" || | ||
targetBlockNode.type === "TSParenthesizedType") { | ||
if (targetBlockNode.type === "TSParenthesizedType") { | ||
targetBlockNode = targetBlockNode.typeAnnotation; | ||
continue; | ||
} | ||
// TSConditionalType's `falseType` may not be a child scope. | ||
const falseType = targetBlockNode.falseType; | ||
const falseTypeScope = result.getScope(falseType); | ||
if (!targetScopes.includes(falseTypeScope)) { | ||
targetScopes.push(falseTypeScope); | ||
} | ||
targetBlockNode = falseType; | ||
} | ||
for (const scope of targetScopes) { | ||
(0, scope_1.removeScope)(result.scopeManager, scope); | ||
} | ||
this.remapNodes([ | ||
@@ -61,0 +80,0 @@ { |
@@ -6,4 +6,4 @@ import type { SvelteAttribute, SvelteShorthandAttribute, SvelteDirective, SvelteSpreadAttribute, SvelteStartTag, SvelteStyleDirective } from "../../ast"; | ||
/** Convert for Attributes */ | ||
export declare function convertAttributes(attributes: SvAST.AttributeOrDirective[], parent: SvelteStartTag, ctx: Context): IterableIterator<SvelteAttribute | SvelteShorthandAttribute | SvelteSpreadAttribute | SvelteDirective | SvelteStyleDirective>; | ||
export declare function convertAttributes(attributes: SvAST.AttributeOrDirective[], parent: SvelteStartTag, elementName: string, ctx: Context): IterableIterator<SvelteAttribute | SvelteShorthandAttribute | SvelteSpreadAttribute | SvelteDirective | SvelteStyleDirective>; | ||
/** Convert for attribute tokens */ | ||
export declare function convertAttributeTokens(attributes: AttributeToken[], parent: SvelteStartTag, ctx: Context): IterableIterator<SvelteAttribute>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
/** Convert for Attributes */ | ||
function* convertAttributes(attributes, parent, ctx) { | ||
function* convertAttributes(attributes, parent, elementName, ctx) { | ||
for (const attr of attributes) { | ||
@@ -25,3 +25,3 @@ if (attr.type === "Attribute") { | ||
if (attr.type === "EventHandler") { | ||
yield convertEventHandlerDirective(attr, parent, ctx); | ||
yield convertEventHandlerDirective(attr, parent, elementName, ctx); | ||
continue; | ||
@@ -188,13 +188,57 @@ } | ||
/** Convert for EventHandler Directive */ | ||
function convertEventHandlerDirective(node, parent, ctx) { | ||
function convertEventHandlerDirective(node, parent, elementName, ctx) { | ||
const directive = Object.assign({ type: "SvelteDirective", kind: "EventHandler", key: null, expression: null, parent }, ctx.getConvertLocation(node)); | ||
const isCustomEvent = parent.parent.type === "SvelteElement" && | ||
(parent.parent.kind === "component" || parent.parent.kind === "special"); | ||
const typing = buildEventHandlerType(parent.parent, elementName, node.name); | ||
processDirective(node, directive, ctx, { | ||
processExpression: buildProcessExpressionForExpression(directive, ctx, isCustomEvent | ||
? "(e:CustomEvent<any>)=>void" | ||
: `(e:'${node.name}' extends infer U?U extends keyof HTMLElementEventMap?HTMLElementEventMap[U]:CustomEvent<any>:never)=>void`), | ||
processExpression: buildProcessExpressionForExpression(directive, ctx, typing), | ||
}); | ||
return directive; | ||
} | ||
/** Build event handler type */ | ||
function buildEventHandlerType(element, elementName, eventName) { | ||
const nativeEventHandlerType = [ | ||
`(e:`, | ||
/**/ `'${eventName}' extends infer EVT`, | ||
/**/ /**/ `?EVT extends keyof HTMLElementEventMap`, | ||
/**/ /**/ /**/ `?HTMLElementEventMap[EVT]`, | ||
/**/ /**/ /**/ `:CustomEvent<any>`, | ||
/**/ /**/ `:never`, | ||
`)=>void`, | ||
].join(""); | ||
if (element.type !== "SvelteElement") { | ||
return nativeEventHandlerType; | ||
} | ||
if (element.kind === "component") { | ||
// `@typescript-eslint/parser` currently cannot parse `*.svelte` import types correctly. | ||
// So if we try to do a correct type parsing, it's argument type will be `any`. | ||
// A workaround is to inject the type directly, as `CustomEvent<any>` is better than `any`. | ||
// const componentEvents = `import('svelte').ComponentEvents<${elementName}>`; | ||
// return `(e:'${eventName}' extends keyof ${componentEvents}?${componentEvents}['${eventName}']:CustomEvent<any>)=>void`; | ||
return `(e:CustomEvent<any>)=>void`; | ||
} | ||
if (element.kind === "special") { | ||
if (elementName === "svelte:component") | ||
return `(e:CustomEvent<any>)=>void`; | ||
return nativeEventHandlerType; | ||
} | ||
const attrName = `on:${eventName}`; | ||
const importSvelteHTMLElements = "import('svelte/elements').SvelteHTMLElements"; | ||
return [ | ||
`'${elementName}' extends infer EL`, | ||
/**/ `?(`, | ||
/**/ /**/ `EL extends keyof ${importSvelteHTMLElements}`, | ||
/**/ /**/ `?(`, | ||
/**/ /**/ /**/ `'${attrName}' extends infer ATTR`, | ||
/**/ /**/ /**/ `?(`, | ||
/**/ /**/ /**/ /**/ `ATTR extends keyof ${importSvelteHTMLElements}[EL]`, | ||
/**/ /**/ /**/ /**/ /**/ `?${importSvelteHTMLElements}[EL][ATTR]`, | ||
/**/ /**/ /**/ /**/ /**/ `:${nativeEventHandlerType}`, | ||
/**/ /**/ /**/ `)`, | ||
/**/ /**/ /**/ `:never`, | ||
/**/ /**/ `)`, | ||
/**/ /**/ `:${nativeEventHandlerType}`, | ||
/**/ `)`, | ||
/**/ `:never`, | ||
].join(""); | ||
} | ||
/** Convert for Class Directive */ | ||
@@ -201,0 +245,0 @@ function convertClassDirective(node, parent, ctx) { |
@@ -162,2 +162,3 @@ "use strict"; | ||
element.startTag.parent = element; | ||
const elementName = node.name; | ||
const { letDirectives, attributes } = extractLetDirectives(node); | ||
@@ -167,7 +168,7 @@ const letParams = []; | ||
ctx.letDirCollections.beginExtract(); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, elementName, ctx)); | ||
letParams.push(...ctx.letDirCollections.extract().getLetParams()); | ||
} | ||
if (!letParams.length && !needScopeByChildren(node)) { | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
element.children.push(...convertChildren(node, element, ctx)); | ||
@@ -177,3 +178,3 @@ } | ||
ctx.scriptLet.nestBlock(element, letParams); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
(0, sort_1.sortNodes)(element.startTag.attributes); | ||
@@ -186,3 +187,3 @@ element.children.push(...convertChildren(node, element, ctx)); | ||
ctx.addToken("HTMLIdentifier", openTokenRange); | ||
const name = Object.assign({ type: "SvelteName", name: node.name, parent: element }, ctx.getConvertLocation(openTokenRange)); | ||
const name = Object.assign({ type: "SvelteName", name: elementName, parent: element }, ctx.getConvertLocation(openTokenRange)); | ||
return name; | ||
@@ -232,2 +233,3 @@ }, | ||
element.startTag.parent = element; | ||
const elementName = node.name; | ||
const { letDirectives, attributes } = extractLetDirectives(node); | ||
@@ -237,7 +239,7 @@ const letParams = []; | ||
ctx.letDirCollections.beginExtract(); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, elementName, ctx)); | ||
letParams.push(...ctx.letDirCollections.extract().getLetParams()); | ||
} | ||
if (!letParams.length && !needScopeByChildren(node)) { | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
element.children.push(...convertChildren(node, element, ctx)); | ||
@@ -247,3 +249,3 @@ } | ||
ctx.scriptLet.nestBlock(element, letParams); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
(0, sort_1.sortNodes)(element.startTag.attributes); | ||
@@ -254,5 +256,5 @@ element.children.push(...convertChildren(node, element, ctx)); | ||
const thisExpression = (node.type === "InlineComponent" && | ||
node.name === "svelte:component" && | ||
elementName === "svelte:component" && | ||
node.expression) || | ||
(node.type === "Element" && node.name === "svelte:element" && node.tag); | ||
(node.type === "Element" && elementName === "svelte:element" && node.tag); | ||
if (thisExpression) { | ||
@@ -283,3 +285,3 @@ const eqIndex = ctx.code.lastIndexOf("=", (0, common_1.getWithLoc)(thisExpression).start); | ||
ctx.addToken("HTMLIdentifier", openTokenRange); | ||
const name = Object.assign({ type: "SvelteName", name: node.name, parent: element }, ctx.getConvertLocation(openTokenRange)); | ||
const name = Object.assign({ type: "SvelteName", name: elementName, parent: element }, ctx.getConvertLocation(openTokenRange)); | ||
return name; | ||
@@ -308,2 +310,3 @@ }, | ||
element.startTag.parent = element; | ||
const elementName = node.name; | ||
const { letDirectives, attributes } = extractLetDirectives(node); | ||
@@ -313,7 +316,7 @@ const letParams = []; | ||
ctx.letDirCollections.beginExtract(); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(letDirectives, element.startTag, elementName, ctx)); | ||
letParams.push(...ctx.letDirCollections.extract().getLetParams()); | ||
} | ||
if (!letParams.length && !needScopeByChildren(node)) { | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
element.children.push(...convertChildren(node, element, ctx)); | ||
@@ -323,3 +326,3 @@ } | ||
ctx.scriptLet.nestBlock(element, letParams); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, ctx)); | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(attributes, element.startTag, elementName, ctx)); | ||
(0, sort_1.sortNodes)(element.startTag.attributes); | ||
@@ -331,3 +334,3 @@ element.children.push(...convertChildren(node, element, ctx)); | ||
buildNameNode: (openTokenRange) => { | ||
const chains = node.name.split("."); | ||
const chains = elementName.split("."); | ||
const id = chains.shift(); | ||
@@ -334,0 +337,0 @@ const idRange = { |
@@ -282,4 +282,4 @@ "use strict"; | ||
function removeScope(scopeManager, scope) { | ||
for (const childScope of scope.childScopes) { | ||
removeScope(scopeManager, childScope); | ||
while (scope.childScopes[0]) { | ||
removeScope(scopeManager, scope.childScopes[0]); | ||
} | ||
@@ -286,0 +286,0 @@ while (scope.references[0]) { |
{ | ||
"name": "svelte-eslint-parser", | ||
"version": "0.24.2", | ||
"version": "0.25.0", | ||
"description": "Svelte parser for ESLint", | ||
@@ -71,3 +71,3 @@ "repository": "git+https://github.com/ota-meshi/svelte-eslint-parser.git", | ||
"@typescript-eslint/eslint-plugin": "^5.4.0", | ||
"@typescript-eslint/parser": "~5.55.0", | ||
"@typescript-eslint/parser": "~5.57.0", | ||
"benchmark": "^2.1.4", | ||
@@ -74,0 +74,0 @@ "chai": "^4.3.4", |
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
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
237429
5792