From 3bd4695fe7f8f7d719d19c852a8311e3b1fb7682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Zugmeyer?= Date: Mon, 8 Apr 2024 18:47:03 +0200 Subject: [PATCH] fix: actually pass `ignoreTagsWithoutType` to extract() --- src/__tests__/extract.js | 13 +++--- src/extract.js | 77 ++++++++++++++------------------ src/verifyPatch.js | 4 +- src/verifyWithFlatConfigPatch.js | 4 +- 4 files changed, 41 insertions(+), 57 deletions(-) diff --git a/src/__tests__/extract.js b/src/__tests__/extract.js index db2a86d..b9a6778 100644 --- a/src/__tests__/extract.js +++ b/src/__tests__/extract.js @@ -21,13 +21,12 @@ function dedent(str) { } function test(params) { - const infos = extract( - dedent(params.input), - params.indent, - params.xmlMode, - params.javaScriptTagNames || ["script"], - params.isJavaScriptMIMEType - ) + const infos = extract(dedent(params.input), params.xmlMode, { + indent: params.indent, + javaScriptTagNames: params.javaScriptTagNames || ["script"], + isJavaScriptMIMEType: params.isJavaScriptMIMEType, + ignoreTagsWithoutType: params.ignoreTagsWithoutType, + }) expect(infos.code.map((code) => code.toString())).toMatchSnapshot() expect(infos.badIndentationLines).toEqual(params.badIndentationLines || []) } diff --git a/src/extract.js b/src/extract.js index ca4107d..880013f 100644 --- a/src/extract.js +++ b/src/extract.js @@ -7,10 +7,9 @@ const NO_IGNORE = 0 const IGNORE_NEXT = 1 const IGNORE_UNTIL_ENABLE = 2 -function iterateScripts(code, options, onChunk) { +function iterateScripts(code, xmlMode, options, onChunk) { if (!code) return - const xmlMode = options.xmlMode const isJavaScriptMIMEType = options.isJavaScriptMIMEType || (() => true) const javaScriptTagNames = options.javaScriptTagNames || ["script"] const ignoreTagsWithoutType = options.ignoreTagsWithoutType || false @@ -210,57 +209,47 @@ function* dedent(indent, slice) { } } -function extract( - code, - indentDescriptor, - xmlMode, - javaScriptTagNames, - isJavaScriptMIMEType -) { +function extract(code, xmlMode, options) { const badIndentationLines = [] const codeParts = [] let lineNumber = 1 let previousHTML = "" - iterateScripts( - code, - { xmlMode, javaScriptTagNames, isJavaScriptMIMEType }, - (chunk) => { - const slice = code.slice(chunk.start, chunk.end) - if (chunk.type === "html") { - const match = slice.match(/\r\n|\n|\r/g) - if (match) lineNumber += match.length - previousHTML = slice - } else if (chunk.type === "script") { - const transformedCode = new TransformableString(code) - let indentSlice = slice - for (const cdata of chunk.cdata) { - transformedCode.replace(cdata.start, cdata.end, "") - if (cdata.end === chunk.end) { - indentSlice = code.slice(chunk.start, cdata.start) - } + iterateScripts(code, xmlMode, options, (chunk) => { + const slice = code.slice(chunk.start, chunk.end) + if (chunk.type === "html") { + const match = slice.match(/\r\n|\n|\r/g) + if (match) lineNumber += match.length + previousHTML = slice + } else if (chunk.type === "script") { + const transformedCode = new TransformableString(code) + let indentSlice = slice + for (const cdata of chunk.cdata) { + transformedCode.replace(cdata.start, cdata.end, "") + if (cdata.end === chunk.end) { + indentSlice = code.slice(chunk.start, cdata.start) } - transformedCode.replace(0, chunk.start, "") - transformedCode.replace(chunk.end, code.length, "") - for (const action of dedent( - computeIndent(indentDescriptor, previousHTML, indentSlice), - indentSlice - )) { - lineNumber += 1 - if (action.type === "dedent") { - transformedCode.replace( - chunk.start + action.from, - chunk.start + action.to, - "" - ) - } else if (action.type === "bad-indent") { - badIndentationLines.push(lineNumber) - } + } + transformedCode.replace(0, chunk.start, "") + transformedCode.replace(chunk.end, code.length, "") + for (const action of dedent( + computeIndent(options.indent, previousHTML, indentSlice), + indentSlice + )) { + lineNumber += 1 + if (action.type === "dedent") { + transformedCode.replace( + chunk.start + action.from, + chunk.start + action.to, + "" + ) + } else if (action.type === "bad-indent") { + badIndentationLines.push(lineNumber) } - codeParts.push(transformedCode) } + codeParts.push(transformedCode) } - ) + }) return { code: codeParts, diff --git a/src/verifyPatch.js b/src/verifyPatch.js index 008a1d7..5abbf61 100644 --- a/src/verifyPatch.js +++ b/src/verifyPatch.js @@ -43,10 +43,8 @@ function createVerifyPatch(verify) { const extractResult = extract( textOrSourceCode, - pluginSettings.indent, mode === "xml", - pluginSettings.javaScriptTagNames, - pluginSettings.isJavaScriptMIMEType + pluginSettings ) if (pluginSettings.reportBadIndent) { diff --git a/src/verifyWithFlatConfigPatch.js b/src/verifyWithFlatConfigPatch.js index 9587283..6941bf6 100644 --- a/src/verifyWithFlatConfigPatch.js +++ b/src/verifyWithFlatConfigPatch.js @@ -34,10 +34,8 @@ function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) { const extractResult = extract( textOrSourceCode, - pluginSettings.indent, mode === "xml", - pluginSettings.javaScriptTagNames, - pluginSettings.isJavaScriptMIMEType + pluginSettings ) if (pluginSettings.reportBadIndent) {