Skip to content

Commit

Permalink
Add support for extension .mjs in no-unsupported-configs rule (cloudf…
Browse files Browse the repository at this point in the history
  • Loading branch information
mattiasw authored Nov 13, 2023
1 parent 48796ca commit c9167ea
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/spotty-buckets-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-next-on-pages': patch
---

Support for file extensions .mjs in no-unsupported-configs ESLint rule
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const ruleSchema = {
const rule: Rule.RuleModule = {
create: context => {
const code = context.sourceCode;
const exportedConfigName = context.filename.match(/next\.config\.js$/)
const exportedConfigName = context.filename.match(/next\.config\.m?js$/)
? getConfigVariableName(code)
: null;

Expand Down Expand Up @@ -217,6 +217,10 @@ function getConfigVariableName(code: SourceCode): string | null {
if (exportedValue?.type === 'Identifier') {
return exportedValue.name;
}
const esmExportedValue = extractESMExportValue(node);
if (esmExportedValue?.type === 'Identifier') {
return esmExportedValue.name;
}
}

const nodeAfterNextConfigComment = getNodeAfterNextConfigTypeComment(code);
Expand Down Expand Up @@ -273,3 +277,15 @@ function extractModuleExportValue(node: Node): Node | null {

return null;
}

/**
* Gets the value of a node potentially representing: `export default ...`
* Returns the node of the value, or null if the input node doesn't represent the code
*/
function extractESMExportValue(node: Node): Node | null {
if (node.type === 'ExportDefaultDeclaration') {
return node.declaration;
}

return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ describe('no-unsupported-configs', () => {
/**
*
* @type {import( "next" ) .
* @type {import( "next" ) .
* NextConfig}
*
*
* The following is my config object
* */
/* this is another comment, surprisingly it doesn't break the type import! */
const nextConfig = {
experimental: {
Expand Down Expand Up @@ -463,4 +463,38 @@ describe('no-unsupported-configs', () => {
],
});
});

test('should work with .mjs config file extension', () => {
tester.run('', rule, {
valid: [
{
filename: 'next.config.mjs',
code: `
const nextConfig = {
}
export default nextConfig;
`,
},
],
invalid: [
{
filename: 'next.config.mjs',
code: `
const nextConfig = {
compress: true,
}
export default nextConfig;
`,
errors: [
{
message:
'The "compress" configuration is not supported by next-on-pages (and is unlikely to be supported in the future).',
},
],
},
],
});
});
});

0 comments on commit c9167ea

Please sign in to comment.