svelte-eslint-parser
Advanced tools
Comparing version 0.11.0 to 0.12.0
@@ -1,5 +0,6 @@ | ||
import type { Comment, Locations, Token } from "../ast"; | ||
import type { Comment, Locations, SvelteScriptElement, SvelteStyleElement, Token } from "../ast"; | ||
import type ESTree from "estree"; | ||
import { ScriptLetContext } from "./script-let"; | ||
import { LetDirectiveCollections } from "./let-directive-collection"; | ||
import type { AttributeToken } from "../parser/html"; | ||
export declare class ScriptsSourceCode { | ||
@@ -34,2 +35,3 @@ private raw; | ||
private state; | ||
private readonly blocks; | ||
constructor(code: string, parserOptions: any); | ||
@@ -65,3 +67,9 @@ getLocFromIndex(index: number): { | ||
stripScriptCode(start: number, end: number): void; | ||
findBlock(element: SvelteScriptElement | SvelteStyleElement): Block | undefined; | ||
} | ||
declare type Block = { | ||
tag: "script" | "style"; | ||
attrs: AttributeToken[]; | ||
contentRange: [number, number]; | ||
}; | ||
export declare class LinesAndColumns { | ||
@@ -79,1 +87,2 @@ private readonly lineStartIndices; | ||
} | ||
export {}; |
@@ -12,2 +12,3 @@ "use strict"; | ||
const resolve_parser_1 = require("../parser/resolve-parser"); | ||
const html_1 = require("../parser/html"); | ||
class ScriptsSourceCode { | ||
@@ -57,2 +58,3 @@ constructor(script, attrs) { | ||
constructor(code, parserOptions) { | ||
var _a; | ||
this.tokens = []; | ||
@@ -63,2 +65,3 @@ this.comments = []; | ||
this.state = {}; | ||
this.blocks = []; | ||
this.code = code; | ||
@@ -70,17 +73,21 @@ this.parserOptions = parserOptions; | ||
let scriptCode = ""; | ||
let scriptAttrs = {}; | ||
const scriptAttrs = {}; | ||
let start = 0; | ||
for (const block of extractBlocks(code)) { | ||
this.blocks.push(block); | ||
templateCode += | ||
code.slice(start, block.codeRange[0]) + | ||
spaces.slice(block.codeRange[0], block.codeRange[1]); | ||
code.slice(start, block.contentRange[0]) + | ||
spaces.slice(block.contentRange[0], block.contentRange[1]); | ||
if (block.tag === "script") { | ||
scriptCode += | ||
spaces.slice(start, block.codeRange[0]) + block.code; | ||
scriptAttrs = Object.assign(scriptAttrs, block.attrs); | ||
spaces.slice(start, block.contentRange[0]) + | ||
code.slice(...block.contentRange); | ||
for (const attr of block.attrs) { | ||
scriptAttrs[attr.key.name] = (_a = attr.value) === null || _a === void 0 ? void 0 : _a.value; | ||
} | ||
} | ||
else { | ||
scriptCode += spaces.slice(start, block.codeRange[1]); | ||
scriptCode += spaces.slice(start, block.contentRange[1]); | ||
} | ||
start = block.codeRange[1]; | ||
start = block.contentRange[1]; | ||
} | ||
@@ -175,2 +182,8 @@ templateCode += code.slice(start); | ||
} | ||
findBlock(element) { | ||
const tag = element.type === "SvelteScriptElement" ? "script" : "style"; | ||
return this.blocks.find((block) => block.tag === tag && | ||
element.range[0] <= block.contentRange[0] && | ||
block.contentRange[1] <= element.range[1]); | ||
} | ||
} | ||
@@ -180,33 +193,36 @@ exports.Context = Context; | ||
function* extractBlocks(code) { | ||
var _a; | ||
const startTagRe = /<(script|style)(\s[\s\S]*?)?>/giu; | ||
const endScriptTagRe = /<\/script(?:\s[\s\S]*?)?>/giu; | ||
const endStyleTagRe = /<\/style(?:\s[\s\S]*?)?>/giu; | ||
let startTagRes; | ||
while ((startTagRes = startTagRe.exec(code))) { | ||
const [startTag, tag, attributes = ""] = startTagRes; | ||
const startTagStart = startTagRes.index; | ||
const startTagEnd = startTagStart + startTag.length; | ||
const startTagOpenRe = /<(script|style)([\s>])/giu; | ||
const endScriptTagRe = /<\/script>/giu; | ||
const endStyleTagRe = /<\/style>/giu; | ||
let startTagOpenMatch; | ||
while ((startTagOpenMatch = startTagOpenRe.exec(code))) { | ||
const [, tag, nextChar] = startTagOpenMatch; | ||
let startTagEnd = startTagOpenRe.lastIndex; | ||
let attrs = []; | ||
if (!nextChar.trim()) { | ||
const attrsData = (0, html_1.parseAttributes)(code, startTagOpenRe.lastIndex); | ||
attrs = attrsData.attributes; | ||
startTagEnd = attrsData.index; | ||
if (code[startTagEnd] === "/") { | ||
startTagEnd++; | ||
} | ||
if (code[startTagEnd] === ">") { | ||
startTagEnd++; | ||
} | ||
else { | ||
continue; | ||
} | ||
} | ||
const endTagRe = tag.toLowerCase() === "script" ? endScriptTagRe : endStyleTagRe; | ||
endTagRe.lastIndex = startTagRe.lastIndex; | ||
const endTagRes = endTagRe.exec(code); | ||
if (endTagRes) { | ||
const endTagStart = endTagRes.index; | ||
const codeRange = [startTagEnd, endTagStart]; | ||
const attrRe = /(?<key>[^\s=]+)(?:=(?:"(?<val1>[^"]*)"|'(?<val2>[^"]*)'|(?<val3>[^\s=]+)))?/gu; | ||
const attrs = {}; | ||
let attrRes; | ||
while ((attrRes = attrRe.exec(attributes))) { | ||
attrs[attrRes.groups.key] = | ||
(_a = (attrRes.groups.val1 || | ||
attrRes.groups.val2 || | ||
attrRes.groups.val3)) !== null && _a !== void 0 ? _a : null; | ||
} | ||
endTagRe.lastIndex = startTagEnd; | ||
const endTagMatch = endTagRe.exec(code); | ||
if (endTagMatch) { | ||
const endTagStart = endTagMatch.index; | ||
const contentRange = [startTagEnd, endTagStart]; | ||
yield { | ||
code: code.slice(...codeRange), | ||
codeRange, | ||
contentRange, | ||
attrs, | ||
tag: tag, | ||
}; | ||
startTagRe.lastIndex = endTagRe.lastIndex; | ||
startTagOpenRe.lastIndex = endTagRe.lastIndex; | ||
} | ||
@@ -213,0 +229,0 @@ } |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertSvelteRoot = void 0; | ||
const compiler_1 = require("svelte/compiler"); | ||
const element_1 = require("./element"); | ||
const attr_1 = require("./attr"); | ||
const text_1 = require("./text"); | ||
/** | ||
@@ -70,28 +69,3 @@ * Convert root | ||
function extractAttributes(element, ctx) { | ||
const script = element.type === "SvelteScriptElement"; | ||
let code = " ".repeat(element.range[0]); | ||
const elementCode = ctx.sourceCode.template.slice(...element.range); | ||
const startRegex = script | ||
? /<script(\s[\s\S]*?)?>/giu | ||
: /<style(\s[\s\S]*?)?>/giu; | ||
const endTag = script ? "</script>" : "</style>"; | ||
let re; | ||
let index = 0; | ||
while ((re = startRegex.exec(elementCode))) { | ||
const [, attributes] = re; | ||
const endTagIndex = elementCode.indexOf(endTag, startRegex.lastIndex); | ||
if (endTagIndex >= 0) { | ||
const contextLength = endTagIndex - startRegex.lastIndex; | ||
code += elementCode.slice(index, re.index); | ||
code += `${script ? "<div " : "<div "}${attributes || ""}>`; | ||
code += `${" ".repeat(contextLength)}</div>`; | ||
startRegex.lastIndex = index = endTagIndex + endTag.length; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
code += elementCode.slice(index); | ||
const svelteAst = (0, compiler_1.parse)(code); | ||
const fakeElement = svelteAst.html.children.find((c) => c.type === "Element"); | ||
var _a, _b; | ||
element.startTag = { | ||
@@ -111,3 +85,24 @@ type: "SvelteStartTag", | ||
}; | ||
element.startTag.attributes.push(...(0, attr_1.convertAttributes)(fakeElement.attributes, element.startTag, ctx)); | ||
const block = ctx.findBlock(element); | ||
if (block) { | ||
for (const attr of block.attrs) { | ||
const attrNode = Object.assign({ type: "SvelteAttribute", boolean: false, key: null, value: [], parent: element.startTag }, ctx.getConvertLocation({ | ||
start: attr.key.start, | ||
end: (_b = (_a = attr.value) === null || _a === void 0 ? void 0 : _a.end) !== null && _b !== void 0 ? _b : attr.key.end, | ||
})); | ||
element.startTag.attributes.push(attrNode); | ||
attrNode.key = Object.assign({ type: "SvelteName", name: attr.key.name, parent: attrNode }, ctx.getConvertLocation(attr.key)); | ||
ctx.addToken("HTMLIdentifier", attr.key); | ||
if (attr.value == null) { | ||
attrNode.boolean = true; | ||
} | ||
else { | ||
const valueLoc = attr.value.quote | ||
? { start: attr.value.start + 1, end: attr.value.end - 1 } | ||
: attr.value; | ||
attrNode.value.push(Object.assign({ type: "SvelteLiteral", value: attr.value.value, parent: attrNode }, ctx.getConvertLocation(valueLoc))); | ||
(0, text_1.extractTextTokens)(valueLoc, ctx); | ||
} | ||
} | ||
} | ||
} |
@@ -11,1 +11,6 @@ import type ESTree from "estree"; | ||
export declare function convertTemplateLiteralToLiteral(node: ESTree.TemplateLiteral, parent: SvelteLiteral["parent"], ctx: Context): SvelteLiteral; | ||
/** Extract tokens */ | ||
export declare function extractTextTokens(node: { | ||
start: number; | ||
end: number; | ||
}, ctx: Context): void; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.convertTemplateLiteralToLiteral = exports.convertTextToLiteral = exports.convertText = void 0; | ||
exports.extractTextTokens = exports.convertTemplateLiteralToLiteral = exports.convertTextToLiteral = exports.convertText = void 0; | ||
const common_1 = require("./common"); | ||
@@ -22,3 +22,3 @@ /** Convert for Text */ | ||
const text = Object.assign({ type: "SvelteLiteral", value: node.quasis[0].value.raw, parent }, ctx.getConvertLocation(node)); | ||
extractTextTokens(node, ctx); | ||
extractTextTokens((0, common_1.getWithLoc)(node), ctx); | ||
return text; | ||
@@ -29,3 +29,3 @@ } | ||
function extractTextTokens(node, ctx) { | ||
const loc = (0, common_1.getWithLoc)(node); | ||
const loc = node; | ||
let start = loc.start; | ||
@@ -46,1 +46,2 @@ let word = false; | ||
} | ||
exports.extractTextTokens = extractTextTokens; |
{ | ||
"name": "svelte-eslint-parser", | ||
"version": "0.11.0", | ||
"version": "0.12.0", | ||
"description": "Svelte parser for ESLint", | ||
@@ -20,3 +20,3 @@ "main": "lib/index.js", | ||
"cover": "nyc --reporter=lcov npm run test", | ||
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot", | ||
"debug": "mocha --require ts-node/register/transpile-only \"tests/src/**/*.ts\" --reporter dot --timeout 60000", | ||
"preversion": "npm run lint && npm test", | ||
@@ -54,4 +54,5 @@ "update-fixtures": "ts-node --transpile-only ./tools/update-fixtures.ts", | ||
"@ota-meshi/eslint-plugin": "^0.10.0", | ||
"@ota-meshi/eslint-plugin-svelte": "^0.20.0", | ||
"@ota-meshi/eslint-plugin-svelte": "^0.22.0", | ||
"@types/benchmark": "^2.1.1", | ||
"@types/chai": "^4.3.0", | ||
"@types/eslint": "^8.0.0", | ||
@@ -66,2 +67,3 @@ "@types/eslint-scope": "^3.7.0", | ||
"benchmark": "^2.1.4", | ||
"chai": "^4.3.4", | ||
"code-red": "^0.2.3", | ||
@@ -83,2 +85,3 @@ "eslint": "^8.2.0", | ||
"mocha": "^9.1.3", | ||
"mocha-chai-jest-snapshot": "^1.1.3", | ||
"nyc": "^15.1.0", | ||
@@ -85,0 +88,0 @@ "prettier": "^2.0.5", |
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
173884
55
4220
40