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

TSLint #264

Closed
LinusU opened this issue Aug 9, 2018 · 5 comments
Closed

TSLint #264

LinusU opened this issue Aug 9, 2018 · 5 comments

Comments

@LinusU
Copy link
Member

LinusU commented Aug 9, 2018

Hey,

I'm adding support for TSLint, and it turns out that the code is basically exactly the same as for ESLint.

--- src/special/eslint.js	2018-08-09 13:33:55.000000000 +0100
+++ src/special/tslint.js	2018-08-09 13:45:28.000000000 +0100
@@ -101,10 +101,10 @@
 
 function checkConfig(config, rootDir) {
   const parser = wrapToArray(config.parser);
-  const plugins = wrapToArray(config.plugins).map(plugin => `eslint-plugin-${plugin}`);
+  const plugins = wrapToArray(config.plugins).map(plugin => `tslint-plugin-${plugin}`);
 
   const presets = wrapToArray(config.extends)
-    .filter(preset => preset !== 'eslint:recommended')
+    .filter(preset => preset !== 'tslint:recommended')
     .map(preset => resolvePresetPackage(preset, rootDir));
 
   const presetPackages = presets
@@ -120,9 +120,9 @@
   return lodash.union(parser, plugins, presetPackages, presetDeps);
 }
 
-export default function parseESLint(content, filename, deps, rootDir) {
+export default function parseTSLint(content, filename, deps, rootDir) {
   const basename = path.basename(filename);
-  if (/^\.eslintrc(\.json|\.js|\.yml|\.yaml)?$/.test(basename)) {
+  if (/^\.tslintrc(\.json|\.js|\.yml|\.yaml)?$/.test(basename)) {
     const config = parse(content);
     return checkConfig(config, rootDir);
   }

How would you prefer this to be done?

  1. Keep as two files with basically the same content
  2. Extract the code out to e.g. src/util/linters.js and just have a basic call in specials/*.js
  3. Just have eslint also check for tslint files/packages

Cheers 🍻

@rumpl
Copy link
Member

rumpl commented Aug 9, 2018

Option 1 seems to be the most future-proof no?

@LinusU
Copy link
Member Author

LinusU commented Aug 9, 2018

Probably, yes. Although I don't think TSLint will move far away from ESLint, the idea behind the project is to work the same but for TypeScript.

Here is how option 2 would look like:

--- src/special/eslint.js	2018-08-09 13:33:55.000000000 +0100
+++ src/utils/linters.js	2018-08-09 14:08:52.000000000 +0100
@@ -37,51 +37,51 @@
   return [obj];
 }
 
-function isEslintConfigAnAbsolutePath(specifier) {
+function isLinterConfigAnAbsolutePath(specifier) {
   return path.isAbsolute(specifier);
 }
 
-function isEslintConfigARelativePath(specifier) {
+function isLinterConfigARelativePath(specifier) {
   return lodash.startsWith(specifier, './') || lodash.startsWith(specifier, '../');
 }
 
-function isEslintConfigFromAPlugin(specifier) {
+function isLinterConfigFromAPlugin(specifier) {
   return lodash.startsWith(specifier, 'plugin:');
 }
 
-function isEslintConfigFromAScopedModule(specifier) {
+function isLinterConfigFromAScopedModule(specifier) {
   return lodash.startsWith(specifier, '@');
 }
 
-function isEslintConfigFromAFullyQualifiedModuleName(specifier, prefix) {
+function isLinterConfigFromAFullyQualifiedModuleName(specifier, prefix) {
   return lodash.startsWith(specifier, prefix);
 }
 
-function resolvePresetPackage(preset, rootDir) {
+function resolvePresetPackage(flavour, preset, rootDir) {
   // inspired from https://github.com/eslint/eslint/blob/5b4a94e26d0ef247fe222dacab5749805d9780dd/lib/config/config-file.js#L347
-  if (isEslintConfigAnAbsolutePath(preset)) {
+  if (isLinterConfigAnAbsolutePath(preset)) {
     return preset;
   }
-  if (isEslintConfigARelativePath(preset)) {
+  if (isLinterConfigARelativePath(preset)) {
     return path.resolve(rootDir, preset);
   }
 
   const { prefix, specifier } = (
-    isEslintConfigFromAPlugin(preset)
-    ? { prefix: 'eslint-plugin-', specifier: preset.substring(preset.indexOf(':') + 1) }
-    : { prefix: 'eslint-config-', specifier: preset }
+    isLinterConfigFromAPlugin(preset)
+    ? { prefix: `${flavour}-plugin-`, specifier: preset.substring(preset.indexOf(':') + 1) }
+    : { prefix: `${flavour}-config-`, specifier: preset }
   );
 
-  if (isEslintConfigFromAScopedModule(specifier)) {
+  if (isLinterConfigFromAScopedModule(specifier)) {
     const scope = specifier.substring(0, specifier.indexOf('/'));
     const module = specifier.substring(specifier.indexOf('/') + 1);
 
-    if (isEslintConfigFromAFullyQualifiedModuleName(module, prefix)) {
+    if (isLinterConfigFromAFullyQualifiedModuleName(module, prefix)) {
       return specifier;
     }
     return `${scope}/${prefix}${module}`;
   }
-  if (isEslintConfigFromAFullyQualifiedModuleName(specifier, prefix)) {
+  if (isLinterConfigFromAFullyQualifiedModuleName(specifier, prefix)) {
     return specifier;
   }
   return `${prefix}${specifier}`;
@@ -99,13 +99,13 @@
   }
 }
 
-function checkConfig(config, rootDir) {
+function checkConfig(flavour, config, rootDir) {
   const parser = wrapToArray(config.parser);
-  const plugins = wrapToArray(config.plugins).map(plugin => `eslint-plugin-${plugin}`);
+  const plugins = wrapToArray(config.plugins).map(plugin => `${flavour}-plugin-${plugin}`);
 
   const presets = wrapToArray(config.extends)
-    .filter(preset => preset !== 'eslint:recommended')
-    .map(preset => resolvePresetPackage(preset, rootDir));
+    .filter(preset => preset !== `${flavour}:recommended`)
+    .map(preset => resolvePresetPackage(flavour, preset, rootDir));
 
   const presetPackages = presets
     .filter(preset => !path.isAbsolute(preset))
@@ -120,11 +120,12 @@
   return lodash.union(parser, plugins, presetPackages, presetDeps);
 }
 
-export default function parseESLint(content, filename, deps, rootDir) {
+export default function parseLinter(flavour, content, filename, deps, rootDir) {
   const basename = path.basename(filename);
-  if (/^\.eslintrc(\.json|\.js|\.yml|\.yaml)?$/.test(basename)) {
+  const filenameRegex = new RegExp(`^\\.${flavour}rc(\\.json|\\.js|\\.yml|\\.yaml)?$`);
+  if (filenameRegex.test(basename)) {
     const config = parse(content);
-    return checkConfig(config, rootDir);
+    return checkConfig(flavour, config, rootDir);
   }
 
   return [];

@rumpl
Copy link
Member

rumpl commented Aug 9, 2018

Go for option 2 then, less code we have the better :)

@LinusU
Copy link
Member Author

LinusU commented Aug 9, 2018

Here we go 🚀 #266

@mnkhouri
Copy link
Member

Fixed in #266, #270 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants