Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit ac395d8

Browse files
roblourensJosh Goldberg
authored andcommitted
Fix #525 - safe/consistent .tsx file type checks (#526)
🙌 Thanks @roblourens!
1 parent 76d0853 commit ac395d8

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

src/noFunctionExpressionRule.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import * as Lint from 'tslint';
33

4+
import { AstUtils } from './utils/AstUtils';
45
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
56
import {ExtendedMetadata} from './utils/ExtendedMetadata';
67

@@ -37,7 +38,8 @@ class NoFunctionExpressionRuleWalker extends ErrorTolerantWalker {
3738

3839
constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
3940
super(sourceFile, options);
40-
if (sourceFile.fileName.endsWith('tsx')) {
41+
42+
if (AstUtils.getLanguageVariant(sourceFile) === ts.LanguageVariant.JSX) {
4143
this.allowGenericFunctionExpression = true;
4244
}
4345
}

src/preferTypeCastRule.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as ts from 'typescript';
22
import * as Lint from 'tslint';
33

4+
import { AstUtils } from './utils/AstUtils';
45
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
56
import {ExtendedMetadata} from './utils/ExtendedMetadata';
67

@@ -34,7 +35,7 @@ export class Rule extends Lint.Rules.AbstractRule {
3435

3536
class PreferTypeCastRuleWalker extends ErrorTolerantWalker {
3637
protected visitSourceFile(node: ts.SourceFile): void {
37-
if (/.*\.tsx/.test(node.fileName) === false) {
38+
if (AstUtils.getLanguageVariant(node) === ts.LanguageVariant.Standard) {
3839
super.visitSourceFile(node);
3940
}
4041
}

src/utils/AstUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as ts from 'typescript';
66
export module AstUtils {
77

88
export function getLanguageVariant(node: ts.SourceFile): ts.LanguageVariant {
9-
if (/.*\.tsx/i.test(node.fileName)) {
9+
if (node.fileName.endsWith('.tsx')) {
1010
return ts.LanguageVariant.JSX;
1111
} else {
1212
return ts.LanguageVariant.Standard;

0 commit comments

Comments
 (0)