This repository has been archived by the owner on Aug 18, 2021. It is now read-only.
add support for preset & plugins in babelOptions #784
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
"use strict"; | ||
|
||
const eslint = require("eslint"); | ||
const unpad = require("dedent"); | ||
|
||
const parser = require("../.."); | ||
|
||
function verifyAndAssertMessagesWithSpecificESLint( | ||
code, | ||
rules, | ||
expectedMessages, | ||
sourceType, | ||
overrideConfig, | ||
linter | ||
) { | ||
const config = { | ||
parser: "current-babel-eslint", | ||
rules, | ||
env: { | ||
node: true, | ||
es6: true, | ||
}, | ||
parserOptions: { | ||
sourceType, | ||
ecmaFeatures: { | ||
globalReturn: true, | ||
}, | ||
}, | ||
}; | ||
|
||
if (overrideConfig) { | ||
for (const key in overrideConfig) { | ||
config[key] = overrideConfig[key]; | ||
} | ||
} | ||
|
||
const messages = linter.verify(code, config); | ||
|
||
if (messages.length !== expectedMessages.length) { | ||
throw new Error( | ||
`Expected ${expectedMessages.length} message(s), got ${ | ||
messages.length | ||
}\n${JSON.stringify(messages, null, 2)}` | ||
); | ||
} | ||
|
||
messages.forEach((message, i) => { | ||
const formatedMessage = `${message.line}:${message.column} ${ | ||
message.message | ||
}${message.ruleId ? ` ${message.ruleId}` : ""}`; | ||
const expectedMessage = expectedMessages[i]; | ||
if (typeof expectedMessage === "string") { | ||
if (formatedMessage !== expectedMessage) { | ||
throw new Error( | ||
` | ||
Message ${i} does not match: | ||
Expected: ${expectedMessage} | ||
Actual: ${formatedMessage} | ||
` | ||
); | ||
} | ||
} else if ( | ||
typeof expectedMessage === "object" && | ||
expectedMessage !== null && | ||
typeof expectedMessage.matcher === "function" && | ||
typeof expectedMessage.message === "string" | ||
) { | ||
if (!expectedMessage.matcher(formatedMessage)) { | ||
throw new Error( | ||
` | ||
Message ${i} does not match: | ||
Expected: ${expectedMessage.message} | ||
Actual: ${formatedMessage} | ||
` | ||
); | ||
} | ||
} else { | ||
throw new Error( | ||
"expected messages must be string or object { matcher: function, message: string }: " + | ||
i + | ||
" -> " + | ||
typeof expectedMessage[i] | ||
); | ||
} | ||
}); | ||
} | ||
|
||
function verifyAndAssertMessages( | ||
code, | ||
rules, | ||
expectedMessages, | ||
sourceType, | ||
overrideConfig | ||
) { | ||
const linter = new eslint.Linter(); | ||
linter.defineParser("current-babel-eslint", parser); | ||
|
||
verifyAndAssertMessagesWithSpecificESLint( | ||
unpad(`${code}`), | ||
rules || {}, | ||
expectedMessages || [], | ||
sourceType, | ||
overrideConfig, | ||
linter | ||
); | ||
} | ||
|
||
describe("babel options", () => { | ||
it("uses the plugins and presets specified in the options", () => { | ||
const code = "class AClass { classProp = 123 };"; | ||
|
||
const rules = { "no-unused-vars": 1, semi: 1 }; | ||
|
||
const babelOptions = { | ||
root: __dirname, | ||
rootMode: "root", | ||
babelrc: false, | ||
presets: ["@babel/preset-env"], | ||
plugins: [], | ||
}; | ||
|
||
verifyAndAssertMessages( | ||
code, | ||
rules, | ||
[ | ||
{ | ||
matcher: message => message.startsWith("1:25 Parsing error:"), | ||
message: "1:25 Parsing error: <path> <rest of message>", | ||
}, | ||
], | ||
undefined, | ||
{ | ||
parserOptions: { | ||
requireConfigFile: false, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it expected that this option would always be used in conjunction with We should also probably update the README if we're going to make this change. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you're not using a babel config file then you need to disable |
||
babelOptions: { | ||
...babelOptions, | ||
presets: ["@babel/preset-env"], | ||
plugins: [], | ||
}, | ||
}, | ||
} | ||
); | ||
|
||
verifyAndAssertMessages( | ||
code, | ||
rules, | ||
["1:7 'AClass' is defined but never used. no-unused-vars"], | ||
undefined, | ||
{ | ||
parserOptions: { | ||
requireConfigFile: false, | ||
babelOptions: { | ||
...babelOptions, | ||
presets: ["@babel/preset-env"], | ||
plugins: ["@babel/plugin-proposal-class-properties"], | ||
}, | ||
}, | ||
} | ||
); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This error message is a bit confusing to me - is that what's coming from Babel's parser?
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.
yes, it's the error from Babel.
To show that the new feature is working, I had to devise a test case showing that the babel configuration gets changed by the provided config.
Unfortunately babel's error messages contain the full path to the file (which is machine dependent) so I couldn't just match the strings exactly.
And in fact, showing that the correct lint error is shown in one case (with correct babel config) and that babel errors (and the exact error text doesn't matter) in the other case seemed like an adequate test.
I'm all ears if someone has suggestions for a better way to test the feature though...