Skip to content

Commit

Permalink
Fix microsoft#525 - safe/consistent .tsx file type checks
Browse files Browse the repository at this point in the history
  • Loading branch information
roblourens committed Oct 8, 2018
1 parent 76d0853 commit 19a1de1
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/noFunctionExpressionRule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

import { AstUtils } from './utils/AstUtils';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {ExtendedMetadata} from './utils/ExtendedMetadata';

Expand Down Expand Up @@ -37,7 +38,8 @@ class NoFunctionExpressionRuleWalker extends ErrorTolerantWalker {

constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
if (sourceFile.fileName.endsWith('tsx')) {

if (AstUtils.getLanguageVariant(sourceFile) === ts.LanguageVariant.JSX) {
this.allowGenericFunctionExpression = true;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/preferTypeCastRule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';

import { AstUtils } from './utils/AstUtils';
import {ErrorTolerantWalker} from './utils/ErrorTolerantWalker';
import {ExtendedMetadata} from './utils/ExtendedMetadata';

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

class PreferTypeCastRuleWalker extends ErrorTolerantWalker {
protected visitSourceFile(node: ts.SourceFile): void {
if (/.*\.tsx/.test(node.fileName) === false) {
if (AstUtils.getLanguageVariant(node) === ts.LanguageVariant.Standard) {
super.visitSourceFile(node);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/AstUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as ts from 'typescript';
export module AstUtils {

export function getLanguageVariant(node: ts.SourceFile): ts.LanguageVariant {
if (/.*\.tsx/i.test(node.fileName)) {
if (node.fileName.endsWith('.tsx')) {
return ts.LanguageVariant.JSX;
} else {
return ts.LanguageVariant.Standard;
Expand Down

0 comments on commit 19a1de1

Please sign in to comment.