-
-
Notifications
You must be signed in to change notification settings - Fork 669
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
feat: fixable match-component-file-name rule #1874
feat: fixable match-component-file-name rule #1874
Conversation
vue-eslint-parser's ESLintLiteralBase Node has 'raw' property. ref: https://github.com/vuejs/vue-eslint-parser/blob/160f4efc4eaf363662b464a4a26a4c9e514deb5d/src/ast/nodes.ts#L395 It was necessary to identify quotes in the match-component-file-name's fixer.
@@ -102,19 +102,25 @@ module.exports = { | |||
*/ | |||
function verifyName(node) { | |||
let name | |||
let quote = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defining let quote
, resulted in a typescript error.
Variable 'quote' implicitly has type 'any' in some locations where its type cannot be determined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could instead set a type without initializing the variable:
/** @type {string} */
let quote
// equivalent to the following Typescript syntax:
let quote: string
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But maybe it would make sense to only compute the quote in the fixer function:
fix(fixer) {
const quote = node.type === 'TemplateLiteral' ? '`' : node.raw[0]
return fixer.replaceText(node, `${quote}${filename}${quote}`)
}
Then you don't have to initialize it and the meaning of the variable is immediately clear from the context.
@@ -347,6 +347,7 @@ export interface PrivateIdentifier extends HasParentNode { | |||
export interface Literal extends HasParentNode { | |||
type: 'Literal' | |||
value: string | boolean | null | number | RegExp | BigInt | |||
raw: string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log(node)
, the following object can be displayed.
The fixer use the raw
property to determine the single/doublequotes.
<ref *2> Node {
type: 'Literal',
start: 38,
end: 51,
loc: SourceLocation {
start: { line: 3, column: 10 },
end: { line: 3, column: 23 }
},
range: [ 38, 51 ],
value: 'MyComponent',
raw: "'MyComponent'",
parent: <ref *1> Node {
type: 'Property',
start: 32,
end: 51,
loc: SourceLocation { start: [Object], end: [Object] },
range: [ 32, 51 ],
method: false,
shorthand: false,
computed: false,
key: Node {
type: 'Identifier',
start: 32,
end: 36,
loc: [SourceLocation],
range: [Array],
name: 'name',
parent: [Circular *1]
},
value: [Circular *2],
kind: 'init',
parent: Node {
type: 'ObjectExpression',
start: 26,
end: 110,
loc: [SourceLocation],
range: [Array],
properties: [Array],
parent: [Node]
}
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found Literal
's raw
property is defined here.
fix(fixer) { | ||
return fixer.replaceText(node, `${quote}${filename}${quote}`) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should not be an auto-fix, since it's sometimes the case that the filename is wrong and the name property is correct.
Instead, please change this to be a suggestion instead. Then cases where the filename is correct and the name property is wrong can be easily corrected in the editor, without causing wrong fixes when running from the commandline.
The suggestion description should be "Rename component to match file name.", as described in the issue #1816.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much. I understand the reason to using suggest instead of fix.
@@ -102,19 +102,25 @@ module.exports = { | |||
*/ | |||
function verifyName(node) { | |||
let name | |||
let quote = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could instead set a type without initializing the variable:
/** @type {string} */
let quote
// equivalent to the following Typescript syntax:
let quote: string
@@ -102,19 +102,25 @@ module.exports = { | |||
*/ | |||
function verifyName(node) { | |||
let name | |||
let quote = '' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But maybe it would make sense to only compute the quote in the fixer function:
fix(fixer) {
const quote = node.type === 'TemplateLiteral' ? '`' : node.raw[0]
return fixer.replaceText(node, `${quote}${filename}${quote}`)
}
Then you don't have to initialize it and the meaning of the variable is immediately clear from the context.
Thanks for your first contribution! I have a few comments, see above. After you're done, please run |
Very thanks for the review!! I will confirm comments, and fix them. |
2bd2cb4
to
0a9a584
Compare
I force pushed commit. Could you please review it? I have checked the operation of 12:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this PR!
Almost LGTM! I have only one request to change the test script.
} | ||
] | ||
], | ||
output: null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove output
as it is no longer needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very thanks for the review!!
I removed output
, and run update the document.
improve vuejs#1816 For the following reasons why name option changed. If change file name, alto need to change import statement. Also, this rule not auto-fixed, because the file name may be incorrect.
0a9a584
to
4ff89bb
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! thank you!
fix #1816
This plugin has always helped me.
Thank you very much.
There are two fixes.