svelte-eslint-parser
Advanced tools
Comparing version 0.26.0 to 0.26.1
import type ESTree from "estree"; | ||
/** indexOf */ | ||
export declare function indexOf(str: string, search: (c: string) => boolean, start: number): number; | ||
export declare function indexOf(str: string, search: (c: string, index: number) => boolean, start: number, end?: number): number; | ||
/** lastIndexOf */ | ||
@@ -5,0 +5,0 @@ export declare function lastIndexOf(str: string, search: (c: string, index: number) => boolean, end: number): number; |
@@ -5,6 +5,7 @@ "use strict"; | ||
/** indexOf */ | ||
function indexOf(str, search, start) { | ||
for (let index = start; index < str.length; index++) { | ||
function indexOf(str, search, start, end) { | ||
const endIndex = end !== null && end !== void 0 ? end : str.length; | ||
for (let index = start; index < endIndex; index++) { | ||
const c = str[index]; | ||
if (search(c)) { | ||
if (search(c, index)) { | ||
return index; | ||
@@ -11,0 +12,0 @@ } |
@@ -11,2 +11,3 @@ "use strict"; | ||
const sort_1 = require("../sort"); | ||
const __1 = require("../.."); | ||
/* eslint-disable complexity -- X */ | ||
@@ -258,22 +259,3 @@ /** Convert for Fragment or Element or ... */ | ||
if (thisExpression) { | ||
const eqIndex = ctx.code.lastIndexOf("=", (0, common_1.getWithLoc)(thisExpression).start); | ||
const startIndex = ctx.code.lastIndexOf("this", eqIndex); | ||
const closeIndex = ctx.code.indexOf("}", (0, common_1.getWithLoc)(thisExpression).end); | ||
const endIndex = (0, common_1.indexOf)(ctx.code, (c) => c === ">" || !c.trim(), closeIndex); | ||
const thisAttr = Object.assign({ type: "SvelteSpecialDirective", kind: "this", key: null, expression: null, parent: element.startTag }, ctx.getConvertLocation({ start: startIndex, end: endIndex })); | ||
thisAttr.key = Object.assign({ type: "SvelteSpecialDirectiveKey", parent: thisAttr }, ctx.getConvertLocation({ start: startIndex, end: eqIndex })); | ||
ctx.addToken("HTMLIdentifier", { | ||
start: startIndex, | ||
end: eqIndex, | ||
}); | ||
ctx.scriptLet.addExpression(thisExpression, thisAttr, null, (es) => { | ||
thisAttr.expression = es; | ||
}); | ||
const targetIndex = element.startTag.attributes.findIndex((attr) => thisAttr.range[1] <= attr.range[0]); | ||
if (targetIndex === -1) { | ||
element.startTag.attributes.push(thisAttr); | ||
} | ||
else { | ||
element.startTag.attributes.splice(targetIndex, 0, thisAttr); | ||
} | ||
processThisAttribute(node, thisExpression, element, ctx); | ||
} | ||
@@ -289,2 +271,104 @@ extractElementTags(element, ctx, { | ||
} | ||
/** process `this=` */ | ||
function processThisAttribute(node, thisValue, element, ctx) { | ||
let thisNode; | ||
if (typeof thisValue === "string") { | ||
// this="..." | ||
const startIndex = findStartIndexOfThis(node, ctx); | ||
const eqIndex = ctx.code.indexOf("=", startIndex + 4 /* t,h,i,s */); | ||
const valueStartIndex = (0, common_1.indexOf)(ctx.code, (c) => Boolean(c.trim()), eqIndex + 1); | ||
const quote = ctx.code.startsWith(thisValue, valueStartIndex) | ||
? null | ||
: ctx.code[valueStartIndex]; | ||
const literalStartIndex = quote | ||
? valueStartIndex + quote.length | ||
: valueStartIndex; | ||
const literalEndIndex = literalStartIndex + thisValue.length; | ||
const endIndex = quote ? literalEndIndex + quote.length : literalEndIndex; | ||
const thisAttr = Object.assign({ type: "SvelteAttribute", key: null, boolean: false, value: [], parent: element.startTag }, ctx.getConvertLocation({ start: startIndex, end: endIndex })); | ||
thisAttr.key = Object.assign({ type: "SvelteName", name: "this", parent: thisAttr }, ctx.getConvertLocation({ start: startIndex, end: eqIndex })); | ||
thisAttr.value.push(Object.assign({ type: "SvelteLiteral", value: thisValue, parent: thisAttr }, ctx.getConvertLocation({ | ||
start: literalStartIndex, | ||
end: literalEndIndex, | ||
}))); | ||
// this | ||
ctx.addToken("HTMLIdentifier", { | ||
start: startIndex, | ||
end: startIndex + 4, | ||
}); | ||
// = | ||
ctx.addToken("Punctuator", { | ||
start: eqIndex, | ||
end: eqIndex + 1, | ||
}); | ||
if (quote) { | ||
// " | ||
ctx.addToken("Punctuator", { | ||
start: valueStartIndex, | ||
end: literalStartIndex, | ||
}); | ||
} | ||
ctx.addToken("HTMLText", { | ||
start: literalStartIndex, | ||
end: literalEndIndex, | ||
}); | ||
if (quote) { | ||
// " | ||
ctx.addToken("Punctuator", { | ||
start: literalEndIndex, | ||
end: endIndex, | ||
}); | ||
} | ||
thisNode = thisAttr; | ||
} | ||
else { | ||
// this={...} | ||
const eqIndex = ctx.code.lastIndexOf("=", (0, common_1.getWithLoc)(thisValue).start); | ||
const startIndex = ctx.code.lastIndexOf("this", eqIndex); | ||
const closeIndex = ctx.code.indexOf("}", (0, common_1.getWithLoc)(thisValue).end); | ||
const endIndex = (0, common_1.indexOf)(ctx.code, (c) => c === ">" || !c.trim(), closeIndex); | ||
const thisDir = Object.assign({ type: "SvelteSpecialDirective", kind: "this", key: null, expression: null, parent: element.startTag }, ctx.getConvertLocation({ start: startIndex, end: endIndex })); | ||
thisDir.key = Object.assign({ type: "SvelteSpecialDirectiveKey", parent: thisDir }, ctx.getConvertLocation({ start: startIndex, end: eqIndex })); | ||
// this | ||
ctx.addToken("HTMLIdentifier", { | ||
start: startIndex, | ||
end: startIndex + 4, | ||
}); | ||
// = | ||
ctx.addToken("Punctuator", { | ||
start: eqIndex, | ||
end: eqIndex + 1, | ||
}); | ||
ctx.scriptLet.addExpression(thisValue, thisDir, null, (es) => { | ||
thisDir.expression = es; | ||
}); | ||
thisNode = thisDir; | ||
} | ||
const targetIndex = element.startTag.attributes.findIndex((attr) => thisNode.range[1] <= attr.range[0]); | ||
if (targetIndex === -1) { | ||
element.startTag.attributes.push(thisNode); | ||
} | ||
else { | ||
element.startTag.attributes.splice(targetIndex, 0, thisNode); | ||
} | ||
} | ||
/** Find the start index of `this` */ | ||
function findStartIndexOfThis(node, ctx) { | ||
var _a, _b, _d, _e; | ||
// Get the end index of `svelte:element` | ||
const startIndex = ctx.code.indexOf(node.name, node.start) + node.name.length; | ||
const sortedAttrs = [...node.attributes].sort((a, b) => a.start - b.start); | ||
// Find the start index of `this` from the end index of `svelte:element`. | ||
// However, it only seeks to the start index of the first attribute (or the end index of element node). | ||
let thisIndex = (0, common_1.indexOf)(ctx.code, (_c, index) => ctx.code.startsWith("this", index), startIndex, (_b = (_a = sortedAttrs[0]) === null || _a === void 0 ? void 0 : _a.start) !== null && _b !== void 0 ? _b : node.end); | ||
while (thisIndex < 0) { | ||
if (sortedAttrs.length === 0) | ||
throw new __1.ParseError("Cannot resolved `this` attribute.", thisIndex, ctx); | ||
// Step3: Find the start index of `this` from the end index of attribute. | ||
// However, it only seeks to the start index of the first attribute (or the end index of element node). | ||
const nextStartIndex = sortedAttrs.shift().end; | ||
thisIndex = (0, common_1.indexOf)(ctx.code, (_c, index) => ctx.code.startsWith("this", index), nextStartIndex, (_e = (_d = sortedAttrs[0]) === null || _d === void 0 ? void 0 : _d.start) !== null && _e !== void 0 ? _e : node.end); | ||
} | ||
return thisIndex; | ||
} | ||
/** Convert for ComponentElement */ | ||
@@ -291,0 +375,0 @@ function convertComponentElement(node, parent, ctx) { |
@@ -97,7 +97,7 @@ import type ESTree from "estree"; | ||
} | ||
export interface SvelteComponent extends BaseElement { | ||
export interface SvelteElement extends BaseElement { | ||
name: "svelte:element"; | ||
tag: ESTree.Expression; | ||
tag: ESTree.Expression | string; | ||
} | ||
export type Element = BasicElement | SvelteComponent; | ||
export type Element = BasicElement | SvelteElement; | ||
export interface BaseInlineComponent extends BaseNode { | ||
@@ -104,0 +104,0 @@ type: "InlineComponent"; |
{ | ||
"name": "svelte-eslint-parser", | ||
"version": "0.26.0", | ||
"version": "0.26.1", | ||
"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
241815
5891