ecma-re-validator-0.3.0/0000755000004100000410000000000014056560646015052 5ustar www-datawww-dataecma-re-validator-0.3.0/ecma-re-validator.gemspec0000644000004100000410000000203314056560646021711 0ustar www-datawww-data# frozen_string_literal: true $LOAD_PATH.push File.expand_path('lib', __dir__) require 'ecma-re-validator/version' Gem::Specification.new do |gem| gem.name = 'ecma-re-validator' gem.version = EcmaReValidator::VERSION gem.authors = ['Garen Torikian'] gem.email = ['gjtorikian@gmail.com'] gem.description = %(Validate a regular expression string against what ECMA-262 can actually do.) gem.summary = %(Validate a regular expression string against what ECMA-262 can actually do.) gem.homepage = 'https://github.com/gjtorikian/ecma-re-validator' gem.license = 'MIT' gem.files = `git ls-files -z`.split("\x0").reject { |f| f =~ %r{^vendor/.*} } gem.test_files = gem.files.grep(%r{^(spec)/}) gem.require_paths = ['lib'] gem.add_dependency 'regexp_parser', '~> 2.0' gem.add_development_dependency 'rspec', '~> 3.1' gem.add_development_dependency 'rake', '~> 13.0' gem.add_development_dependency 'awesome_print' gem.add_development_dependency 'pry', '~> 0.10' end ecma-re-validator-0.3.0/.travis.yml0000644000004100000410000000030214056560646017156 0ustar www-datawww-datalanguage: ruby script: "./script/cibuild" rvm: - 2.4 - 2.5 - 2.6 - 2.7 sudo: false cache: bundler notifications: email: on_success: never on_failure: never git: depth: 10 ecma-re-validator-0.3.0/README.md0000644000004100000410000000110714056560646016330 0ustar www-datawww-data# ecma-re-validator [![Build Status](https://travis-ci.org/gjtorikian/ecma-re-validator.svg?branch=master)](https://travis-ci.org/gjtorikian/ecma-re-validator) Pass in a string to validate if it would work in ECMA-262, aka JavaScript. The information for what is valid and what isn't comes from . ## Usage Pass in either a string or a Regexp: ``` ruby require 'ecma-re-validator' re = "[Ss]mith\\\\b" EcmaReValidator.valid?(re) # true re = /(?<=a)b/ EcmaReValidator.valid?(re) # false--lookbehinds don't exist in JS ``` ecma-re-validator-0.3.0/spec/0000755000004100000410000000000014056560646016004 5ustar www-datawww-dataecma-re-validator-0.3.0/spec/conditionals_spec.rb0000644000004100000410000000103214056560646022025 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::Conditionals' do it 'should pass if regexp is using just (?...)' do re = '(?:Aa)' expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should fail if regexp is using complicated if-then-else' do re = '(?(?=condition)(then1|then2|then3)|(else1|else2|else3))' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp is using basic if-then-else' do re = 'b(?(1)c|d)' expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/spec/mode_modifiers_spec.rb0000644000004100000410000000113214056560646022325 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::ModeModifiers' do it 'should fail if regexp has simple option' do re = '(?i)test' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has multiple options' do re = '(?ism)test' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has colon option' do re = '(?mix:abc)' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has hyphen option' do re = 'te(?-i)st' expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/spec/comments_spec.rb0000644000004100000410000000102414056560646021165 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::Comments' do it 'should fail if regexp has inline comments' do re = /(?#comment)hello/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has inline comments across lines' do re = %r{ start # some text \s # white space char (group) # first group (?:alt1|alt2) # some alternation end }x expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/spec/unicode_spec.rb0000644000004100000410000000462014056560646020773 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::Unicode' do it 'should fail if regexp uses \p{L} or \p{Letter}' do re = /\p{L}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Letter}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{M} or \p{Mark}' do re = /\p{M}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Mark}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{Z} or \p{Separator}' do re = /\p{Z}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Separator}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{S} or \p{Symbol}' do re = /\p{S}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Symbol}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{N} or \p{Number}' do re = /\p{N}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Number}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{P} or \p{Punctuation}' do re = /\p{P}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Punctuation}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses \p{C} or \p{Other}' do re = /\p{C}/ expect(EcmaReValidator.valid?(re)).to eql(false) re = /\p{Other}/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp uses a script' do re = /\p{Armenian}/ expect(EcmaReValidator.valid?(re)).to eql(false) end # not yet supported # it 'should fail if regexp uses a block' do # re = /\p{InArmenian}/ # # expect(EcmaReValidator.valid?(re)).to eql(false) # end it 'should pass if regexp uses a \u' do re = /\uf8f8/ expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should pass if regexp uses a \x' do re = /\x22/ expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should pass if regexp uses a \w' do re = /^\w+/ expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should pass if regexp uses a \s' do re = /\s*wow/ expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should pass if regexp uses a \d' do re = /\d$/ expect(EcmaReValidator.valid?(re)).to eql(true) end end ecma-re-validator-0.3.0/spec/lookbehind_spec.rb0000644000004100000410000000120414056560646021456 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::Lookbehind' do it 'should fail if regexp has a positive lookbehind' do re = '(?<=a)b' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should pass if regexp has an escaped positive lookbehind' do re = '\\(?<=a\\)b' expect(EcmaReValidator.valid?(re)).to eql(true) end it 'should fail if regexp has a negative lookbehind' do re = '(?integer|insert|in)\b./ expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/spec/validator_spec.rb0000644000004100000410000000105014056560646021324 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaRe' do it 'should fail if input is not a string or regexp' do re = 92 expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if string is not resolvable' do re = '(\w' expect(EcmaReValidator.valid?(re)).to eql(false) end it 'passes for a valid regexp string' do re = "[Ss]mith\\\\b" expect(EcmaReValidator.valid?(re)).to eql(true) end it 'passes for a valid regexp' do re = /[Ss]mith\\\\b/ expect(EcmaReValidator.valid?(re)).to eql(true) end end ecma-re-validator-0.3.0/spec/named_capture_groups_spec.rb0000644000004100000410000000060014056560646023545 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::NamedCaptureGroups' do it 'should fail if regexp has named capture group using ?<>' do re = /(?group)/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has named capture group using ?\'\'' do re = /(?'name'group)/ expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/spec/possesive_quantifiers_spec.rb0000644000004100000410000000076514056560646024005 0ustar www-datawww-datarequire 'spec_helper' describe 'EcmaReValidator::PossesiveQuantifiers' do it 'should fail if regexp has *+ possesive quantifier' do re = /"[^"]*+"/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has ++ possesive quantifier' do re = /"[^"]++"/ expect(EcmaReValidator.valid?(re)).to eql(false) end it 'should fail if regexp has ?+ possesive quantifier' do re = /"[^"]?+"/ expect(EcmaReValidator.valid?(re)).to eql(false) end end ecma-re-validator-0.3.0/.gitignore0000644000004100000410000000047514056560646017050 0ustar www-datawww-data# File generated by script/bootstrap /.bundle/ /bin /vendor/ruby /vendor/gems/ /vendor/cache/ruby/ *.rbc .bundle .config coverage InstalledFiles lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp Gemfile.lock out/ sample.rb run_sample.rb src/ docs/ # YARD artifacts .yardoc _yardoc doc/ .DS_Store ecma-re-validator-0.3.0/script/0000755000004100000410000000000014056560646016356 5ustar www-datawww-dataecma-re-validator-0.3.0/script/cibuild0000755000004100000410000000005114056560646017713 0ustar www-datawww-data#!/bin/sh set -e bundle exec rake spec ecma-re-validator-0.3.0/script/bootstrap0000755000004100000410000000011014056560646020311 0ustar www-datawww-data#!/bin/bash bundle install --local --binstubs --path vendor/cache "$@" ecma-re-validator-0.3.0/LICENSE0000644000004100000410000000207214056560646016060 0ustar www-datawww-dataThe MIT License (MIT) Copyright (c) 2015 Garen Torikian Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ecma-re-validator-0.3.0/Rakefile0000644000004100000410000000021714056560646016517 0ustar www-datawww-datarequire 'bundler' Bundler::GemHelper.install_tasks require 'rspec/core/rake_task' RSpec::Core::RakeTask.new(:spec) task :default => [:spec] ecma-re-validator-0.3.0/lib/0000755000004100000410000000000014056560646015620 5ustar www-datawww-dataecma-re-validator-0.3.0/lib/ecma-re-validator.rb0000644000004100000410000000237014056560646021443 0ustar www-datawww-databegin require 'awesome_print' require 'pry' rescue LoadError; end require 'regexp_parser' module EcmaReValidator # JS doesn't have Unicode matching UNICODE_CHARACTERS = Regexp::Syntax::Token::UnicodeProperty::All INVALID_REGEXP = [ # JS doesn't have \A, \Z, \z :bos, :eos_ob_eol, :eos, # JS doesn't have lookbehinds :lookbehind, :nlookbehind, # JS doesn't have atomic grouping :atomic, # JS doesn't have possesive quantifiers :zero_or_one_possessive, :zero_or_more_possessive, :one_or_more_possessive, # JS doesn't have named capture groups :named_ab, :named_sq, # JS doesn't support modifying options :options, :options_switch, # JS doesn't support conditionals :condition_open, # JS doesn't support comments :comment ] INVALID_TOKENS = INVALID_REGEXP + UNICODE_CHARACTERS def self.valid?(input) if input.is_a? String begin input = Regexp.new(input) rescue RegexpError return false end elsif !input.is_a? Regexp return false end Regexp::Scanner.scan(input).none? do |t| if t[1] == :word || t[1] == :space || t[1] == :digit t[0] != :type else INVALID_TOKENS.include?(t[1]) end end end end ecma-re-validator-0.3.0/lib/ecma-re-validator/0000755000004100000410000000000014056560646021114 5ustar www-datawww-dataecma-re-validator-0.3.0/lib/ecma-re-validator/version.rb0000644000004100000410000000011614056560646023124 0ustar www-datawww-data# frozen_string_literal: true module EcmaReValidator VERSION = '0.3.0' end ecma-re-validator-0.3.0/Gemfile0000644000004100000410000000004614056560646016345 0ustar www-datawww-datasource 'http://rubygems.org' gemspec