-
-
Notifications
You must be signed in to change notification settings - Fork 40
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: add the ability to configure typescript to javascript file extension conversion #112
Changes from all commits
3793b1e
0783c34
af3268e
d3eedc2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"use strict" | ||
|
||
const DEFAULT_MAPPING = normalise([ | ||
["", ".js"], | ||
[".ts", ".js"], | ||
[".cts", ".cjs"], | ||
[".mts", ".mjs"], | ||
[".tsx", ".jsx"], | ||
]) | ||
|
||
/** | ||
* @typedef {Object} ExtensionMap | ||
* @property {Record<string, string>} forward Convert from typescript to javascript | ||
* @property {Record<string, string[]>} backward Convert from javascript to typescript | ||
*/ | ||
|
||
function normalise(typescriptExtensionMap) { | ||
const forward = {} | ||
const backward = {} | ||
for (const [typescript, javascript] of typescriptExtensionMap) { | ||
forward[typescript] = javascript | ||
if (!typescript) { | ||
continue | ||
} | ||
backward[javascript] ??= [] | ||
backward[javascript].push(typescript) | ||
} | ||
return { forward, backward } | ||
} | ||
|
||
/** | ||
* Gets `typescriptExtensionMap` property from a given option object. | ||
* | ||
* @param {object|undefined} option - An option object to get. | ||
* @returns {ExtensionMap} The `typescriptExtensionMap` value, or `null`. | ||
*/ | ||
function get(option) { | ||
if ( | ||
option && | ||
option.typescriptExtensionMap && | ||
Array.isArray(option.typescriptExtensionMap) | ||
Comment on lines
+39
to
+41
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. nits: can be simply rewritten to 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. Oh, I am totally going to fix this in another PR! I just followed what was there 👀 |
||
) { | ||
return normalise(option.typescriptExtensionMap) | ||
} | ||
|
||
return null | ||
} | ||
|
||
/** | ||
* Gets "typescriptExtensionMap" setting. | ||
* | ||
* 1. This checks `options` property, then returns it if exists. | ||
* 2. This checks `settings.n` | `settings.node` property, then returns it if exists. | ||
* 3. This returns `DEFAULT_MAPPING`. | ||
* | ||
* @param {import('eslint').Rule.RuleContext} context - The rule context. | ||
* @returns {string[]} A list of extensions. | ||
*/ | ||
module.exports = function getTypescriptExtensionMap(context) { | ||
return ( | ||
get(context.options && context.options[0]) || | ||
get( | ||
context.settings && (context.settings.n || context.settings.node) | ||
) || | ||
// TODO: Detect tsconfig.json here | ||
DEFAULT_MAPPING | ||
) | ||
} | ||
|
||
module.exports.schema = { | ||
type: "array", | ||
items: { | ||
type: "array", | ||
prefixItems: [ | ||
{ type: "string", pattern: "^(?:|\\.\\w+)$" }, | ||
{ type: "string", pattern: "^\\.\\w+$" }, | ||
], | ||
additionalItems: false, | ||
}, | ||
uniqueItems: true, | ||
} |
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.
To avoid duplication, I'm just wondering whether we could add a section to the readme that describes the shared settings. In the rule docs we can add a link to that. (surely not blocking merging this PR. 😄 )