svelte-eslint-parser
Advanced tools
Comparing version 0.34.0-next.2 to 0.34.0-next.3
export declare const name: "svelte-eslint-parser"; | ||
export declare const version: "0.34.0-next.2"; | ||
export declare const version: "0.34.0-next.3"; |
@@ -8,2 +8,2 @@ "use strict"; | ||
exports.name = "svelte-eslint-parser"; | ||
exports.version = "0.34.0-next.2"; | ||
exports.version = "0.34.0-next.3"; |
@@ -8,2 +8,3 @@ "use strict"; | ||
const errors_1 = require("../../errors"); | ||
const svelte_version_1 = require("../svelte-version"); | ||
/** Convert for Attributes */ | ||
@@ -117,3 +118,3 @@ function* convertAttributes(attributes, parent, ctx) { | ||
} | ||
processAttributeValue(node.value, attribute, ctx); | ||
processAttributeValue(node.value, attribute, parent, ctx); | ||
// Not required for shorthands. Therefore, register the token here. | ||
@@ -124,17 +125,28 @@ ctx.addToken("HTMLIdentifier", keyRange); | ||
/** Common process attribute value */ | ||
function processAttributeValue(nodeValue, attribute, ctx) { | ||
for (let index = 0; index < nodeValue.length; index++) { | ||
const v = nodeValue[index]; | ||
function processAttributeValue(nodeValue, attribute, attributeParent, ctx) { | ||
const nodes = nodeValue | ||
.filter((v) => v.type !== "Text" || | ||
// ignore empty | ||
// https://github.com/sveltejs/svelte/pull/6539 | ||
v.start < v.end) | ||
.map((v, index, array) => { | ||
if (v.type === "Text") { | ||
if (v.start === v.end) { | ||
// Empty | ||
// https://github.com/sveltejs/svelte/pull/6539 | ||
continue; | ||
} | ||
const next = nodeValue[index + 1]; | ||
const next = array[index + 1]; | ||
if (next && next.start < v.end) { | ||
// Maybe bug in Svelte can cause the completion index to shift. | ||
// console.log(ctx.getText(v), v.data) | ||
v.end = next.start; | ||
return Object.assign(Object.assign({}, v), { end: next.start }); | ||
} | ||
} | ||
return v; | ||
}); | ||
if (nodes.length === 1 && | ||
nodes[0].type === "MustacheTag" && | ||
attribute.type === "SvelteAttribute") { | ||
const typing = buildAttributeType(attributeParent.parent, attribute.key.name, ctx); | ||
const mustache = (0, mustache_1.convertMustacheTag)(nodes[0], attribute, typing, ctx); | ||
attribute.value.push(mustache); | ||
return; | ||
} | ||
for (const v of nodes) { | ||
if (v.type === "Text") { | ||
attribute.value.push((0, text_1.convertTextToLiteral)(v, attribute, ctx)); | ||
@@ -144,3 +156,3 @@ continue; | ||
if (v.type === "MustacheTag") { | ||
const mustache = (0, mustache_1.convertMustacheTag)(v, attribute, ctx); | ||
const mustache = (0, mustache_1.convertMustacheTag)(v, attribute, null, ctx); | ||
attribute.value.push(mustache); | ||
@@ -153,2 +165,30 @@ continue; | ||
} | ||
/** Build attribute type */ | ||
function buildAttributeType(element, attrName, ctx) { | ||
if (svelte_version_1.svelteVersion.gte(5) && | ||
attrName.startsWith("on") && | ||
(element.type !== "SvelteElement" || element.kind === "html")) { | ||
return buildEventHandlerType(element, attrName.slice(2), ctx); | ||
} | ||
if (element.type !== "SvelteElement" || element.kind !== "component") { | ||
return null; | ||
} | ||
const elementName = ctx.elements.get(element).name; | ||
const componentPropsType = `import('svelte').ComponentProps<${elementName}>`; | ||
return conditional({ | ||
check: `'${attrName}'`, | ||
extends: `infer PROP`, | ||
true: conditional({ | ||
check: `PROP`, | ||
extends: `keyof ${componentPropsType}`, | ||
true: `${componentPropsType}[PROP]`, | ||
false: `never`, | ||
}), | ||
false: `never`, | ||
}); | ||
/** Generate `C extends E ? T : F` type. */ | ||
function conditional(types) { | ||
return `${types.check} extends ${types.extends}?(${types.true}):(${types.false})`; | ||
} | ||
} | ||
/** Convert for Spread */ | ||
@@ -304,3 +344,3 @@ function convertSpreadAttribute(node, parent, ctx) { | ||
}); | ||
processAttributeValue(node.value, directive, ctx); | ||
processAttributeValue(node.value, directive, parent, ctx); | ||
return directive; | ||
@@ -307,0 +347,0 @@ } |
@@ -7,3 +7,3 @@ "use strict"; | ||
/** Get start index of block */ | ||
function startBlockIndex(code, endIndex) { | ||
function startBlockIndex(code, endIndex, block) { | ||
return (0, common_1.lastIndexOf)(code, (c, index) => { | ||
@@ -18,3 +18,3 @@ if (c !== "{") { | ||
} | ||
return code.startsWith("#if", next) || code.startsWith(":else", next); | ||
return code.startsWith(block, next); | ||
} | ||
@@ -28,5 +28,3 @@ return false; | ||
// {:else if expr} {/if} | ||
const nodeStart = elseif | ||
? startBlockIndex(ctx.code, node.start - 1) | ||
: node.start; | ||
const nodeStart = startBlockIndex(ctx.code, elseif ? node.start - 1 : node.start, elseif ? ":else" : "#if"); | ||
const ifBlock = Object.assign({ type: "SvelteIfBlock", elseif: Boolean(elseif), expression: null, children: [], else: null, parent }, ctx.getConvertLocation({ start: nodeStart, end: node.end })); | ||
@@ -46,6 +44,13 @@ ctx.scriptLet.nestIfBlock(node.expression, ifBlock, (es) => { | ||
} | ||
const elseStart = startBlockIndex(ctx.code, node.else.start - 1); | ||
let baseStart = node.else.start; | ||
if (node.else.children.length === 1) { | ||
const c = node.else.children[0]; | ||
if (c.type === "IfBlock" && c.elseif) { | ||
baseStart = Math.min(baseStart, c.start, (0, common_1.getWithLoc)(c.expression).start); | ||
} | ||
} | ||
const elseStart = startBlockIndex(ctx.code, baseStart - 1, ":else"); | ||
if (node.else.children.length === 1) { | ||
const c = node.else.children[0]; | ||
if (c.type === "IfBlock" && c.elseif) { | ||
const elseBlock = Object.assign({ type: "SvelteElseBlock", elseif: true, children: [], parent: ifBlock }, ctx.getConvertLocation({ | ||
@@ -82,3 +87,4 @@ start: elseStart, | ||
// {#each expr as item, index (key)} {/each} | ||
const eachBlock = Object.assign({ type: "SvelteEachBlock", expression: null, context: null, index: null, key: null, children: [], else: null, parent }, ctx.getConvertLocation(node)); | ||
const nodeStart = startBlockIndex(ctx.code, node.start, "#each"); | ||
const eachBlock = Object.assign({ type: "SvelteEachBlock", expression: null, context: null, index: null, key: null, children: [], else: null, parent }, ctx.getConvertLocation({ start: nodeStart, end: node.end })); | ||
let indexRange = null; | ||
@@ -113,3 +119,3 @@ if (node.index) { | ||
} | ||
const elseStart = startBlockIndex(ctx.code, node.else.start - 1); | ||
const elseStart = startBlockIndex(ctx.code, node.else.start - 1, ":else"); | ||
const elseBlock = Object.assign({ type: "SvelteElseBlock", elseif: false, children: [], parent: eachBlock }, ctx.getConvertLocation({ | ||
@@ -129,3 +135,4 @@ start: elseStart, | ||
function convertAwaitBlock(node, parent, ctx) { | ||
const awaitBlock = Object.assign({ type: "SvelteAwaitBlock", expression: null, kind: "await", pending: null, then: null, catch: null, parent }, ctx.getConvertLocation(node)); | ||
const nodeStart = startBlockIndex(ctx.code, node.start, "#await"); | ||
const awaitBlock = Object.assign({ type: "SvelteAwaitBlock", expression: null, kind: "await", pending: null, then: null, catch: null, parent }, ctx.getConvertLocation({ start: nodeStart, end: node.end })); | ||
ctx.scriptLet.addExpression(node.expression, awaitBlock, null, (expression) => { | ||
@@ -256,3 +263,4 @@ awaitBlock.expression = expression; | ||
function convertKeyBlock(node, parent, ctx) { | ||
const keyBlock = Object.assign({ type: "SvelteKeyBlock", expression: null, children: [], parent }, ctx.getConvertLocation(node)); | ||
const nodeStart = startBlockIndex(ctx.code, node.start, "#key"); | ||
const keyBlock = Object.assign({ type: "SvelteKeyBlock", expression: null, children: [], parent }, ctx.getConvertLocation({ start: nodeStart, end: node.end })); | ||
ctx.scriptLet.addExpression(node.expression, keyBlock, null, (expression) => { | ||
@@ -271,3 +279,4 @@ keyBlock.expression = expression; | ||
// {#snippet x(args)}...{/snippet} | ||
const snippetBlock = Object.assign({ type: "SvelteSnippetBlock", id: null, context: null, children: [], parent }, ctx.getConvertLocation(node)); | ||
const nodeStart = startBlockIndex(ctx.code, node.start, "#snippet"); | ||
const snippetBlock = Object.assign({ type: "SvelteSnippetBlock", id: null, context: null, children: [], parent }, ctx.getConvertLocation({ start: nodeStart, end: node.end })); | ||
const closeParenIndex = ctx.code.indexOf(")", (0, common_1.getWithLoc)(node.context || node.expression).end); | ||
@@ -274,0 +283,0 @@ ctx.scriptLet.nestSnippetBlock(node.expression, closeParenIndex, snippetBlock, (id, context) => { |
@@ -55,3 +55,3 @@ "use strict"; | ||
if (child.type === "MustacheTag") { | ||
yield (0, mustache_1.convertMustacheTag)(child, parent, ctx); | ||
yield (0, mustache_1.convertMustacheTag)(child, parent, null, ctx); | ||
continue; | ||
@@ -58,0 +58,0 @@ } |
@@ -5,3 +5,3 @@ import type { SvelteDebugTag, SvelteMustacheTag, SvelteMustacheTagRaw, SvelteMustacheTagText } from "../../ast"; | ||
/** Convert for MustacheTag */ | ||
export declare function convertMustacheTag(node: SvAST.MustacheTag, parent: SvelteMustacheTag["parent"], ctx: Context): SvelteMustacheTagText; | ||
export declare function convertMustacheTag(node: SvAST.MustacheTag, parent: SvelteMustacheTag["parent"], typing: string | null, ctx: Context): SvelteMustacheTagText; | ||
/** Convert for MustacheTag */ | ||
@@ -8,0 +8,0 @@ export declare function convertRawMustacheTag(node: SvAST.RawMustacheTag, parent: SvelteMustacheTag["parent"], ctx: Context): SvelteMustacheTagRaw; |
@@ -5,4 +5,4 @@ "use strict"; | ||
/** Convert for MustacheTag */ | ||
function convertMustacheTag(node, parent, ctx) { | ||
return convertMustacheTag0(node, "text", parent, ctx); | ||
function convertMustacheTag(node, parent, typing, ctx) { | ||
return convertMustacheTag0(node, "text", parent, typing, ctx); | ||
} | ||
@@ -12,3 +12,3 @@ exports.convertMustacheTag = convertMustacheTag; | ||
function convertRawMustacheTag(node, parent, ctx) { | ||
const mustache = convertMustacheTag0(node, "raw", parent, ctx); | ||
const mustache = convertMustacheTag0(node, "raw", parent, null, ctx); | ||
const atHtmlStart = ctx.code.indexOf("@html", mustache.range[0]); | ||
@@ -39,5 +39,5 @@ ctx.addToken("MustacheKeyword", { | ||
/** Convert to MustacheTag */ | ||
function convertMustacheTag0(node, kind, parent, ctx) { | ||
function convertMustacheTag0(node, kind, parent, typing, ctx) { | ||
const mustache = Object.assign({ type: "SvelteMustacheTag", kind, expression: null, parent }, ctx.getConvertLocation(node)); | ||
ctx.scriptLet.addExpression(node.expression, mustache, null, (es) => { | ||
ctx.scriptLet.addExpression(node.expression, mustache, typing, (es) => { | ||
mustache.expression = es; | ||
@@ -44,0 +44,0 @@ }); |
{ | ||
"name": "svelte-eslint-parser", | ||
"version": "0.34.0-next.2", | ||
"version": "0.34.0-next.3", | ||
"description": "Svelte parser for ESLint", | ||
@@ -5,0 +5,0 @@ "repository": "git+https://github.com/sveltejs/svelte-eslint-parser.git", |
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
284248
6866