diff --git a/tests/samples/codefiles/typescript.ts b/tests/samples/codefiles/typescript.ts new file mode 100644 index 0000000..157017f --- /dev/null +++ b/tests/samples/codefiles/typescript.ts @@ -0,0 +1,3 @@ +/** + * A Comment + */ diff --git a/tests/test_languages.py b/tests/test_languages.py index 530a5c9..5fe1d3e 100644 --- a/tests/test_languages.py +++ b/tests/test_languages.py @@ -198,3 +198,19 @@ class LanguagesTestCase(utils.TestCase): language = None self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language) + + def test_typescript_detected_correctly(self): + response = Response() + response.status_code = 500 + self.patched['wakatime.packages.requests.adapters.HTTPAdapter.send'].return_value = response + + now = u(int(time.time())) + config = 'tests/samples/configs/good_config.cfg' + entity = 'tests/samples/codefiles/typescript.ts' + args = ['--file', entity, '--config', config, '--time', now] + + retval = execute(args) + self.assertEquals(retval, 102) + + language = u('TypeScript') + self.assertEqual(self.patched['wakatime.offlinequeue.Queue.push'].call_args[0][0].get('language'), language) diff --git a/wakatime/packages/__init__.py b/wakatime/packages/__init__.py index d02f693..e69de29 100644 --- a/wakatime/packages/__init__.py +++ b/wakatime/packages/__init__.py @@ -1,4 +0,0 @@ -import tzlocal -from pygments.lexers import get_lexer_by_name, guess_lexer_for_filename -from pygments.modeline import get_filetype_from_buffer -from pygments.util import ClassNotFound diff --git a/wakatime/packages/pygments/lexers/javascript.py b/wakatime/packages/pygments/lexers/javascript.py index 862535c..554ec47 100644 --- a/wakatime/packages/pygments/lexers/javascript.py +++ b/wakatime/packages/pygments/lexers/javascript.py @@ -450,6 +450,7 @@ class TypeScriptLexer(RegexLexer): aliases = ['ts', 'typescript'] filenames = ['*.ts', '*.tsx'] mimetypes = ['text/x-typescript'] + priority = 0.11 flags = re.DOTALL | re.MULTILINE diff --git a/wakatime/stats.py b/wakatime/stats.py index 252261c..1fa8df7 100644 --- a/wakatime/stats.py +++ b/wakatime/stats.py @@ -16,12 +16,12 @@ import sys from .compat import u, open from .dependencies import DependencyParser -from .packages import ( +from .packages.pygments.lexers import ( + ClassNotFound, get_lexer_by_name, guess_lexer_for_filename, - get_filetype_from_buffer, ) -from .packages.pygments.lexers import ClassNotFound +from .packages.pygments.modeline import get_filetype_from_buffer try: from .packages import simplejson as json # pragma: nocover @@ -32,6 +32,32 @@ except (ImportError, SyntaxError): # pragma: nocover log = logging.getLogger('WakaTime') +def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None, + plugin=None, alternate_language=None): + if entity_type != 'file': + stats = { + 'language': None, + 'dependencies': [], + 'lines': None, + 'lineno': lineno, + 'cursorpos': cursorpos, + } + else: + language, lexer = guess_language(file_name) + parser = DependencyParser(file_name, lexer) + dependencies = parser.parse() + if language is None and alternate_language: + language = standardize_language(alternate_language, plugin) + stats = { + 'language': language, + 'dependencies': dependencies, + 'lines': number_lines_in_file(file_name), + 'lineno': lineno, + 'cursorpos': cursorpos, + } + return stats + + def guess_language(file_name): """Guess lexer and language for a file. @@ -79,13 +105,13 @@ def guess_lexer_using_filename(file_name, text): try: lexer = guess_lexer_for_filename(file_name, text) - except: # pragma: nocover + except: pass if lexer is not None: try: accuracy = lexer.analyse_text(text) - except: # pragma: nocover + except: pass return lexer, accuracy @@ -159,32 +185,6 @@ def number_lines_in_file(file_name): return lines -def get_file_stats(file_name, entity_type='file', lineno=None, cursorpos=None, - plugin=None, alternate_language=None): - if entity_type != 'file': - stats = { - 'language': None, - 'dependencies': [], - 'lines': None, - 'lineno': lineno, - 'cursorpos': cursorpos, - } - else: - language, lexer = guess_language(file_name) - parser = DependencyParser(file_name, lexer) - dependencies = parser.parse() - if language is None and alternate_language: - language = standardize_language(alternate_language, plugin) - stats = { - 'language': language, - 'dependencies': dependencies, - 'lines': number_lines_in_file(file_name), - 'lineno': lineno, - 'cursorpos': cursorpos, - } - return stats - - def standardize_language(language, plugin): """Maps a string to the equivalent Pygments language."""