Skip to content

Commit

Permalink
fix: actually pass ignoreTagsWithoutType to extract()
Browse files Browse the repository at this point in the history
  • Loading branch information
BenoitZugmeyer committed Apr 8, 2024
1 parent 52553b8 commit 3bd4695
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 57 deletions.
13 changes: 6 additions & 7 deletions src/__tests__/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || [])
}
Expand Down
77 changes: 33 additions & 44 deletions src/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 1 addition & 3 deletions src/verifyPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ function createVerifyPatch(verify) {

const extractResult = extract(
textOrSourceCode,
pluginSettings.indent,
mode === "xml",
pluginSettings.javaScriptTagNames,
pluginSettings.isJavaScriptMIMEType
pluginSettings
)

if (pluginSettings.reportBadIndent) {
Expand Down
4 changes: 1 addition & 3 deletions src/verifyWithFlatConfigPatch.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) {

const extractResult = extract(
textOrSourceCode,
pluginSettings.indent,
mode === "xml",
pluginSettings.javaScriptTagNames,
pluginSettings.isJavaScriptMIMEType
pluginSettings
)

if (pluginSettings.reportBadIndent) {
Expand Down

0 comments on commit 3bd4695

Please sign in to comment.