-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
17f8838
commit d7cde04
Showing
6 changed files
with
296 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
const getSettings = require("./settings").getSettings | ||
const getFileMode = require("./getFileMode") | ||
const extract = require("./extract") | ||
const { verifyWithSharedScopes } = require("./verifyWithSharedScopes") | ||
const { remapMessages } = require("./remapMessages") | ||
|
||
const PREPARE_RULE_NAME = "__eslint-plugin-html-prepare" | ||
const PREPARE_PLUGIN_NAME = "__eslint-plugin-html-prepare" | ||
|
||
module.exports = { createVerifyWithFlatConfigPatch } | ||
|
||
function createVerifyWithFlatConfigPatch(verifyWithFlatConfig) { | ||
return function (textOrSourceCode, providedConfig, providedOptions) { | ||
const callOriginalVerify = () => | ||
verifyWithFlatConfig.call( | ||
this, | ||
textOrSourceCode, | ||
providedConfig, | ||
providedOptions | ||
) | ||
|
||
const pluginSettings = getSettings(providedConfig.settings || {}) | ||
const mode = getFileMode(pluginSettings, providedOptions.filename) | ||
|
||
let messages | ||
;[messages, providedConfig] = verifyExternalHtmlPlugin( | ||
providedConfig, | ||
callOriginalVerify | ||
) | ||
|
||
const extractResult = extract( | ||
textOrSourceCode, | ||
pluginSettings.indent, | ||
mode === "xml", | ||
pluginSettings.javaScriptTagNames, | ||
pluginSettings.isJavaScriptMIMEType | ||
) | ||
|
||
if (pluginSettings.reportBadIndent) { | ||
messages.push( | ||
...extractResult.badIndentationLines.map((line) => ({ | ||
message: "Bad line indentation.", | ||
line, | ||
column: 1, | ||
ruleId: "(html plugin)", | ||
severity: pluginSettings.reportBadIndent, | ||
})) | ||
) | ||
} | ||
|
||
// Save code parts parsed source code so we don't have to parse it twice | ||
const sourceCodes = new WeakMap() | ||
const verifyCodePart = (codePart, { prepare, ignoreRules } = {}) => { | ||
providedConfig.plugins[PREPARE_PLUGIN_NAME] = { | ||
rules: { | ||
[PREPARE_RULE_NAME]: { | ||
create(context) { | ||
sourceCodes.set(codePart, context.getSourceCode()) | ||
return { | ||
Program(program) { | ||
if (prepare) { | ||
prepare(context, program) | ||
} | ||
}, | ||
} | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
const localMessages = verifyWithFlatConfig.call( | ||
this, | ||
sourceCodes.get(codePart) || String(codePart), | ||
{ | ||
...providedConfig, | ||
rules: Object.assign( | ||
{ [`${PREPARE_PLUGIN_NAME}/${PREPARE_RULE_NAME}`]: "error" }, | ||
!ignoreRules && providedConfig.rules | ||
), | ||
}, | ||
ignoreRules | ||
? { | ||
...providedOptions, | ||
reportUnusedDisableDirectives: false, | ||
} | ||
: providedOptions | ||
) | ||
|
||
messages.push( | ||
...remapMessages(localMessages, extractResult.hasBOM, codePart) | ||
) | ||
} | ||
|
||
const parserOptions = providedConfig.languageOptions.parserOptions || {} | ||
if (parserOptions.sourceType === "module") { | ||
for (const codePart of extractResult.code) { | ||
verifyCodePart(codePart) | ||
} | ||
} else { | ||
verifyWithSharedScopes(extractResult.code, verifyCodePart, parserOptions) | ||
} | ||
|
||
messages.sort((ma, mb) => ma.line - mb.line || ma.column - mb.column) | ||
|
||
return messages | ||
} | ||
} | ||
|
||
const externalHtmlPlugins = [ | ||
{ parser: "@html-eslint/parser", plugin: "@html-eslint/eslint-plugin" }, | ||
] | ||
|
||
function tryRequire(name) { | ||
try { | ||
return require(name) | ||
} catch (e) { | ||
return undefined | ||
} | ||
} | ||
|
||
function findExternalHtmlPluginName(config) { | ||
if (!config.languageOptions || !config.languageOptions.parser) { | ||
return | ||
} | ||
for (const { parser, plugin } of externalHtmlPlugins) { | ||
let parserModule = tryRequire(parser) | ||
if (config.languageOptions.parser === parserModule) { | ||
const pluginModule = tryRequire(plugin) | ||
for (const [name, plugin] of Object.entries(config.plugins)) { | ||
if (plugin === pluginModule) { | ||
return name | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
function verifyExternalHtmlPlugin(config, callOriginalVerify) { | ||
const htmlPluginName = findExternalHtmlPluginName(config) | ||
if (!htmlPluginName) { | ||
return [[], config] | ||
} | ||
|
||
const rules = {} | ||
for (const name in config.rules) { | ||
if (!name.startsWith(htmlPluginName + "/")) { | ||
rules[name] = config.rules[name] | ||
} | ||
} | ||
|
||
return [ | ||
callOriginalVerify(), | ||
{ | ||
...config, | ||
languageOptions: { | ||
...config.languageOptions, | ||
parser: require("espree"), | ||
}, | ||
rules, | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.