lib/plugins/processor/post.ts

Summary

Maintainability
D
2 days
Test Coverage

Function processPost has 94 lines of code (exceeds 25 allowed). Consider refactoring.
Open

function processPost(ctx: Hexo, file: _File) {
  const Post = ctx.model('Post');
  const { path } = file.params;
  const doc = Post.findOne({source: file.path});
  const { config } = ctx;
Severity: Major
Found in lib/plugins/processor/post.ts - About 3 hrs to fix

    File post.ts has 253 lines of code (exceeds 250 allowed). Consider refactoring.
    Open

    import { toDate, timezone, isExcludedFile, isTmpFile, isHiddenFile, isMatch } from './common';
    import Promise from 'bluebird';
    import { parse as yfm } from 'hexo-front-matter';
    import { extname, join, posix, sep } from 'path';
    import { stat, listDir } from 'hexo-fs';
    Severity: Minor
    Found in lib/plugins/processor/post.ts - About 2 hrs to fix

      Function processAsset has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring.
      Open

      function processAsset(ctx: Hexo, file: _File) {
        const PostAsset = ctx.model('PostAsset');
        const Post = ctx.model('Post');
        const id = file.source.substring(ctx.base_dir.length);
        const postAsset = PostAsset.findById(id);
      Severity: Minor
      Found in lib/plugins/processor/post.ts - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Consider simplifying this complex logical expression.
      Open

          } else if (info && info.year && (info.month || info.i_month) && (info.day || info.i_day)) {
            data.date = new Date(
              info.year,
              parseInt(info.month || info.i_month, 10) - 1,
              parseInt(info.day || info.i_day, 10)
      Severity: Critical
      Found in lib/plugins/processor/post.ts - About 1 hr to fix

        Function processAsset has 33 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

        function processAsset(ctx: Hexo, file: _File) {
          const PostAsset = ctx.model('PostAsset');
          const Post = ctx.model('Post');
          const id = file.source.substring(ctx.base_dir.length);
          const postAsset = PostAsset.findById(id);
        Severity: Minor
        Found in lib/plugins/processor/post.ts - About 1 hr to fix

          Function parseFilename has 26 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

          function parseFilename(config: string, path: string) {
            config = config.substring(0, config.length - extname(config).length);
            path = path.substring(0, path.length - extname(path).length);
          
            if (!permalink || permalink.rule !== config) {
          Severity: Minor
          Found in lib/plugins/processor/post.ts - About 1 hr to fix

            Function scanAssetDir has 26 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

            function scanAssetDir(ctx: Hexo, post) {
              if (!ctx.config.post_asset_folder) return;
            
              const assetDir = post.asset_dir;
              const baseDir = ctx.base_dir;
            Severity: Minor
            Found in lib/plugins/processor/post.ts - About 1 hr to fix

              Avoid too many return statements within this function.
              Open

                  return postAsset.remove();
              Severity: Major
              Found in lib/plugins/processor/post.ts - About 30 mins to fix

                Function shouldSkipAsset has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring.
                Open

                function shouldSkipAsset(ctx: Hexo, post, asset) {
                  if (!ctx._showDrafts()) {
                    if (post.published === false && asset) {
                      // delete existing draft assets if draft posts are hidden
                      asset.remove();
                Severity: Minor
                Found in lib/plugins/processor/post.ts - About 25 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Identical blocks of code found in 2 locations. Consider refactoring.
                Open

                    if (data.updated) {
                      if (timezoneCfg) data.updated = timezone(data.updated, timezoneCfg);
                    } else if (updated_option === 'date') {
                      data.updated = data.date;
                    } else if (updated_option === 'empty') {
                Severity: Major
                Found in lib/plugins/processor/post.ts and 1 other location - About 3 hrs to fix
                lib/plugins/processor/asset.ts on lines 71..79

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 104.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 3 locations. Consider refactoring.
                Open

                    for (let i = 0, len = keys.length; i < len; i++) {
                      const key = keys[i];
                      if (!preservedKeys[key]) data[key] = info[key];
                    }
                Severity: Major
                Found in lib/plugins/processor/post.ts and 2 other locations - About 1 hr to fix
                lib/plugins/console/new.ts on lines 56..59
                lib/plugins/filter/new_post_path.ts on lines 75..78

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 68.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Identical blocks of code found in 2 locations. Consider refactoring.
                Open

                    if (doc) {
                      if (file.type !== 'update') {
                        ctx.log.warn(`Trying to "create" ${magenta(file.path)}, but the file already exists!`);
                      }
                      return doc.replace(data);
                Severity: Major
                Found in lib/plugins/processor/post.ts and 1 other location - About 1 hr to fix
                lib/plugins/processor/asset.ts on lines 100..105

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 65.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 2 locations. Consider refactoring.
                Open

                    if (data.date) {
                      if (timezoneCfg) data.date = timezone(data.date, timezoneCfg);
                    } else {
                      data.date = stats.birthtime;
                    }
                Severity: Minor
                Found in lib/plugins/processor/post.ts and 1 other location - About 55 mins to fix
                lib/plugins/processor/asset.ts on lines 63..67

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 54.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 3 locations. Consider refactoring.
                Open

                    if (data.category && !data.categories) {
                      data.categories = data.category;
                      data.category = undefined;
                    }
                Severity: Minor
                Found in lib/plugins/processor/post.ts and 2 other locations - About 35 mins to fix
                lib/plugins/processor/post.ts on lines 146..149
                lib/plugins/processor/post.ts on lines 157..160

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 47.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 3 locations. Consider refactoring.
                Open

                    if (data.tag && !data.tags) {
                      data.tags = data.tag;
                      data.tag = undefined;
                    }
                Severity: Minor
                Found in lib/plugins/processor/post.ts and 2 other locations - About 35 mins to fix
                lib/plugins/processor/post.ts on lines 141..144
                lib/plugins/processor/post.ts on lines 157..160

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 47.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                Similar blocks of code found in 3 locations. Consider refactoring.
                Open

                    if (data.photo && !data.photos) {
                      data.photos = data.photo;
                      data.photo = undefined;
                    }
                Severity: Minor
                Found in lib/plugins/processor/post.ts and 2 other locations - About 35 mins to fix
                lib/plugins/processor/post.ts on lines 141..144
                lib/plugins/processor/post.ts on lines 146..149

                Duplicated Code

                Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                Tuning

                This issue has a mass of 47.

                We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                Refactorings

                Further Reading

                There are no issues that match your filters.

                Category
                Status