Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get the real filename to avoid rule invalidation #1852

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/rules/multi-word-component-names.js
Expand Up @@ -111,7 +111,7 @@ module.exports = {
'Program:exit'(node) {
if (hasName) return
if (!hasVue && node.body.length > 0) return
const fileName = context.getFilename()
const fileName = utils.getPhysicalFilename(context)
const componentName = path.basename(fileName, path.extname(fileName))
if (
utils.isVueFile(fileName) &&
Expand Down
6 changes: 3 additions & 3 deletions lib/rules/no-multi-spaces.js
Expand Up @@ -4,7 +4,7 @@
*/
'use strict'

const path = require('path')
const utils = require('../utils')

// ------------------------------------------------------------------------------
// Rule Definition
Expand Down Expand Up @@ -52,8 +52,8 @@ module.exports = {
return {
Program(node) {
if (context.parserServices.getTemplateBodyTokenStore == null) {
const filename = context.getFilename()
if (path.extname(filename) === '.vue') {
const filename = utils.getPhysicalFilename(context)
if (utils.isVueFile(filename)) {
context.report({
loc: { line: 1, column: 0 },
message:
Expand Down
2 changes: 1 addition & 1 deletion lib/rules/require-direct-export.js
Expand Up @@ -31,7 +31,7 @@ module.exports = {
},
/** @param {RuleContext} context */
create(context) {
const filePath = context.getFilename()
const filePath = utils.getPhysicalFilename(context)
if (!utils.isVueFile(filePath)) return {}

const disallowFunctional = (context.options[0] || {})
Expand Down
4 changes: 3 additions & 1 deletion lib/utils/indent-common.js
Expand Up @@ -33,6 +33,7 @@ const {
last
} = require('./indent-utils')
const { defineVisitor: tsDefineVisitor } = require('./indent-ts')
const utils = require('../utils')

/**
* @typedef {import('../../typings/eslint-plugin-vue/util-types/node').HasLocation} HasLocation
Expand Down Expand Up @@ -205,7 +206,8 @@ module.exports.defineVisitor = function create(
tokenStore,
defaultOptions
) {
if (!context.getFilename().endsWith('.vue')) return {}
const filename = utils.getPhysicalFilename(context)
if (!utils.isVueFile(filename)) return {}

const options = parseOptions(
context.options[0],
Expand Down
39 changes: 33 additions & 6 deletions lib/utils/index.js
Expand Up @@ -311,6 +311,18 @@ function wrapContextToOverrideReportMethodToSkipDynamicArgument(context) {
})
}

/**
* Return the full path of the file on disk, if not set, it will fallback to `context.getFilename()`.
* @param {RuleContext} context The ESLint rule context.
* @returns {string}
*/
function getPhysicalFilename(context) {
if (context.getPhysicalFilename) {
return context.getPhysicalFilename()
}
return context.getFilename()
}

// ------------------------------------------------------------------------------
// Exports
// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -1891,7 +1903,14 @@ module.exports = {
}

return true
}
},

/**
* Return the full path of the file on disk, if not set, it will fallback to `context.getFilename()`.
* @param {RuleContext} context The ESLint rule context.
* @returns {string}
*/
getPhysicalFilename
}

// ------------------------------------------------------------------------------
Expand Down Expand Up @@ -1987,8 +2006,8 @@ function defineTemplateBodyVisitor(
options
) {
if (context.parserServices.defineTemplateBodyVisitor == null) {
const filename = context.getFilename()
if (path.extname(filename) === '.vue') {
const filename = getPhysicalFilename(context)
if (isVueFile(filename)) {
context.report({
loc: { line: 1, column: 0 },
message:
Expand All @@ -2015,8 +2034,8 @@ function defineTemplateBodyVisitor(
*/
function defineDocumentVisitor(context, documentVisitor, options) {
if (context.parserServices.defineDocumentVisitor == null) {
const filename = context.getFilename()
if (path.extname(filename) === '.vue') {
const filename = getPhysicalFilename(context)
if (isVueFile(filename)) {
context.report({
loc: { line: 1, column: 0 },
message:
Expand Down Expand Up @@ -2373,6 +2392,14 @@ function getVExpressionContainer(node) {
* @param {string} path
*/
function isVueFile(path) {
/**
* When `path` is `context.getFilename()`, it returns the filename associated with the source or `<input>` if not specified.
* When `path` is `context.getPhysicalFilename()`, it returns the full path of the file on disk or `<text>` if not specified.
* If filename is not specified, we assume it's a vue file.
*/
if (['<input>', '<text>'].includes(path)) {
return true
}
return path.endsWith('.vue') || path.endsWith('.jsx')
}

Expand Down Expand Up @@ -2583,7 +2610,7 @@ function isSFCObject(context, node) {
if (node.type !== 'ObjectExpression') {
return false
}
const filePath = context.getFilename()
const filePath = getPhysicalFilename(context)
const ext = path.extname(filePath)
if (ext !== '.vue' && ext) {
return false
Expand Down
9 changes: 9 additions & 0 deletions typings/eslint/index.d.ts
Expand Up @@ -329,6 +329,15 @@ export namespace Rule {

// eslint@6 does not have this method.
getCwd?: () => string

/**
* When linting a file, it returns the full path of the file on disk without any code block information.
* When linting text, it returns the value passed to `—stdin-filename` or `<text>` if not specified.
* If not set, it will fallback to `getFilename()`
* This was added in eslint@7.28.0
* @since 7.28.0
*/
getPhysicalFilename?: () => string
}

type ReportDescriptor =
Expand Down