@@ -186,6 +186,7 @@ async fn analyze_package_inner(
186
186
. analyzer
187
187
. get_parsed_source ( module. specifier ( ) )
188
188
{
189
+ check_for_banned_extensions ( & parsed_source) ?;
189
190
check_for_banned_syntax ( & parsed_source) ?;
190
191
check_for_banned_triple_slash_directives ( & parsed_source) ?;
191
192
}
@@ -802,6 +803,21 @@ fn collect_dependencies(
802
803
Ok ( dependencies)
803
804
}
804
805
806
+ fn check_for_banned_extensions (
807
+ parsed_source : & ParsedSource ,
808
+ ) -> Result < ( ) , PublishError > {
809
+ match parsed_source. media_type ( ) {
810
+ deno_ast:: MediaType :: Cjs | deno_ast:: MediaType :: Cts => {
811
+ Err ( PublishError :: CommonJs {
812
+ specifier : parsed_source. specifier ( ) . to_string ( ) ,
813
+ line : 0 ,
814
+ column : 0 ,
815
+ } )
816
+ }
817
+ _ => Ok ( ( ) ) ,
818
+ }
819
+ }
820
+
805
821
fn check_for_banned_syntax (
806
822
parsed_source : & ParsedSource ,
807
823
) -> Result < ( ) , PublishError > {
@@ -957,8 +973,15 @@ fn check_for_banned_triple_slash_directives(
957
973
#[ cfg( test) ]
958
974
mod tests {
959
975
fn parse ( source : & str ) -> deno_ast:: ParsedSource {
960
- let specifier = deno_ast:: ModuleSpecifier :: parse ( "file:///mod.ts" ) . unwrap ( ) ;
961
976
let media_type = deno_ast:: MediaType :: TypeScript ;
977
+ parse_with_media_type ( source, media_type)
978
+ }
979
+
980
+ fn parse_with_media_type (
981
+ source : & str ,
982
+ media_type : deno_ast:: MediaType ,
983
+ ) -> deno_ast:: ParsedSource {
984
+ let specifier = deno_ast:: ModuleSpecifier :: parse ( "file:///mod.ts" ) . unwrap ( ) ;
962
985
deno_ast:: parse_module ( deno_ast:: ParseParams {
963
986
specifier,
964
987
text : source. into ( ) ,
@@ -970,6 +993,27 @@ mod tests {
970
993
. unwrap ( )
971
994
}
972
995
996
+ #[ test]
997
+ fn banned_extensions ( ) {
998
+ let x =
999
+ parse_with_media_type ( "let x = 1;" , deno_ast:: MediaType :: TypeScript ) ;
1000
+ assert ! ( super :: check_for_banned_extensions( & x) . is_ok( ) ) ;
1001
+
1002
+ let x = parse_with_media_type ( "let x = 1;" , deno_ast:: MediaType :: Cjs ) ;
1003
+ let err = super :: check_for_banned_extensions ( & x) . unwrap_err ( ) ;
1004
+ assert ! (
1005
+ matches!( err, super :: PublishError :: CommonJs { .. } ) ,
1006
+ "{err:?}" ,
1007
+ ) ;
1008
+
1009
+ let x = parse_with_media_type ( "let x = 1;" , deno_ast:: MediaType :: Cts ) ;
1010
+ let err = super :: check_for_banned_extensions ( & x) . unwrap_err ( ) ;
1011
+ assert ! (
1012
+ matches!( err, super :: PublishError :: CommonJs { .. } ) ,
1013
+ "{err:?}" ,
1014
+ ) ;
1015
+ }
1016
+
973
1017
#[ test]
974
1018
fn banned_triple_slash_directives ( ) {
975
1019
let x = parse ( "let x = 1;" ) ;
0 commit comments