2017-05-15 13:59:14 +00:00
"use strict" ;
/ * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Copyright ( c ) Microsoft Corporation . All rights reserved .
* Licensed under the MIT License . See License . txt in the project root for license information .
* -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- * /
var _ _extends = ( this && this . _ _extends ) || ( function ( ) {
var extendStatics = Object . setPrototypeOf ||
( { _ _proto _ _ : [ ] } instanceof Array && function ( d , b ) { d . _ _proto _ _ = b ; } ) ||
function ( d , b ) { for ( var p in b ) if ( b . hasOwnProperty ( p ) ) d [ p ] = b [ p ] ; } ;
return function ( d , b ) {
extendStatics ( d , b ) ;
function _ _ ( ) { this . constructor = d ; }
d . prototype = b === null ? Object . create ( b ) : ( _ _ . prototype = b . prototype , new _ _ ( ) ) ;
} ;
} ) ( ) ;
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
2017-06-16 10:06:51 +00:00
var ts = require ( "typescript" ) ;
2017-05-15 13:59:14 +00:00
var Lint = require ( "tslint" ) ;
var minimatch = require ( "minimatch" ) ;
2017-06-16 10:06:51 +00:00
var path _1 = require ( "path" ) ;
2017-09-04 09:02:19 +00:00
var Rule = /** @class */ ( function ( _super ) {
2017-05-15 13:59:14 +00:00
_ _extends ( Rule , _super ) ;
function Rule ( ) {
return _super !== null && _super . apply ( this , arguments ) || this ;
}
Rule . prototype . apply = function ( sourceFile ) {
var configs = this . getOptions ( ) . ruleArguments ;
for ( var _i = 0 , configs _1 = configs ; _i < configs _1 . length ; _i ++ ) {
var config = configs _1 [ _i ] ;
if ( minimatch ( sourceFile . fileName , config . target ) ) {
return this . applyWithWalker ( new ImportPatterns ( sourceFile , this . getOptions ( ) , config ) ) ;
}
}
return [ ] ;
} ;
return Rule ;
} ( Lint . Rules . AbstractRule ) ) ;
exports . Rule = Rule ;
2017-09-04 09:02:19 +00:00
var ImportPatterns = /** @class */ ( function ( _super ) {
2017-05-15 13:59:14 +00:00
_ _extends ( ImportPatterns , _super ) ;
function ImportPatterns ( file , opts , _config ) {
var _this = _super . call ( this , file , opts ) || this ;
_this . _config = _config ;
return _this ;
}
2017-06-16 10:06:51 +00:00
ImportPatterns . prototype . visitImportEqualsDeclaration = function ( node ) {
if ( node . moduleReference . kind === ts . SyntaxKind . ExternalModuleReference ) {
this . _validateImport ( node . moduleReference . expression . getText ( ) , node ) ;
}
} ;
2017-05-15 13:59:14 +00:00
ImportPatterns . prototype . visitImportDeclaration = function ( node ) {
2017-06-16 10:06:51 +00:00
this . _validateImport ( node . moduleSpecifier . getText ( ) , node ) ;
} ;
2017-07-14 08:05:33 +00:00
ImportPatterns . prototype . visitCallExpression = function ( node ) {
_super . prototype . visitCallExpression . call ( this , node ) ;
// import('foo') statements inside the code
if ( node . expression . kind === ts . SyntaxKind . ImportKeyword ) {
var path = node . arguments [ 0 ] ;
this . _validateImport ( path . getText ( ) , node ) ;
}
} ;
2017-06-16 10:06:51 +00:00
ImportPatterns . prototype . _validateImport = function ( path , node ) {
2017-05-15 13:59:14 +00:00
// remove quotes
path = path . slice ( 1 , - 1 ) ;
2017-06-16 10:06:51 +00:00
// resolve relative paths
2017-05-15 13:59:14 +00:00
if ( path [ 0 ] === '.' ) {
2017-06-16 10:06:51 +00:00
path = path _1 . join ( this . getSourceFile ( ) . fileName , path ) ;
2017-05-15 13:59:14 +00:00
}
2017-06-16 09:15:42 +00:00
var restrictions ;
if ( typeof this . _config . restrictions === 'string' ) {
restrictions = [ this . _config . restrictions ] ;
}
else {
restrictions = this . _config . restrictions ;
}
var matched = false ;
for ( var _i = 0 , restrictions _1 = restrictions ; _i < restrictions _1 . length ; _i ++ ) {
var pattern = restrictions _1 [ _i ] ;
if ( minimatch ( path , pattern ) ) {
matched = true ;
break ;
}
}
if ( ! matched ) {
// None of the restrictions matched
2017-06-20 14:08:02 +00:00
this . addFailure ( this . createFailure ( node . getStart ( ) , node . getWidth ( ) , "Imports violates '" + restrictions . join ( ' or ' ) + "' restrictions. See https://github.com/Microsoft/vscode/wiki/Code-Organization" ) ) ;
2017-05-15 13:59:14 +00:00
}
} ;
return ImportPatterns ;
} ( Lint . RuleWalker ) ) ;