Skip to content

Commit

Permalink
Fix Vue parser for JS
Browse files Browse the repository at this point in the history
document sources where bundler is replacing sourcecode
  • Loading branch information
vdiez committed Nov 14, 2024
1 parent 4fbcd08 commit 1559ab8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
7 changes: 7 additions & 0 deletions esbuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ await esbuild.build({
pattern: [['path.dirname(fileURLToPath(import.meta.url))', '__dirname']],
}),
// Simplify the loader function in babel. At the end it's just importing Babel parser
// This matches the result of the TS compilation of the following lines
// https://github.com/babel/babel/blob/v7.25.1/eslint/babel-eslint-parser/src/parse.cts#L8-L12
textReplace({
include: /node_modules[\/\\]@babel[\/\\]eslint-parser[\/\\]lib[\/\\]parse\.cjs$/,
pattern: [
Expand All @@ -33,7 +35,9 @@ await esbuild.build({
textReplace({
include: /node_modules[\/\\]eslint[\/\\]lib[\/\\]rule-tester[\/\\]rule-tester\.js$/,
pattern: [
// https://github.com/eslint/eslint/blob/v8.57.0/lib/rule-tester/rule-tester.js#L56
['const espreePath = require.resolve("espree");', ''],
// https://github.com/eslint/eslint/blob/v8.57.0/lib/rule-tester/rule-tester.js#L781
['config.parser = espreePath;', ''],
],
}),
Expand All @@ -42,6 +46,7 @@ await esbuild.build({
include: /node_modules[\/\\]eslint-module-utils[\/\\]resolve\.js$/,
pattern: [
[
// https://github.com/import-js/eslint-plugin-import/blob/v2.11.0/utils/resolve.js#L157
'tryRequire(`eslint-import-resolver-${name}`, sourceFile)',
'require("eslint-import-resolver-node")',
],
Expand All @@ -54,6 +59,7 @@ await esbuild.build({
include: /node_modules[\/\\]postcss-html[\/\\]extract\.js$/,
pattern: [
[
//https://github.com/ota-meshi/postcss-html/blob/v0.36.0/extract.js#L108
'style.syntax = loadSyntax(opts, __dirname);',
`style.syntax = {
parse: require("postcss-html/template-parse"),
Expand All @@ -73,6 +79,7 @@ await esbuild.build({
include: /node_modules[\/\\]stylelint[\/\\]lib[\/\\]lintPostcssResult\.js$/,
pattern: [
[
// https://github.com/stylelint/stylelint/blob/15.10.0/lib/lintPostcssResult.js#L52
"postcssDoc && postcssDoc.constructor.name === 'Document' ? postcssDoc.nodes : [postcssDoc]",
"postcssDoc && postcssDoc.constructor === require('postcss-syntax/document') ? postcssDoc.nodes : [postcssDoc]",
],
Expand Down
3 changes: 3 additions & 0 deletions packages/css/src/linter/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ export function createStylelintConfig(rules: RuleConfig[]): stylelint.Config {
}
}
return {
// We can pass just postcss function to the bundle and the module will resolve all plugins
// automatically. However, esbuild will not be able to resolve our dependencies. We pass them
// explicitly so that no dynamic requires happen at bundle time.
customSyntax: postcss({
html: postcssHtml,
scss: postcssScss,
Expand Down
2 changes: 1 addition & 1 deletion packages/jsts/src/builders/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function buildSourceCode(input: JsTsAnalysisInput, language: JsTsLanguage
return parseForESLint(
input.fileContent,
parser.parse,
buildParserOptions({ parser: vueFile ? parsers.javascript.parser : undefined }, true),
buildParserOptions({ parser: vueFile ? parsers.javascript : undefined }, true),
);
} catch (error) {
debug(`Failed to parse ${input.filePath} with Javascript parser: ${error.message}`);
Expand Down

0 comments on commit 1559ab8

Please sign in to comment.